Skip to content

Commit

Permalink
When cast needs a dereference point at full cast
Browse files Browse the repository at this point in the history
After the fix of #37453 in PR #37369, instead of pointing at only the
cast type, point at the full cast span when a cast needs a dereference:

```
error: casting `&{float}` as `f32` is invalid
  --> ../../../src/test/ui/mismatched_types/cast-rfc0401.rs:81:30
   |
81 |     vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
   |                              ^^^^^^^^ cannot cast `&{float}` as `f32`
   |
help: did you mean `*s`?
  --> ../../../src/test/ui/mismatched_types/cast-rfc0401.rs:81:30
   |
81 |     vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
   |                              ^
```

instead of

```
error: casting `&{float}` as `f32` is invalid
  --> ../../../src/test/ui/mismatched_types/cast-rfc0401.rs:81:35
   |
81 |     vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
   |                              -    ^^^
   |                              |
   |                              |
   |                              did you mean `*s`?
   |                              cannot cast `&{float}` as `f32`
```
  • Loading branch information
estebank committed Dec 15, 2016
1 parent 6483bdd commit 868fb03
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 126 deletions.
7 changes: 4 additions & 3 deletions src/librustc_typeck/check/cast.rs
Expand Up @@ -142,20 +142,21 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
match e {
CastError::NeedDeref => {
let error_span = self.span;
let cast_ty = fcx.ty_to_string(self.cast_ty);
let mut err = fcx.type_error_struct(self.cast_span,
let mut err = fcx.type_error_struct(error_span,
|actual| {
format!("casting `{}` as `{}` is invalid",
actual,
cast_ty)
},
self.expr_ty);
err.span_label(self.expr.span,
err.span_label(error_span,
&format!("cannot cast `{}` as `{}`",
fcx.ty_to_string(self.expr_ty),
cast_ty));
if let Ok(snippet) = fcx.sess().codemap().span_to_snippet(self.expr.span) {
err.span_label(self.expr.span,
err.span_help(self.expr.span,
&format!("did you mean `*{}`?", snippet));
}
err.emit();
Expand Down
123 changes: 0 additions & 123 deletions src/test/compile-fail/cast-rfc0401.rs

This file was deleted.

82 changes: 82 additions & 0 deletions src/test/ui/mismatched_types/cast-rfc0401.rs
@@ -0,0 +1,82 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn illegal_cast<U:?Sized,V:?Sized>(u: *const U) -> *const V
{
u as *const V
}

fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
{
u as *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
}

fn main()
{
let f: f32 = 1.2;
let v = 0 as *const u8;
let fat_v : *const [u8] = unsafe { &*(0 as *const [u8; 1])};
let fat_sv : *const [i8] = unsafe { &*(0 as *const [i8; 1])};
let foo: &Foo = &f;

let _ = v as &u8;
let _ = v as E;
let _ = v as fn();
let _ = v as (u32,);
let _ = Some(&v) as *const u8;

let _ = v as f32;
let _ = main as f64;
let _ = &v as usize;
let _ = f as *const u8;
let _ = 3_i32 as bool;
let _ = E::A as bool;
let _ = 0x61u32 as char;

let _ = false as f32;
let _ = E::A as f32;
let _ = 'a' as f32;

let _ = false as *const u8;
let _ = E::A as *const u8;
let _ = 'a' as *const u8;

let _ = 42usize as *const [u8];
let _ = v as *const [u8];
let _ = fat_v as *const Foo;
let _ = foo as *const str;
let _ = foo as *mut str;
let _ = main as *mut str;
let _ = &f as *mut f32;
let _ = &f as *const f64;
let _ = fat_sv as usize;

let a : *const str = "hello";
let _ = a as *const Foo;

// check no error cascade
let _ = main.f as *const u32;

let cf: *const Foo = &0;
let _ = cf as *const [u16];
let _ = cf as *const Bar;

vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
}

0 comments on commit 868fb03

Please sign in to comment.