Skip to content

Commit 3913d99

Browse files
authored
Rollup merge of #97169 - gimbles:u32-diagnostic, r=petrochenkov
Improve `u32 as char` cast diagnostic Fixes #97160
2 parents 12644bc + 9e5c24e commit 3913d99

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

compiler/rustc_typeck/src/check/cast.rs

+16-10
Original file line numberDiff line numberDiff 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
}

src/test/ui/error-codes/E0604.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
error[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-
|
104
LL | 1u32 as char;
115
| ^^^^^^^^^^^^
6+
| |
7+
| invalid cast
8+
| help: try `char::from_u32` instead: `char::from_u32(1u32)`
129

1310
error: aborting due to previous error
1411

src/test/ui/error-festival.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,11 @@ LL | | }
5656
error[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-
|
6559
LL | 0u32 as char;
6660
| ^^^^^^^^^^^^
61+
| |
62+
| invalid cast
63+
| help: try `char::from_u32` instead: `char::from_u32(0u32)`
6764

6865
error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
6966
--> $DIR/error-festival.rs:29:5

src/test/ui/mismatched_types/cast-rfc0401.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,11 @@ LL | let _ = E::A as bool;
9797
error[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-
|
106100
LL | let _ = 0x61u32 as char;
107101
| ^^^^^^^^^^^^^^^
102+
| |
103+
| invalid cast
104+
| help: try `char::from_u32` instead: `char::from_u32(0x61u32)`
108105

109106
error[E0606]: casting `bool` as `f32` is invalid
110107
--> $DIR/cast-rfc0401.rs:43:13

0 commit comments

Comments
 (0)