Skip to content

Commit

Permalink
Do not ICE on unmet trait alias bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
eggyal committed Feb 16, 2023
1 parent 999ac5f commit 540bd98
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
17 changes: 11 additions & 6 deletions compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// This is the "trait" (meaning, the predicate "proved" by this `impl`) which provides the `Self` type we care about.
// For the purposes of this function, we hope that it is a `struct` type, and that our current `expr` is a literal of
// that struct type.
let impl_trait_self_ref: Option<ty::TraitRef<'tcx>> =
self.tcx.impl_trait_ref(obligation.impl_def_id).map(|impl_def| impl_def.skip_binder());

let Some(impl_trait_self_ref) = impl_trait_self_ref else {
// It is possible that this is absent. In this case, we make no progress.
return Err(expr);
let impl_trait_self_ref = if self.tcx.is_trait_alias(obligation.impl_def_id) {
self.tcx.mk_trait_ref(
obligation.impl_def_id,
ty::InternalSubsts::identity_for_item(self.tcx, obligation.impl_def_id),
)
} else {
self.tcx
.impl_trait_ref(obligation.impl_def_id)
.map(|impl_def| impl_def.skip_binder())
// It is possible that this is absent. In this case, we make no progress.
.ok_or(expr)?
};

// We only really care about the `Self` type itself, which we extract from the ref.
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/traits/alias/issue-108072-unmet-trait-alias-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Regression test for #108072: do not ICE upon unmet trait alias constraint

#![feature(trait_alias)]

trait IteratorAlias = Iterator;

fn f(_: impl IteratorAlias) {}

fn main() {
f(()) //~ `()` is not an iterator
}
19 changes: 19 additions & 0 deletions tests/ui/traits/alias/issue-108072-unmet-trait-alias-bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0277]: `()` is not an iterator
--> $DIR/issue-108072-unmet-trait-alias-bound.rs:10:7
|
LL | f(())
| - ^^ `()` is not an iterator
| |
| required by a bound introduced by this call
|
= help: the trait `Iterator` is not implemented for `()`
= note: required for `()` to implement `IteratorAlias`
note: required by a bound in `f`
--> $DIR/issue-108072-unmet-trait-alias-bound.rs:7:14
|
LL | fn f(_: impl IteratorAlias) {}
| ^^^^^^^^^^^^^ required by this bound in `f`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit 540bd98

Please sign in to comment.