#91939: integer to char cast error, make more targeted
This commit is contained in:
parent
a15cb49362
commit
badb81a612
@ -328,17 +328,28 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
|||||||
err.emit();
|
err.emit();
|
||||||
}
|
}
|
||||||
CastError::CastToChar => {
|
CastError::CastToChar => {
|
||||||
type_error_struct!(
|
let mut err = type_error_struct!(
|
||||||
fcx.tcx.sess,
|
fcx.tcx.sess,
|
||||||
self.span,
|
self.span,
|
||||||
self.expr_ty,
|
self.expr_ty,
|
||||||
E0604,
|
E0604,
|
||||||
"only `u8` can be cast as `char`, not `{}`",
|
"only `u8` can be cast as `char`, not `{}`",
|
||||||
self.expr_ty
|
self.expr_ty
|
||||||
)
|
);
|
||||||
.span_label(self.span, "invalid cast")
|
err.span_label(self.span, "invalid cast");
|
||||||
.span_help(self.span, "try `char::from_u32` instead")
|
if self.expr_ty.is_numeric() {
|
||||||
.emit();
|
err.span_help(
|
||||||
|
self.span,
|
||||||
|
if self.expr_ty == fcx.tcx.types.i8 {
|
||||||
|
"try casting from `u8` instead"
|
||||||
|
} else if self.expr_ty == fcx.tcx.types.u32 {
|
||||||
|
"try `char::from_u32` instead"
|
||||||
|
} else {
|
||||||
|
"try `char::from_u32` instead (via a `u32`)"
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
err.emit();
|
||||||
}
|
}
|
||||||
CastError::NonScalar => {
|
CastError::NonScalar => {
|
||||||
let mut err = type_error_struct!(
|
let mut err = type_error_struct!(
|
||||||
|
@ -17,6 +17,12 @@ error[E0604]: only `u8` can be cast as `char`, not `i8`
|
|||||||
|
|
|
|
||||||
LL | : [u32; 5i8 as char as usize]
|
LL | : [u32; 5i8 as char as usize]
|
||||||
| ^^^^^^^^^^^ invalid cast
|
| ^^^^^^^^^^^ invalid cast
|
||||||
|
|
|
||||||
|
help: try casting from `u8` instead
|
||||||
|
--> $DIR/const-eval-overflow-4b.rs:22:13
|
||||||
|
|
|
||||||
|
LL | : [u32; 5i8 as char as usize]
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
@ -3,6 +3,12 @@ error[E0604]: only `u8` can be cast as `char`, not `u32`
|
|||||||
|
|
|
|
||||||
LL | 1u32 as char;
|
LL | 1u32 as char;
|
||||||
| ^^^^^^^^^^^^ invalid cast
|
| ^^^^^^^^^^^^ invalid cast
|
||||||
|
|
|
||||||
|
help: try `char::from_u32` instead
|
||||||
|
--> $DIR/E0604.rs:2:5
|
||||||
|
|
|
||||||
|
LL | 1u32 as char;
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -58,6 +58,12 @@ error[E0604]: only `u8` can be cast as `char`, not `u32`
|
|||||||
|
|
|
|
||||||
LL | 0u32 as char;
|
LL | 0u32 as char;
|
||||||
| ^^^^^^^^^^^^ invalid cast
|
| ^^^^^^^^^^^^ invalid cast
|
||||||
|
|
|
||||||
|
help: try `char::from_u32` instead
|
||||||
|
--> $DIR/error-festival.rs:25:5
|
||||||
|
|
|
||||||
|
LL | 0u32 as char;
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
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
|
||||||
|
@ -99,6 +99,12 @@ error[E0604]: only `u8` can be cast as `char`, not `u32`
|
|||||||
|
|
|
|
||||||
LL | let _ = 0x61u32 as char;
|
LL | let _ = 0x61u32 as char;
|
||||||
| ^^^^^^^^^^^^^^^ invalid cast
|
| ^^^^^^^^^^^^^^^ invalid cast
|
||||||
|
|
|
||||||
|
help: try `char::from_u32` instead
|
||||||
|
--> $DIR/cast-rfc0401.rs:41:13
|
||||||
|
|
|
||||||
|
LL | let _ = 0x61u32 as char;
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
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…
x
Reference in New Issue
Block a user