Auto merge of #25038 - bluss:fat-pointer-cast, r=luqmana

typeck: Make sure casts from other types to fat pointers are illegal

Fixes ICEs where non-fat pointers and scalars are cast to fat pointers,

Fixes #21397
Fixes #22955
Fixes #23237
Fixes #24100
This commit is contained in:
bors 2015-05-02 17:44:11 +00:00
commit 5574029b68
4 changed files with 23 additions and 10 deletions

View file

@ -8,12 +8,24 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Make sure casts between thin pointer <-> fat pointer are illegal.
pub trait Trait {}
fn main() {
let a: &[i32] = &[1, 2, 3];
let b: Box<[i32]> = Box::new([1, 2, 3]);
let p = a as *const [i32];
let q = a.as_ptr();
a as usize; //~ ERROR cast from fat pointer
b as usize; //~ ERROR cast from fat pointer
p as usize; //~ ERROR cast from fat pointer
a as usize; //~ ERROR illegal cast
b as usize; //~ ERROR illegal cast
p as usize; //~ ERROR illegal cast
// #22955
q as *const [i32]; //~ ERROR illegal cast
// #21397
let t: *mut (Trait + 'static) = 0 as *mut _; //~ ERROR illegal cast
let mut fail: *const str = 0 as *const str; //~ ERROR illegal cast
}

View file

@ -11,9 +11,9 @@
extern crate libc;
fn main() {
let foo: *mut libc::c_void;
let cb: &mut Fn() = unsafe {
&mut *(foo as *mut Fn())
//~^ ERROR use of possibly uninitialized variable: `foo`
let ptr: *mut () = 0 as *mut _;
let _: &mut Fn() = unsafe {
&mut *(ptr as *mut Fn())
//~^ ERROR illegal cast
};
}

View file

@ -9,5 +9,5 @@
// except according to those terms.
fn main() {
0 as &std::any::Any; //~ ERROR non-scalar cast: `i32` as `&core::any::Any`
0 as &std::any::Any; //~ ERROR illegal cast
}