Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lit span when suggesting suffix lit cast #138484

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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 `)`
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/cast/cast_lit_suffix-issue-138392.fixed
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions tests/ui/cast/cast_lit_suffix-issue-138392.rs
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions tests/ui/cast/cast_lit_suffix-issue-138392.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading