File tree Expand file tree Collapse file tree 4 files changed +25
-28
lines changed
compiler/rustc_typeck/src/check Expand file tree Collapse file tree 4 files changed +25
-28
lines changed Original file line number Diff line number Diff line change @@ -347,16 +347,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
347347 ) ;
348348 err. span_label ( self . span , "invalid cast" ) ;
349349 if self . expr_ty . is_numeric ( ) {
350- err. span_help (
351- self . span ,
352- if self . expr_ty == fcx. tcx . types . i8 {
353- "try casting from `u8` instead"
354- } else if self . expr_ty == fcx. tcx . types . u32 {
355- "try `char::from_u32` instead"
356- } else {
357- "try `char::from_u32` instead (via a `u32`)"
358- } ,
359- ) ;
350+ if self . expr_ty == fcx. tcx . types . u32 {
351+ match fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . expr . span ) {
352+ Ok ( snippet) => err. span_suggestion (
353+ self . span ,
354+ "try `char::from_u32` instead" ,
355+ format ! ( "char::from_u32({snippet})" ) ,
356+ Applicability :: MachineApplicable ,
357+ ) ,
358+
359+ Err ( _) => err. span_help ( self . span , "try `char::from_u32` instead" ) ,
360+ } ;
361+ } else if self . expr_ty == fcx. tcx . types . i8 {
362+ err. span_help ( self . span , "try casting from `u8` instead" ) ;
363+ } else {
364+ err. span_help ( self . span , "try `char::from_u32` instead (via a `u32`)" ) ;
365+ } ;
360366 }
361367 err. emit ( ) ;
362368 }
Original file line number Diff line number Diff line change 11error[E0604]: only `u8` can be cast as `char`, not `u32`
22 --> $DIR/E0604.rs:2:5
33 |
4- LL | 1u32 as char;
5- | ^^^^^^^^^^^^ invalid cast
6- |
7- help: try `char::from_u32` instead
8- --> $DIR/E0604.rs:2:5
9- |
104LL | 1u32 as char;
115 | ^^^^^^^^^^^^
6+ | |
7+ | invalid cast
8+ | help: try `char::from_u32` instead: `char::from_u32(1u32)`
129
1310error: aborting due to previous error
1411
Original file line number Diff line number Diff line change @@ -56,14 +56,11 @@ LL | | }
5656error[E0604]: only `u8` can be cast as `char`, not `u32`
5757 --> $DIR/error-festival.rs:25:5
5858 |
59- LL | 0u32 as char;
60- | ^^^^^^^^^^^^ invalid cast
61- |
62- help: try `char::from_u32` instead
63- --> $DIR/error-festival.rs:25:5
64- |
6559LL | 0u32 as char;
6660 | ^^^^^^^^^^^^
61+ | |
62+ | invalid cast
63+ | help: try `char::from_u32` instead: `char::from_u32(0u32)`
6764
6865error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
6966 --> $DIR/error-festival.rs:29:5
Original file line number Diff line number Diff line change @@ -97,14 +97,11 @@ LL | let _ = E::A as bool;
9797error[E0604]: only `u8` can be cast as `char`, not `u32`
9898 --> $DIR/cast-rfc0401.rs:41:13
9999 |
100- LL | let _ = 0x61u32 as char;
101- | ^^^^^^^^^^^^^^^ invalid cast
102- |
103- help: try `char::from_u32` instead
104- --> $DIR/cast-rfc0401.rs:41:13
105- |
106100LL | let _ = 0x61u32 as char;
107101 | ^^^^^^^^^^^^^^^
102+ | |
103+ | invalid cast
104+ | help: try `char::from_u32` instead: `char::from_u32(0x61u32)`
108105
109106error[E0606]: casting `bool` as `f32` is invalid
110107 --> $DIR/cast-rfc0401.rs:43:13
You can’t perform that action at this time.
0 commit comments