Skip to content

Commit

Permalink
typeck: Make sure casts to or from fat pointers are illegal
Browse files Browse the repository at this point in the history
Fixes ICEs where non-fat pointers and scalars are cast to fat pointers,

Fixes #21397
Fixes #22955
Fixes #23237
Fixes #24100
  • Loading branch information
Ulrik Sverdrup committed May 1, 2015
1 parent 2568a4d commit 4806fb2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/cast.rs
Expand Up @@ -170,9 +170,10 @@ pub fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
demand::coerce(fcx, e.span, t_1, &e);
}
}
} else if fcx.type_is_fat_ptr(t_e, span) && !fcx.type_is_fat_ptr(t_1, span) {
} else if fcx.type_is_fat_ptr(t_e, span) != fcx.type_is_fat_ptr(t_1, span) {
fcx.type_error_message(span, |actual| {
format!("illegal cast; cast from fat pointer: `{}` as `{}`",
format!("illegal cast; cast to or from fat pointer: `{}` as `{}` \
involving incompatible type.",
actual, fcx.infcx().ty_to_string(t_1))
}, t_e, None);
} else if !(t_e_is_scalar && t_1_is_trivial) {
Expand Down
18 changes: 15 additions & 3 deletions src/test/compile-fail/fat-ptr-cast.rs
Expand Up @@ -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 illegal cast
b as usize; //~ ERROR illegal cast
p as usize; //~ ERROR illegal cast

// #22955
q as *const [i32]; //~ ERROR illegal cast

a as usize; //~ ERROR cast from fat pointer
b as usize; //~ ERROR cast from fat pointer
p as usize; //~ ERROR cast from fat pointer
// #21397
let t: *mut (Trait + 'static) = 0 as *mut _; //~ ERROR illegal cast
let mut fail: *const str = 0 as *const str; //~ ERROR illegal cast
}
8 changes: 4 additions & 4 deletions src/test/compile-fail/issue-22034.rs
Expand Up @@ -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
};
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-22289.rs
Expand Up @@ -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
}

0 comments on commit 4806fb2

Please sign in to comment.