diff --git a/compiler/rustc_typeck/src/coherence/orphan.rs b/compiler/rustc_typeck/src/coherence/orphan.rs index 697ef7bc02237..7a990e6122db9 100644 --- a/compiler/rustc_typeck/src/coherence/orphan.rs +++ b/compiler/rustc_typeck/src/coherence/orphan.rs @@ -52,12 +52,14 @@ fn do_orphan_check_impl<'tcx>( // Ensure no opaque types are present in this impl header. See issues #76202 and #86411 for examples, // and #84660 where it would otherwise allow unsoundness. - if trait_ref.has_opaque_types() { + if trait_ref.has_opaque_types() || trait_ref.has_projections() { + let param_env = tcx.param_env(def_id); trace!("{:#?}", item); // First we find the opaque type in question. for ty in trait_ref.substs { for ty in ty.walk() { let ty::subst::GenericArgKind::Type(ty) = ty.unpack() else { continue }; + let ty = tcx.try_normalize_erasing_regions(param_env, ty).unwrap_or(ty); let ty::Opaque(def_id, _) = *ty.kind() else { continue }; trace!(?def_id); @@ -99,7 +101,6 @@ fn do_orphan_check_impl<'tcx>( return Err(reported); } } - span_bug!(sp, "opaque type not found, but `has_opaque_types` is set") } match traits::orphan_check(tcx, item.def_id.to_def_id()) { diff --git a/src/test/ui/type-alias-impl-trait/impl_trait_for_tait.rs b/src/test/ui/type-alias-impl-trait/impl_trait_for_tait.rs new file mode 100644 index 0000000000000..22d496ac678bb --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/impl_trait_for_tait.rs @@ -0,0 +1,21 @@ +// compile-flags: --crate-type=lib + +#![feature(type_alias_impl_trait)] +type Alias = impl Sized; + +fn constrain() -> Alias { + 1i32 +} + +trait HideIt { + type Assoc; +} + +impl HideIt for () { + type Assoc = Alias; +} + +pub trait Yay {} + +impl Yay for <() as HideIt>::Assoc {} +//~^ ERROR: cannot implement trait on type alias impl trait diff --git a/src/test/ui/type-alias-impl-trait/impl_trait_for_tait.stderr b/src/test/ui/type-alias-impl-trait/impl_trait_for_tait.stderr new file mode 100644 index 0000000000000..93563ce82c40a --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/impl_trait_for_tait.stderr @@ -0,0 +1,14 @@ +error: cannot implement trait on type alias impl trait + --> $DIR/impl_trait_for_tait.rs:20:1 + | +LL | impl Yay for <() as HideIt>::Assoc {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: type alias impl trait defined here + --> $DIR/impl_trait_for_tait.rs:4:14 + | +LL | type Alias = impl Sized; + | ^^^^^^^^^^ + +error: aborting due to previous error +