about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs7
-rw-r--r--tests/ui/cast/cast_lit_suffix-issue-138392.fixed6
-rw-r--r--tests/ui/cast/cast_lit_suffix-issue-138392.rs6
-rw-r--r--tests/ui/cast/cast_lit_suffix-issue-138392.stderr31
4 files changed, 47 insertions, 3 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index 37aaaed5477..f19e36206a7 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -2983,7 +2983,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             return false;
         }
 
-        let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) else {
+        let span = if let hir::ExprKind::Lit(lit) = &expr.kind { lit.span } else { expr.span };
+        let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) else {
             return false;
         };
 
@@ -3078,10 +3079,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 // Remove fractional part from literal, for example `42.0f32` into `42`
                 let src = src.trim_end_matches(&checked_ty.to_string());
                 let len = src.split('.').next().unwrap().len();
-                expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
+                span.with_lo(span.lo() + BytePos(len as u32))
             } else {
                 let len = src.trim_end_matches(&checked_ty.to_string()).len();
-                expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
+                span.with_lo(span.lo() + BytePos(len as u32))
             },
             if expr.precedence() < ExprPrecedence::Unambiguous {
                 // Readd `)`
diff --git a/tests/ui/cast/cast_lit_suffix-issue-138392.fixed b/tests/ui/cast/cast_lit_suffix-issue-138392.fixed
new file mode 100644
index 00000000000..c6fbd09f89c
--- /dev/null
+++ b/tests/ui/cast/cast_lit_suffix-issue-138392.fixed
@@ -0,0 +1,6 @@
+//@ run-rustfix
+#![allow(unused_parens)]
+fn main() {
+    let _x: u8 = (4u8); //~ ERROR: mismatched types
+    let _y: u8 = (4u8); //~ ERROR: mismatched types
+}
diff --git a/tests/ui/cast/cast_lit_suffix-issue-138392.rs b/tests/ui/cast/cast_lit_suffix-issue-138392.rs
new file mode 100644
index 00000000000..86dbbbbf126
--- /dev/null
+++ b/tests/ui/cast/cast_lit_suffix-issue-138392.rs
@@ -0,0 +1,6 @@
+//@ run-rustfix
+#![allow(unused_parens)]
+fn main() {
+    let _x: u8 = (4i32); //~ ERROR: mismatched types
+    let _y: u8 = (4.0f32); //~ ERROR: mismatched types
+}
diff --git a/tests/ui/cast/cast_lit_suffix-issue-138392.stderr b/tests/ui/cast/cast_lit_suffix-issue-138392.stderr
new file mode 100644
index 00000000000..998fcfc36dc
--- /dev/null
+++ b/tests/ui/cast/cast_lit_suffix-issue-138392.stderr
@@ -0,0 +1,31 @@
+error[E0308]: mismatched types
+  --> $DIR/cast_lit_suffix-issue-138392.rs:4:18
+   |
+LL |     let _x: u8 = (4i32);
+   |             --   ^^^^^^ expected `u8`, found `i32`
+   |             |
+   |             expected due to this
+   |
+help: change the type of the numeric literal from `i32` to `u8`
+   |
+LL -     let _x: u8 = (4i32);
+LL +     let _x: u8 = (4u8);
+   |
+
+error[E0308]: mismatched types
+  --> $DIR/cast_lit_suffix-issue-138392.rs:5:18
+   |
+LL |     let _y: u8 = (4.0f32);
+   |             --   ^^^^^^^^ expected `u8`, found `f32`
+   |             |
+   |             expected due to this
+   |
+help: change the type of the numeric literal from `f32` to `u8`
+   |
+LL -     let _y: u8 = (4.0f32);
+LL +     let _y: u8 = (4u8);
+   |
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.