Skip to content

Commit 3cf22de

Browse files
Suggest rewriting a malformed hex literal if we expect a float
1 parent b38a6d3 commit 3cf22de

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::ty::{
1919
TypeVisitable,
2020
};
2121
use rustc_session::errors::ExprParenthesesNeeded;
22+
use rustc_span::source_map::Spanned;
2223
use rustc_span::symbol::{sym, Ident};
2324
use rustc_span::{Span, Symbol};
2425
use rustc_trait_selection::infer::InferCtxtExt;
@@ -1259,6 +1260,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12591260
);
12601261
true
12611262
}
1263+
ExprKind::Lit(Spanned {
1264+
node: rustc_ast::LitKind::Int(lit, rustc_ast::LitIntType::Unsuffixed),
1265+
span,
1266+
}) => {
1267+
let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) else { return false; };
1268+
if !(snippet.starts_with("0x") || snippet.starts_with("0X")) {
1269+
return false;
1270+
}
1271+
if snippet.len() <= 5 || !snippet.is_char_boundary(snippet.len() - 3) {
1272+
return false;
1273+
}
1274+
let (_, suffix) = snippet.split_at(snippet.len() - 3);
1275+
let value = match suffix {
1276+
"f32" => (lit - 0xf32) / (16 * 16 * 16),
1277+
"f64" => (lit - 0xf64) / (16 * 16 * 16),
1278+
_ => return false,
1279+
};
1280+
err.span_suggestions(
1281+
expr.span,
1282+
"rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float",
1283+
[format!("0x{value:X} as {suffix}"), format!("{value}_{suffix}")],
1284+
Applicability::MaybeIncorrect,
1285+
);
1286+
true
1287+
}
12621288
_ => false,
12631289
}
12641290
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
let _f: f32 = 0xAAf32;
3+
//~^ ERROR mismatched types
4+
//~| HELP rewrite this
5+
6+
let _f: f32 = 0xAB_f32;
7+
//~^ ERROR mismatched types
8+
//~| HELP rewrite this
9+
10+
let _f: f64 = 0xFF_f64;
11+
//~^ ERROR mismatched types
12+
//~| HELP rewrite this
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/bad-hex-float-lit.rs:2:19
3+
|
4+
LL | let _f: f32 = 0xAAf32;
5+
| --- ^^^^^^^ expected `f32`, found integer
6+
| |
7+
| expected due to this
8+
|
9+
help: rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float
10+
|
11+
LL | let _f: f32 = 0xAA as f32;
12+
| ~~~~~~~~~~~
13+
LL | let _f: f32 = 170_f32;
14+
| ~~~~~~~
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/bad-hex-float-lit.rs:6:19
18+
|
19+
LL | let _f: f32 = 0xAB_f32;
20+
| --- ^^^^^^^^ expected `f32`, found integer
21+
| |
22+
| expected due to this
23+
|
24+
help: rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float
25+
|
26+
LL | let _f: f32 = 0xAB as f32;
27+
| ~~~~~~~~~~~
28+
LL | let _f: f32 = 171_f32;
29+
| ~~~~~~~
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/bad-hex-float-lit.rs:10:19
33+
|
34+
LL | let _f: f64 = 0xFF_f64;
35+
| --- ^^^^^^^^ expected `f64`, found integer
36+
| |
37+
| expected due to this
38+
|
39+
help: rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float
40+
|
41+
LL | let _f: f64 = 0xFF as f64;
42+
| ~~~~~~~~~~~
43+
LL | let _f: f64 = 255_f64;
44+
| ~~~~~~~
45+
46+
error: aborting due to 3 previous errors
47+
48+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)