Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ICE on unmet trait alias bounds #108115

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
eggyal marked this conversation as resolved.
Show resolved Hide resolved
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`.