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 wf of existential projections #77186

Closed
wants to merge 2 commits 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
59 changes: 59 additions & 0 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,65 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
));
}
}

// We also check the well formedness of projections, to prevent things like #27675
//
// ```rust
// trait Setup {
// type From: Copy;
// }
//
// fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
// *from
// }
//
// pub fn copy_any<T>(t: &T) -> T {
// copy::<dyn Setup<From=T>>(t)
// }
// ```
if let Some(data) = data.no_bound_vars() {
let tcx = self.infcx.tcx;
for pred in data {
let projection = match pred {
ty::ExistentialPredicate::Trait(_) | ty::ExistentialPredicate::AutoTrait(_) => {
continue; // Nothing to do here.
}
ty::ExistentialPredicate::Projection(proj) => proj,
};

let proj = projection.with_self_ty(tcx, tcx.types.self_param);
let self_ty = tcx.mk_projection(projection.item_def_id, proj.projection_ty.substs);

let preds = tcx
.predicates_of(projection.item_def_id)
.instantiate(tcx, proj.projection_ty.substs);
for pred in preds.predicates.iter() {
match pred.skip_binders() {
ty::PredicateAtom::Trait(pred, ct) => {
// If we have `<Self as Trait>::AssocTy: Trait`,
// `projection.ty: Trait` must hold.
if pred.self_ty() == self_ty {
let trait_ref = ty::TraitRef::new(
pred.def_id(),
tcx.mk_substs_trait(projection.ty, &pred.trait_ref.substs[1..]),
);
// FIXME: Use a better obligation cause here.
let cause = self.cause(traits::MiscObligation);
self.out.push(traits::Obligation::new(
cause,
self.param_env,
ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref }, ct)
.to_predicate(tcx),
));
}
}
// FIXME: Do we have to do something for other predicates here,
// there is probably still subtle unsoundness here.
_ => (),
}
}
}
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/wf/existential-projections-wf-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Taken from https://github.com/rust-lang/rust/issues/27675#issuecomment-696956878
trait Setup {
type From: Copy;
}

fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
*from
}

pub fn copy_any<T>(t: &T) -> T {
copy::<dyn Setup<From=T>>(t)
//~^ ERROR the trait bound `T: Copy`
}

fn main() {
let st = String::from("Hello");
copy_any(&st);
}
14 changes: 14 additions & 0 deletions src/test/ui/wf/existential-projections-wf-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0277]: the trait bound `T: Copy` is not satisfied
--> $DIR/existential-projections-wf-1.rs:11:31
|
LL | copy::<dyn Setup<From=T>>(t)
| ^ the trait `Copy` is not implemented for `T`
|
help: consider restricting type parameter `T`
|
LL | pub fn copy_any<T: Copy>(t: &T) -> T {
| ^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
22 changes: 22 additions & 0 deletions src/test/ui/wf/existential-projections-wf-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Taken from https://github.com/rust-lang/rust/issues/27675#issuecomment-696956785
trait Id<T>: Sized {
fn id(self) -> T;
}
impl<T> Id<T> for T {
fn id(self) -> T { self }
}

trait Setup<T> {
type From: Id<T>;
}

fn transmute<T, U: Setup<T> + ?Sized>(from: U::From) -> T {
Id::id(from)
}

pub fn safe_transmute<T, U>(t: T) -> U {
transmute::<U, dyn Setup<U, From=T>>(t)
//~^ ERROR the trait bound
}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/wf/existential-projections-wf-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0277]: the trait bound `T: Id<U>` is not satisfied
--> $DIR/existential-projections-wf-2.rs:18:42
|
LL | transmute::<U, dyn Setup<U, From=T>>(t)
| ^ the trait `Id<U>` is not implemented for `T`
|
help: consider restricting type parameter `T`
|
LL | pub fn safe_transmute<T: Id<U>, U>(t: T) -> U {
| ^^^^^^^

error: aborting due to previous error

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