make CastError::NeedsDeref create a MachineApplicable suggestion + other misc fixes

This commit is contained in:
Ezra Shaw 2023-01-16 20:24:01 +13:00
parent 41edaac716
commit ca1178f022
No known key found for this signature in database
GPG Key ID: 17CD5C2ADAE0D344
4 changed files with 29 additions and 31 deletions

View File

@ -151,7 +151,7 @@ fn pointer_kind(
#[derive(Copy, Clone)]
pub enum CastError {
ErrorGuaranteed,
ErrorGuaranteed(ErrorGuaranteed),
CastToBool,
CastToChar,
@ -176,8 +176,8 @@ pub enum CastError {
}
impl From<ErrorGuaranteed> for CastError {
fn from(_: ErrorGuaranteed) -> Self {
CastError::ErrorGuaranteed
fn from(err: ErrorGuaranteed) -> Self {
CastError::ErrorGuaranteed(err)
}
}
@ -225,11 +225,10 @@ pub fn new(
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError) {
match e {
CastError::ErrorGuaranteed => {
CastError::ErrorGuaranteed(_) => {
// an error has already been reported
}
CastError::NeedDeref => {
let error_span = self.span;
let mut err = make_invalid_casting_error(
fcx.tcx.sess,
self.span,
@ -237,21 +236,14 @@ fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError) {
self.cast_ty,
fcx,
);
let cast_ty = fcx.ty_to_string(self.cast_ty);
err.span_label(
error_span,
format!("cannot cast `{}` as `{}`", fcx.ty_to_string(self.expr_ty), cast_ty),
err.span_suggestion_verbose(
self.expr_span.shrink_to_lo(),
"dereference the expression",
"*",
Applicability::MachineApplicable,
);
if let Ok(snippet) = fcx.sess().source_map().span_to_snippet(self.expr_span) {
err.span_suggestion(
self.expr_span,
"dereference the expression",
format!("*{}", snippet),
Applicability::MaybeIncorrect,
);
} else {
err.span_help(self.expr_span, "dereference the expression with `*`");
}
err.emit();
}
CastError::NeedViaThinPtr | CastError::NeedViaPtr => {

View File

@ -2,10 +2,12 @@ error[E0606]: casting `&u8` as `u8` is invalid
--> $DIR/E0606.rs:2:5
|
LL | &0u8 as u8;
| ----^^^^^^
| |
| cannot cast `&u8` as `u8`
| help: dereference the expression: `*&0u8`
| ^^^^^^^^^^
|
help: dereference the expression
|
LL | *&0u8 as u8;
| +
error: aborting due to previous error

View File

@ -69,10 +69,12 @@ error[E0606]: casting `&u8` as `u32` is invalid
--> $DIR/error-festival.rs:37:18
|
LL | let y: u32 = x as u32;
| -^^^^^^^
| |
| cannot cast `&u8` as `u32`
| help: dereference the expression: `*x`
| ^^^^^^^^
|
help: dereference the expression
|
LL | let y: u32 = *x as u32;
| +
error[E0607]: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
--> $DIR/error-festival.rs:41:5

View File

@ -243,10 +243,12 @@ error[E0606]: casting `&{float}` as `f32` is invalid
--> $DIR/cast-rfc0401.rs:71:30
|
LL | vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
| -^^^^^^^
| |
| cannot cast `&{float}` as `f32`
| help: dereference the expression: `*s`
| ^^^^^^^^
|
help: dereference the expression
|
LL | vec![0.0].iter().map(|s| *s as f32).collect::<Vec<f32>>();
| +
error: aborting due to 34 previous errors