File tree 4 files changed +25
-28
lines changed
compiler/rustc_typeck/src/check
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> {
347
347
) ;
348
348
err. span_label ( self . span , "invalid cast" ) ;
349
349
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
+ } ;
360
366
}
361
367
err. emit ( ) ;
362
368
}
Original file line number Diff line number Diff line change 1
1
error[E0604]: only `u8` can be cast as `char`, not `u32`
2
2
--> $DIR/E0604.rs:2:5
3
3
|
4
- LL | 1u32 as char;
5
- | ^^^^^^^^^^^^ invalid cast
6
- |
7
- help: try `char::from_u32` instead
8
- --> $DIR/E0604.rs:2:5
9
- |
10
4
LL | 1u32 as char;
11
5
| ^^^^^^^^^^^^
6
+ | |
7
+ | invalid cast
8
+ | help: try `char::from_u32` instead: `char::from_u32(1u32)`
12
9
13
10
error: aborting due to previous error
14
11
Original file line number Diff line number Diff line change @@ -56,14 +56,11 @@ LL | | }
56
56
error[E0604]: only `u8` can be cast as `char`, not `u32`
57
57
--> $DIR/error-festival.rs:25:5
58
58
|
59
- LL | 0u32 as char;
60
- | ^^^^^^^^^^^^ invalid cast
61
- |
62
- help: try `char::from_u32` instead
63
- --> $DIR/error-festival.rs:25:5
64
- |
65
59
LL | 0u32 as char;
66
60
| ^^^^^^^^^^^^
61
+ | |
62
+ | invalid cast
63
+ | help: try `char::from_u32` instead: `char::from_u32(0u32)`
67
64
68
65
error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
69
66
--> $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;
97
97
error[E0604]: only `u8` can be cast as `char`, not `u32`
98
98
--> $DIR/cast-rfc0401.rs:41:13
99
99
|
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
- |
106
100
LL | let _ = 0x61u32 as char;
107
101
| ^^^^^^^^^^^^^^^
102
+ | |
103
+ | invalid cast
104
+ | help: try `char::from_u32` instead: `char::from_u32(0x61u32)`
108
105
109
106
error[E0606]: casting `bool` as `f32` is invalid
110
107
--> $DIR/cast-rfc0401.rs:43:13
You can’t perform that action at this time.
0 commit comments