Rollup merge of #97169 - gimbles:u32-diagnostic, r=petrochenkov
Improve `u32 as char` cast diagnostic Fixes #97160
This commit is contained in:
commit
3913d99c40
4 changed files with 25 additions and 28 deletions
|
@ -347,16 +347,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||||
);
|
);
|
||||||
err.span_label(self.span, "invalid cast");
|
err.span_label(self.span, "invalid cast");
|
||||||
if self.expr_ty.is_numeric() {
|
if self.expr_ty.is_numeric() {
|
||||||
err.span_help(
|
if self.expr_ty == fcx.tcx.types.u32 {
|
||||||
self.span,
|
match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
|
||||||
if self.expr_ty == fcx.tcx.types.i8 {
|
Ok(snippet) => err.span_suggestion(
|
||||||
"try casting from `u8` instead"
|
self.span,
|
||||||
} else if self.expr_ty == fcx.tcx.types.u32 {
|
"try `char::from_u32` instead",
|
||||||
"try `char::from_u32` instead"
|
format!("char::from_u32({snippet})"),
|
||||||
} else {
|
Applicability::MachineApplicable,
|
||||||
"try `char::from_u32` instead (via a `u32`)"
|
),
|
||||||
},
|
|
||||||
);
|
Err(_) => err.span_help(self.span, "try `char::from_u32` instead"),
|
||||||
|
};
|
||||||
|
} else if self.expr_ty == fcx.tcx.types.i8 {
|
||||||
|
err.span_help(self.span, "try casting from `u8` instead");
|
||||||
|
} else {
|
||||||
|
err.span_help(self.span, "try `char::from_u32` instead (via a `u32`)");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
err.emit();
|
err.emit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
||||||
--> $DIR/E0604.rs:2:5
|
--> $DIR/E0604.rs:2:5
|
||||||
|
|
|
|
||||||
LL | 1u32 as char;
|
|
||||||
| ^^^^^^^^^^^^ invalid cast
|
|
||||||
|
|
|
||||||
help: try `char::from_u32` instead
|
|
||||||
--> $DIR/E0604.rs:2:5
|
|
||||||
|
|
|
||||||
LL | 1u32 as char;
|
LL | 1u32 as char;
|
||||||
| ^^^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
| |
|
||||||
|
| invalid cast
|
||||||
|
| help: try `char::from_u32` instead: `char::from_u32(1u32)`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -56,14 +56,11 @@ LL | | }
|
||||||
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
||||||
--> $DIR/error-festival.rs:25:5
|
--> $DIR/error-festival.rs:25:5
|
||||||
|
|
|
|
||||||
LL | 0u32 as char;
|
|
||||||
| ^^^^^^^^^^^^ invalid cast
|
|
||||||
|
|
|
||||||
help: try `char::from_u32` instead
|
|
||||||
--> $DIR/error-festival.rs:25:5
|
|
||||||
|
|
|
||||||
LL | 0u32 as char;
|
LL | 0u32 as char;
|
||||||
| ^^^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
| |
|
||||||
|
| invalid cast
|
||||||
|
| help: try `char::from_u32` instead: `char::from_u32(0u32)`
|
||||||
|
|
||||||
error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
|
error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
|
||||||
--> $DIR/error-festival.rs:29:5
|
--> $DIR/error-festival.rs:29:5
|
||||||
|
|
|
@ -97,14 +97,11 @@ LL | let _ = E::A as bool;
|
||||||
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
||||||
--> $DIR/cast-rfc0401.rs:41:13
|
--> $DIR/cast-rfc0401.rs:41:13
|
||||||
|
|
|
|
||||||
LL | let _ = 0x61u32 as char;
|
|
||||||
| ^^^^^^^^^^^^^^^ invalid cast
|
|
||||||
|
|
|
||||||
help: try `char::from_u32` instead
|
|
||||||
--> $DIR/cast-rfc0401.rs:41:13
|
|
||||||
|
|
|
||||||
LL | let _ = 0x61u32 as char;
|
LL | let _ = 0x61u32 as char;
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^
|
||||||
|
| |
|
||||||
|
| invalid cast
|
||||||
|
| help: try `char::from_u32` instead: `char::from_u32(0x61u32)`
|
||||||
|
|
||||||
error[E0606]: casting `bool` as `f32` is invalid
|
error[E0606]: casting `bool` as `f32` is invalid
|
||||||
--> $DIR/cast-rfc0401.rs:43:13
|
--> $DIR/cast-rfc0401.rs:43:13
|
||||||
|
|
Loading…
Add table
Reference in a new issue