Navigation Menu

Skip to content

Commit

Permalink
Improve reference cast help message
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 16, 2016
1 parent c11e2bd commit 37903bf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 32 deletions.
59 changes: 36 additions & 23 deletions src/librustc_typeck/check/cast.rs
Expand Up @@ -105,7 +105,6 @@ enum CastError {
NeedViaPtr,
NeedViaThinPtr,
NeedViaInt,
NeedViaUsize,
NonScalar,
}

Expand Down Expand Up @@ -139,26 +138,39 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {

fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
match e {
CastError::NeedViaPtr |
CastError::NeedViaThinPtr |
CastError::NeedViaInt |
CastError::NeedViaUsize => {
CastError::NeedViaPtr => {
let mut err = fcx.type_error_struct(self.span,
|actual| {
format!("casting `{}` as `{}` is invalid",
actual,
fcx.ty_to_string(self.cast_ty))
},
self.expr_ty);
if self.cast_ty.is_uint() {
err.help(&format!("cast through {} first",
match e {
CastError::NeedViaPtr => "a raw pointer",
CastError::NeedViaThinPtr => "a thin pointer",
_ => bug!(),
}));
}
err.emit();
}
CastError::NeedViaInt => {
fcx.type_error_struct(self.span,
|actual| {
format!("casting `{}` as `{}` is invalid",
actual,
fcx.ty_to_string(self.cast_ty))
},
self.expr_ty)
.help(&format!("cast through {} first",
match e {
CastError::NeedViaPtr => "a raw pointer",
CastError::NeedViaThinPtr => "a thin pointer",
CastError::NeedViaInt => "an integer",
CastError::NeedViaUsize => "a usize",
_ => bug!(),
}))
.emit();
|actual| {
format!("casting `{}` as `{}` is invalid",
actual,
fcx.ty_to_string(self.cast_ty))
},
self.expr_ty)
.help(&format!("cast through {} first",
match e {
CastError::NeedViaInt => "an integer",
_ => bug!(),
}))
.emit();
}
CastError::CastToBool => {
struct_span_err!(fcx.tcx.sess, self.span, E0054, "cannot cast as `bool`")
Expand Down Expand Up @@ -366,21 +378,23 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
(Int(Bool), Float) |
(Int(CEnum), Float) |
(Int(Char), Float) => Err(CastError::NeedViaInt),

(Int(Bool), Ptr(_)) |
(Int(CEnum), Ptr(_)) |
(Int(Char), Ptr(_)) => Err(CastError::NeedViaUsize),
(Int(Char), Ptr(_)) |
(Ptr(_), Float) |
(FnPtr, Float) |
(Float, Ptr(_)) => Err(CastError::IllegalCast),

// ptr -> *
(Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast
(Ptr(m_expr), Int(_)) => self.check_ptr_addr_cast(fcx, m_expr), // ptr-addr-cast
(Ptr(_), Float) | (FnPtr, Float) => Err(CastError::NeedViaUsize),
(FnPtr, Int(_)) => Ok(CastKind::FnPtrAddrCast),
(RPtr(_), Int(_)) |
(RPtr(_), Float) => Err(CastError::NeedViaPtr),
// * -> ptr
(Int(_), Ptr(mt)) => self.check_addr_ptr_cast(fcx, mt), // addr-ptr-cast
(FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
(Float, Ptr(_)) => Err(CastError::NeedViaUsize),
(RPtr(rmt), Ptr(mt)) => self.check_ref_cast(fcx, rmt, mt), // array-ptr-cast

// prim -> prim
Expand All @@ -391,7 +405,6 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
(Int(_), Int(_)) | (Int(_), Float) | (Float, Int(_)) | (Float, Float) => {
Ok(CastKind::NumericCast)
}

}
}

Expand Down
6 changes: 0 additions & 6 deletions src/test/compile-fail/cast-rfc0401.rs
Expand Up @@ -48,16 +48,13 @@ fn main()

let _ = v as f32;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = main as f64;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = &v as usize;
//~^ ERROR casting
//~^^ HELP through a raw pointer first
let _ = f as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = 3_i32 as bool;
//~^ ERROR cannot cast as `bool` [E0054]
//~| unsupported cast
Expand All @@ -80,13 +77,10 @@ fn main()

let _ = false as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = E::A as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = 'a' as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first

let _ = 42usize as *const [u8]; //~ ERROR casting
let _ = v as *const [u8]; //~ ERROR cannot cast
Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/fat-ptr-cast.rs
Expand Up @@ -19,6 +19,9 @@ fn main() {

a as usize; //~ ERROR casting
//~^ HELP cast through a raw pointer first
a as isize; //~ ERROR casting
a as i16; //~ ERROR casting `&[i32]` as `i16` is invalid
a as u32; //~ ERROR casting `&[i32]` as `u32` is invalid
b as usize; //~ ERROR non-scalar cast
p as usize;
//~^ ERROR casting
Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/issue-17444.rs
Expand Up @@ -15,5 +15,4 @@ enum Test {
fn main() {
let _x = Test::Foo as *const isize;
//~^ ERROR casting `Test` as `*const isize` is invalid
//~^^ HELP cast through a usize first
}
1 change: 0 additions & 1 deletion src/test/compile-fail/issue-21554.rs
Expand Up @@ -13,5 +13,4 @@ struct Inches(i32);
fn main() {
Inches as f32;
//~^ ERROR casting
//~^^ cast through a usize first
}
1 change: 0 additions & 1 deletion src/test/compile-fail/typeck-cast-pointer-to-float.rs
Expand Up @@ -12,5 +12,4 @@ fn main() {
let x : i16 = 22;
((&x) as *const i16) as f32;
//~^ ERROR casting `*const i16` as `f32` is invalid
//~^^ HELP cast through a usize first
}

0 comments on commit 37903bf

Please sign in to comment.