Skip to content

Commit ca1178f

Browse files
committed
make CastError::NeedsDeref create a MachineApplicable suggestion + other misc fixes
1 parent 41edaac commit ca1178f

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
151151

152152
#[derive(Copy, Clone)]
153153
pub enum CastError {
154-
ErrorGuaranteed,
154+
ErrorGuaranteed(ErrorGuaranteed),
155155

156156
CastToBool,
157157
CastToChar,
@@ -176,8 +176,8 @@ pub enum CastError {
176176
}
177177

178178
impl From<ErrorGuaranteed> for CastError {
179-
fn from(_: ErrorGuaranteed) -> Self {
180-
CastError::ErrorGuaranteed
179+
fn from(err: ErrorGuaranteed) -> Self {
180+
CastError::ErrorGuaranteed(err)
181181
}
182182
}
183183

@@ -225,33 +225,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
225225

226226
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError) {
227227
match e {
228-
CastError::ErrorGuaranteed => {
228+
CastError::ErrorGuaranteed(_) => {
229229
// an error has already been reported
230230
}
231231
CastError::NeedDeref => {
232-
let error_span = self.span;
233232
let mut err = make_invalid_casting_error(
234233
fcx.tcx.sess,
235234
self.span,
236235
self.expr_ty,
237236
self.cast_ty,
238237
fcx,
239238
);
240-
let cast_ty = fcx.ty_to_string(self.cast_ty);
241-
err.span_label(
242-
error_span,
243-
format!("cannot cast `{}` as `{}`", fcx.ty_to_string(self.expr_ty), cast_ty),
239+
240+
err.span_suggestion_verbose(
241+
self.expr_span.shrink_to_lo(),
242+
"dereference the expression",
243+
"*",
244+
Applicability::MachineApplicable,
244245
);
245-
if let Ok(snippet) = fcx.sess().source_map().span_to_snippet(self.expr_span) {
246-
err.span_suggestion(
247-
self.expr_span,
248-
"dereference the expression",
249-
format!("*{}", snippet),
250-
Applicability::MaybeIncorrect,
251-
);
252-
} else {
253-
err.span_help(self.expr_span, "dereference the expression with `*`");
254-
}
246+
255247
err.emit();
256248
}
257249
CastError::NeedViaThinPtr | CastError::NeedViaPtr => {

tests/ui/error-codes/E0606.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0606]: casting `&u8` as `u8` is invalid
22
--> $DIR/E0606.rs:2:5
33
|
44
LL | &0u8 as u8;
5-
| ----^^^^^^
6-
| |
7-
| cannot cast `&u8` as `u8`
8-
| help: dereference the expression: `*&0u8`
5+
| ^^^^^^^^^^
6+
|
7+
help: dereference the expression
8+
|
9+
LL | *&0u8 as u8;
10+
| +
911

1012
error: aborting due to previous error
1113

tests/ui/error-festival.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ error[E0606]: casting `&u8` as `u32` is invalid
6969
--> $DIR/error-festival.rs:37:18
7070
|
7171
LL | let y: u32 = x as u32;
72-
| -^^^^^^^
73-
| |
74-
| cannot cast `&u8` as `u32`
75-
| help: dereference the expression: `*x`
72+
| ^^^^^^^^
73+
|
74+
help: dereference the expression
75+
|
76+
LL | let y: u32 = *x as u32;
77+
| +
7678

7779
error[E0607]: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
7880
--> $DIR/error-festival.rs:41:5

tests/ui/mismatched_types/cast-rfc0401.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,12 @@ error[E0606]: casting `&{float}` as `f32` is invalid
243243
--> $DIR/cast-rfc0401.rs:71:30
244244
|
245245
LL | vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
246-
| -^^^^^^^
247-
| |
248-
| cannot cast `&{float}` as `f32`
249-
| help: dereference the expression: `*s`
246+
| ^^^^^^^^
247+
|
248+
help: dereference the expression
249+
|
250+
LL | vec![0.0].iter().map(|s| *s as f32).collect::<Vec<f32>>();
251+
| +
250252

251253
error: aborting due to 34 previous errors
252254

0 commit comments

Comments
 (0)