Skip to content

Commit 2f4ca4e

Browse files
authored
Rollup merge of rust-lang#38245 - estebank:cast-deref-hint-2, r=nikomatsakis
When cast needs a dereference point at full cast After the fix of rust-lang#37453 in PR rust-lang#37369, instead of pointing at only the cast type, point at the full cast span when a cast needs a dereference, as well as assign the error label to the correct span for proper coloring: <img width="471" alt="error span pointing at the entire cast" src="https://cloud.githubusercontent.com/assets/1606434/21024245/8797fc2e-bd38-11e6-82c1-66c281c656c1.png"> instead of <img width="471" alt="error span pointing at the type of the cast" src="https://cloud.githubusercontent.com/assets/1606434/21023777/d4814aa6-bd36-11e6-9fc3-b2a0ea5ee15d.png"> Move `compile-fail` test to `ui` test.
2 parents 4cfb2b4 + 868fb03 commit 2f4ca4e

File tree

4 files changed

+326
-126
lines changed

4 files changed

+326
-126
lines changed

src/librustc_typeck/check/cast.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,21 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
142142
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
143143
match e {
144144
CastError::NeedDeref => {
145+
let error_span = self.span;
145146
let cast_ty = fcx.ty_to_string(self.cast_ty);
146-
let mut err = fcx.type_error_struct(self.cast_span,
147+
let mut err = fcx.type_error_struct(error_span,
147148
|actual| {
148149
format!("casting `{}` as `{}` is invalid",
149150
actual,
150151
cast_ty)
151152
},
152153
self.expr_ty);
153-
err.span_label(self.expr.span,
154+
err.span_label(error_span,
154155
&format!("cannot cast `{}` as `{}`",
155156
fcx.ty_to_string(self.expr_ty),
156157
cast_ty));
157158
if let Ok(snippet) = fcx.sess().codemap().span_to_snippet(self.expr.span) {
158-
err.span_label(self.expr.span,
159+
err.span_help(self.expr.span,
159160
&format!("did you mean `*{}`?", snippet));
160161
}
161162
err.emit();

src/test/compile-fail/cast-rfc0401.rs

-123
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn illegal_cast<U:?Sized,V:?Sized>(u: *const U) -> *const V
12+
{
13+
u as *const V
14+
}
15+
16+
fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
17+
{
18+
u as *const str
19+
}
20+
21+
trait Foo { fn foo(&self) {} }
22+
impl<T> Foo for T {}
23+
24+
trait Bar { fn foo(&self) {} }
25+
impl<T> Bar for T {}
26+
27+
enum E {
28+
A, B
29+
}
30+
31+
fn main()
32+
{
33+
let f: f32 = 1.2;
34+
let v = 0 as *const u8;
35+
let fat_v : *const [u8] = unsafe { &*(0 as *const [u8; 1])};
36+
let fat_sv : *const [i8] = unsafe { &*(0 as *const [i8; 1])};
37+
let foo: &Foo = &f;
38+
39+
let _ = v as &u8;
40+
let _ = v as E;
41+
let _ = v as fn();
42+
let _ = v as (u32,);
43+
let _ = Some(&v) as *const u8;
44+
45+
let _ = v as f32;
46+
let _ = main as f64;
47+
let _ = &v as usize;
48+
let _ = f as *const u8;
49+
let _ = 3_i32 as bool;
50+
let _ = E::A as bool;
51+
let _ = 0x61u32 as char;
52+
53+
let _ = false as f32;
54+
let _ = E::A as f32;
55+
let _ = 'a' as f32;
56+
57+
let _ = false as *const u8;
58+
let _ = E::A as *const u8;
59+
let _ = 'a' as *const u8;
60+
61+
let _ = 42usize as *const [u8];
62+
let _ = v as *const [u8];
63+
let _ = fat_v as *const Foo;
64+
let _ = foo as *const str;
65+
let _ = foo as *mut str;
66+
let _ = main as *mut str;
67+
let _ = &f as *mut f32;
68+
let _ = &f as *const f64;
69+
let _ = fat_sv as usize;
70+
71+
let a : *const str = "hello";
72+
let _ = a as *const Foo;
73+
74+
// check no error cascade
75+
let _ = main.f as *const u32;
76+
77+
let cf: *const Foo = &0;
78+
let _ = cf as *const [u16];
79+
let _ = cf as *const Bar;
80+
81+
vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
82+
}

0 commit comments

Comments
 (0)