Prohibit casts between fat pointers to different traits

This makes them compliant with the new version of RFC 401 (i.e.
    RFC 1052).

Fixes #26391. I *hope* the tests I have are enough.

This is a [breaking-change]
This commit is contained in:
Ariel Ben-Yehuda 2015-06-18 15:51:06 +03:00
parent 0d82fb55db
commit ea7637ebc0
4 changed files with 24 additions and 8 deletions

View file

@ -21,6 +21,9 @@ fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
trait Foo { fn foo(&self) {} }
impl<T> Foo for T {}
trait Bar { fn foo(&self) {} }
impl<T> Bar for T {}
enum E {
A, B
}
@ -72,4 +75,7 @@ fn main()
// check no error cascade
let _ = main.f as *const u32; //~ ERROR attempted access of field
let cf: *const Foo = &0;
let _ = cf as *const [u8]; //~ ERROR vtable kinds
let _ = cf as *const Bar; //~ ERROR vtable kinds
}

View file

@ -23,12 +23,11 @@ impl<T> Foo<T> for () {}
impl Foo<u32> for u32 { fn foo(&self, _: u32) -> u32 { self+43 } }
impl Bar for () {}
unsafe fn fool<'a>(t: *const (Foo<u32>+'a)) -> u32 {
let bar : *const Bar = t as *const Bar;
unsafe fn round_trip_and_call<'a>(t: *const (Foo<u32>+'a)) -> u32 {
let foo_e : *const Foo<u16> = t as *const _;
let r_1 = foo_e as *mut Foo<u32>;
(&*r_1).foo(0)*(&*(bar as *const Foo<u32>)).foo(0)
(&*r_1).foo(0)
}
#[repr(C)]
@ -43,8 +42,8 @@ fn foo_to_bar<T:?Sized>(u: *const FooS<T>) -> *const BarS<T> {
fn main() {
let x = 4u32;
let y : &Foo<u32> = &x;
let fl = unsafe { fool(y as *const Foo<u32>) };
assert_eq!(fl, (43+4)*(43+4));
let fl = unsafe { round_trip_and_call(y as *const Foo<u32>) };
assert_eq!(fl, (43+4));
let s = FooS([0,1,2]);
let u: &FooS<[u32]> = &s;

View file

@ -99,6 +99,12 @@ fn main()
let l_via_str = unsafe{&*(s as *const [u8])};
assert_eq!(&l, l_via_str);
// ptr-ptr-cast (both vk=Length, check length is preserved)
let l: [[u8; 3]; 2] = [[3, 2, 6], [4, 5, 1]];
let p: *const [[u8; 3]] = &l;
let p: &[[u8; 2]] = unsafe {&*(p as *const [[u8; 2]])};
assert_eq!(p, [[3, 2], [6, 4]]);
// enum-cast
assert_eq!(Simple::A as u8, 0);
assert_eq!(Simple::B as u8, 1);