Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e737135

Browse files
authoredMar 16, 2025
Unrolled build for rust-lang#138484
Rollup merge of rust-lang#138484 - xizheyin:issue-138392, r=compiler-errors Use lit span when suggesting suffix lit cast Fixes rust-lang#138392
2 parents 9f274ba + bc6047a commit e737135

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed
 

‎compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2983,7 +2983,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29832983
return false;
29842984
}
29852985

2986-
let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) else {
2986+
let span = if let hir::ExprKind::Lit(lit) = &expr.kind { lit.span } else { expr.span };
2987+
let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) else {
29872988
return false;
29882989
};
29892990

@@ -3078,10 +3079,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30783079
// Remove fractional part from literal, for example `42.0f32` into `42`
30793080
let src = src.trim_end_matches(&checked_ty.to_string());
30803081
let len = src.split('.').next().unwrap().len();
3081-
expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
3082+
span.with_lo(span.lo() + BytePos(len as u32))
30823083
} else {
30833084
let len = src.trim_end_matches(&checked_ty.to_string()).len();
3084-
expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
3085+
span.with_lo(span.lo() + BytePos(len as u32))
30853086
},
30863087
if expr.precedence() < ExprPrecedence::Unambiguous {
30873088
// Readd `)`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ run-rustfix
2+
#![allow(unused_parens)]
3+
fn main() {
4+
let _x: u8 = (4u8); //~ ERROR: mismatched types
5+
let _y: u8 = (4u8); //~ ERROR: mismatched types
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ run-rustfix
2+
#![allow(unused_parens)]
3+
fn main() {
4+
let _x: u8 = (4i32); //~ ERROR: mismatched types
5+
let _y: u8 = (4.0f32); //~ ERROR: mismatched types
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/cast_lit_suffix-issue-138392.rs:4:18
3+
|
4+
LL | let _x: u8 = (4i32);
5+
| -- ^^^^^^ expected `u8`, found `i32`
6+
| |
7+
| expected due to this
8+
|
9+
help: change the type of the numeric literal from `i32` to `u8`
10+
|
11+
LL - let _x: u8 = (4i32);
12+
LL + let _x: u8 = (4u8);
13+
|
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/cast_lit_suffix-issue-138392.rs:5:18
17+
|
18+
LL | let _y: u8 = (4.0f32);
19+
| -- ^^^^^^^^ expected `u8`, found `f32`
20+
| |
21+
| expected due to this
22+
|
23+
help: change the type of the numeric literal from `f32` to `u8`
24+
|
25+
LL - let _y: u8 = (4.0f32);
26+
LL + let _y: u8 = (4u8);
27+
|
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)
Failed to load comments.