Skip to content

Make sure casts from other types to fat pointers are illegal #25038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this test case was actually testing the wrong thing. The associated issue is actually just another instance of casting thin <-> fat pointers, and this test case should not have closed that issue. Note that the error is complaining about something completely unrelated to why the code is actually invalid.

It would be great if you could actually fix up this test case to avoid the other reasons why this case is invalid. A corrected test case looks like

fn main() {
    let ptr: *mut () = 0 as *mut _;
    let _: &mut Fn() = unsafe {
        &mut *(ptr as *mut Fn())
    };
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely. Finding this test case was a bit confusing. Thanks for the test code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

};
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-22289.rs
Original file line number Diff line number Diff line change
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
}