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

Check for opaque types behind projections #99857

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions compiler/rustc_typeck/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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()) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/type-alias-impl-trait/impl_trait_for_tait.rs
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions src/test/ui/type-alias-impl-trait/impl_trait_for_tait.stderr
Original file line number Diff line number Diff line change
@@ -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