diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-05-19 17:22:50 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-19 17:22:50 +0200 |
| commit | 3913d99c40e7dabee227129603feed8c0e1542fb (patch) | |
| tree | 90fa2f1bb816cbddbf973e4c1248a71e976e3d08 | |
| parent | 12644bc39d6a1aae74e7a1d51a9b650cacfa4616 (diff) | |
| parent | 9e5c24eaf886a314312b4e5047ffd30addb0fe90 (diff) | |
| download | rust-3913d99c40e7dabee227129603feed8c0e1542fb.tar.gz rust-3913d99c40e7dabee227129603feed8c0e1542fb.zip | |
Rollup merge of #97169 - gimbles:u32-diagnostic, r=petrochenkov
Improve `u32 as char` cast diagnostic Fixes #97160
| -rw-r--r-- | compiler/rustc_typeck/src/check/cast.rs | 26 | ||||
| -rw-r--r-- | src/test/ui/error-codes/E0604.stderr | 9 | ||||
| -rw-r--r-- | src/test/ui/error-festival.stderr | 9 | ||||
| -rw-r--r-- | src/test/ui/mismatched_types/cast-rfc0401.stderr | 9 |
4 files changed, 25 insertions, 28 deletions
diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index a153997599a..d9aaf730efc 100644 --- a/compiler/rustc_typeck/src/check/cast.rs +++ b/compiler/rustc_typeck/src/check/cast.rs @@ -347,16 +347,22 @@ impl<'a, 'tcx> CastCheck<'tcx> { ); err.span_label(self.span, "invalid cast"); if self.expr_ty.is_numeric() { - 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`)" - }, - ); + if self.expr_ty == fcx.tcx.types.u32 { + match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) { + Ok(snippet) => err.span_suggestion( + self.span, + "try `char::from_u32` instead", + format!("char::from_u32({snippet})"), + Applicability::MachineApplicable, + ), + + 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(); } diff --git a/src/test/ui/error-codes/E0604.stderr b/src/test/ui/error-codes/E0604.stderr index d715d28b73c..68da03928b7 100644 --- a/src/test/ui/error-codes/E0604.stderr +++ b/src/test/ui/error-codes/E0604.stderr @@ -2,13 +2,10 @@ error[E0604]: only `u8` can be cast as `char`, not `u32` --> $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; | ^^^^^^^^^^^^ + | | + | invalid cast + | help: try `char::from_u32` instead: `char::from_u32(1u32)` error: aborting due to previous error diff --git a/src/test/ui/error-festival.stderr b/src/test/ui/error-festival.stderr index 0ddb6fc99b0..81aa268cacc 100644 --- a/src/test/ui/error-festival.stderr +++ b/src/test/ui/error-festival.stderr @@ -57,13 +57,10 @@ error[E0604]: only `u8` can be cast as `char`, not `u32` --> $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; | ^^^^^^^^^^^^ + | | + | invalid cast + | help: try `char::from_u32` instead: `char::from_u32(0u32)` error[E0605]: non-primitive cast: `u8` as `Vec<u8>` --> $DIR/error-festival.rs:29:5 diff --git a/src/test/ui/mismatched_types/cast-rfc0401.stderr b/src/test/ui/mismatched_types/cast-rfc0401.stderr index 84220ea172a..3a508459cc0 100644 --- a/src/test/ui/mismatched_types/cast-rfc0401.stderr +++ b/src/test/ui/mismatched_types/cast-rfc0401.stderr @@ -98,13 +98,10 @@ error[E0604]: only `u8` can be cast as `char`, not `u32` --> $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; | ^^^^^^^^^^^^^^^ + | | + | invalid cast + | help: try `char::from_u32` instead: `char::from_u32(0x61u32)` error[E0606]: casting `bool` as `f32` is invalid --> $DIR/cast-rfc0401.rs:43:13 |
