Skip to content

Commit

Permalink
Rollup merge of rust-lang#40319 - eddyb:it's-"unsize"-not-"unsound", …
Browse files Browse the repository at this point in the history
…r=nikomatsakis

Disallow subtyping between T and U in T: Unsize<U>.

Because `&mut T` can be coerced to `&mut U`, `T` and `U` must be unified invariantly. Fixes rust-lang#40288.
E.g. coercing `&mut [&'a X; N]` to `&mut [&'b X]` must require `'a` be equal to `'b`, otherwise you can convert between `&'a X` and `&'b X` (in either direction), potentially unsoundly lengthening lifetimes.

Subtyping here was introduced with `Unsize` in rust-lang#24619 (landed in 1.1, original PR is rust-lang#23785).
  • Loading branch information
Ariel Ben-Yehuda authored Mar 11, 2017
2 parents b49036c + cfb41ae commit c886815
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 130 deletions.
6 changes: 3 additions & 3 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
let new_trait = tcx.mk_dynamic(
ty::Binder(tcx.mk_existential_predicates(iter)), r_b);
let InferOk { obligations, .. } =
self.infcx.sub_types(false, &obligation.cause, new_trait, target)
self.infcx.eq_types(false, &obligation.cause, new_trait, target)
.map_err(|_| Unimplemented)?;
self.inferred_obligations.extend(obligations);

Expand Down Expand Up @@ -2520,7 +2520,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// [T; n] -> [T].
(&ty::TyArray(a, _), &ty::TySlice(b)) => {
let InferOk { obligations, .. } =
self.infcx.sub_types(false, &obligation.cause, a, b)
self.infcx.eq_types(false, &obligation.cause, a, b)
.map_err(|_| Unimplemented)?;
self.inferred_obligations.extend(obligations);
}
Expand Down Expand Up @@ -2583,7 +2583,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
});
let new_struct = tcx.mk_adt(def, tcx.mk_substs(params));
let InferOk { obligations, .. } =
self.infcx.sub_types(false, &obligation.cause, new_struct, target)
self.infcx.eq_types(false, &obligation.cause, new_struct, target)
.map_err(|_| Unimplemented)?;
self.inferred_obligations.extend(obligations);

Expand Down
14 changes: 12 additions & 2 deletions src/librustc_typeck/check/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use astconv::AstConv;

use super::FnCtxt;

use rustc::infer::InferOk;
use rustc::traits;
use rustc::ty::{self, Ty, TraitRef};
use rustc::ty::{ToPredicate, TypeFoldable};
Expand Down Expand Up @@ -149,6 +150,14 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {

pub fn finalize<'b, I>(self, pref: LvaluePreference, exprs: I)
where I: IntoIterator<Item = &'b hir::Expr>
{
let fcx = self.fcx;
fcx.register_infer_ok_obligations(self.finalize_as_infer_ok(pref, exprs));
}

pub fn finalize_as_infer_ok<'b, I>(self, pref: LvaluePreference, exprs: I)
-> InferOk<'tcx, ()>
where I: IntoIterator<Item = &'b hir::Expr>
{
let methods: Vec<_> = self.steps
.iter()
Expand Down Expand Up @@ -176,8 +185,9 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
}
}

for obligation in self.obligations {
self.fcx.register_predicate(obligation);
InferOk {
value: (),
obligations: self.obligations
}
}
}
Expand Down
Loading

0 comments on commit c886815

Please sign in to comment.