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

This commit is contained in:
Eduard-Mihai Burtescu 2017-03-07 16:17:16 +02:00
parent b04ebef432
commit 319890487a
3 changed files with 74 additions and 3 deletions

View file

@ -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);
@ -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);
}
@ -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);

View file

@ -0,0 +1,41 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn prove_static<T: 'static + ?Sized>(_: &'static T) {}
fn lifetime_transmute_slice<'a, T: ?Sized>(x: &'a T, y: &T) -> &'a T {
let mut out = [x];
//~^ ERROR cannot infer an appropriate lifetime due to conflicting requirements
{
let slice: &mut [_] = &mut out;
slice[0] = y;
}
out[0]
}
struct Struct<T, U: ?Sized> {
head: T,
_tail: U
}
fn lifetime_transmute_struct<'a, T: ?Sized>(x: &'a T, y: &T) -> &'a T {
let mut out = Struct { head: x, _tail: [()] };
//~^ ERROR cannot infer an appropriate lifetime due to conflicting requirements
{
let dst: &mut Struct<_, [()]> = &mut out;
dst.head = y;
}
out.head
}
fn main() {
prove_static(lifetime_transmute_slice("", &String::from("foo")));
prove_static(lifetime_transmute_struct("", &String::from("bar")));
}

View file

@ -0,0 +1,30 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn save_ref<'a>(refr: &'a i32, to: &mut [&'a i32]) {
for val in &mut *to {
*val = refr;
}
}
fn main() {
let ref init = 0i32;
let ref mut refr = 1i32;
let mut out = [init];
save_ref(&*refr, &mut out);
// This shouldn't be allowed as `refr` is borrowed
*refr = 3; //~ ERROR cannot assign to `*refr` because it is borrowed
// Prints 3?!
println!("{:?}", out[0]);
}