10000 Allow opaque types in trait impl headers and rely on coherence to reject unsound cases by oli-obk · Pull Request #103488 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Allow opaque types in trait impl headers and rely on coherence to reject unsound cases #103488

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

Merged
merged 10 commits into from
Nov 23, 2022
Merged
Prev Previous commit
Test generalization during coherence
  • Loading branch information
oli-obk committed Nov 21, 2022
commit c16a90f5e3cb26b15e12c8d24d7b1cafbe90e24a
6 changes: 5 additions & 1 deletion compiler/rustc_infer/src/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
}

fn mark_ambiguous(&mut self) {
self.infcx.tcx.sess.delay_span_bug(self.cause.span, "we only generalize opaque types in situations where we already error for them elsewhere in coherence");
span_bug!(self.cause.span, "opaque types are handled in `tys`");
}

fn binders<T>(
Expand Down Expand Up @@ -675,6 +675,10 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
// relatable.
Ok(t)
}
ty::Opaque(def_id, substs) => {
let s = self.relate(substs, substs)?;
Ok(if s == substs { t } else { self.infcx.tcx.mk_opaque(def_id, s) })
}
_ => relate::super_relate_tys(self, t, t),
}?;

Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/type-alias-impl-trait/coherence_generalization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// check-pass

#![feature(type_alias_impl_trait)]
trait Trait {}
type Opaque<T> = impl Sized;
fn foo<T>() -> Opaque<T> {
()
}

impl<T, V> Trait for (T, V, V, u32) {}
impl<U, V> Trait for (Opaque<U>, V, i32, V) {}

fn main() {}
0