Skip to content

Commit cf5c764

Browse files
authored
Rollup merge of rust-lang#100609 - chenyukang:fix-100527, r=davidtwco
Extend invalid floating point literal suffix suggestion Fixes rust-lang#100527
2 parents 070de4b + 89a51a1 commit cf5c764

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21932193
E0610,
21942194
"`{expr_t}` is a primitive type and therefore doesn't have fields",
21952195
);
2196-
let is_valid_suffix = |field: String| {
2196+
let is_valid_suffix = |field: &str| {
21972197
if field == "f32" || field == "f64" {
21982198
return true;
21992199
}
@@ -2218,20 +2218,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22182218
let suffix = chars.collect::<String>();
22192219
suffix.is_empty() || suffix == "f32" || suffix == "f64"
22202220
};
2221+
let maybe_partial_suffix = |field: &str| -> Option<&str> {
2222+
let first_chars = ['f', 'l'];
2223+
if field.len() >= 1
2224+
&& field.to_lowercase().starts_with(first_chars)
2225+
&& field[1..].chars().all(|c| c.is_ascii_digit())
2226+
{
2227+
if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
2228+
} else {
2229+
None
2230+
}
2231+
};
22212232
if let ty::Infer(ty::IntVar(_)) = expr_t.kind()
22222233
&& let ExprKind::Lit(Spanned {
22232234
node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
22242235
..
22252236
}) = base.kind
22262237
&& !base.span.from_expansion()
2227-
&& is_valid_suffix(field_name)
22282238
{
2229-
err.span_suggestion_verbose(
2230-
field.span.shrink_to_lo(),
2231-
"If the number is meant to be a floating point number, consider adding a `0` after the period",
2232-
'0',
2233-
Applicability::MaybeIncorrect,
2234-
);
2239+
if is_valid_suffix(&field_name) {
2240+
err.span_suggestion_verbose(
2241+
field.span.shrink_to_lo(),
2242+
"if intended to be a floating point literal, consider adding a `0` after the period",
2243+
'0',
2244+
Applicability::MaybeIncorrect,
2245+
);
2246+
} else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
2247+
err.span_suggestion_verbose(
2248+
field.span,
2249+
format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
2250+
format!("0{correct_suffix}"),
2251+
Applicability::MaybeIncorrect,
2252+
);
2253+
}
22352254
}
22362255
err.emit();
22372256
}

src/test/ui/attempted-access-non-fatal.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ fn main() {
33
let x = 0;
44
let _ = x.foo; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
55
let _ = x.bar; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
6+
let _ = 0.f; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
7+
let _ = 2.l; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
8+
let _ = 12.F; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
9+
let _ = 34.L; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
610
}

src/test/ui/attempted-access-non-fatal.stderr

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,50 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
1010
LL | let _ = x.bar;
1111
| ^^^
1212

13-
error: aborting due to 2 previous errors
13+
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
14+
--> $DIR/attempted-access-non-fatal.rs:6:15
15+
|
16+
LL | let _ = 0.f;
17+
| ^
18+
|
19+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f32` suffix
20+
|
21+
LL | let _ = 0.0f32;
22+
| ~~~~
23+
24+
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
25+
--> $DIR/attempted-access-non-fatal.rs:7:15
26+
|
27+
LL | let _ = 2.l;
28+
| ^
29+
|
30+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
31+
|
32+
LL | let _ = 2.0f64;
33+
| ~~~~
34+
35+
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
36+
--> $DIR/attempted-access-non-fatal.rs:8:16
37+
|
38+
LL | let _ = 12.F;
39+
| ^
40+
|
41+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f32` suffix
42+
|
43+
LL | let _ = 12.0f32;
44+
| ~~~~
45+
46+
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
47+
--> $DIR/attempted-access-non-fatal.rs:9:16
48+
|
49+
LL | let _ = 34.L;
50+
| ^
51+
|
52+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
53+
|
54+
LL | let _ = 34.0f64;
55+
| ~~~~
56+
57+
error: aborting due to 6 previous errors
1458

1559
For more information about this error, try `rustc --explain E0610`.

src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
44
LL | 2.e1;
55
| ^^
66
|
7-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
7+
help: if intended to be a floating point literal, consider adding a `0` after the period
88
|
99
LL | 2.0e1;
1010
| +
@@ -15,7 +15,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
1515
LL | 2.E1;
1616
| ^^
1717
|
18-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
18+
help: if intended to be a floating point literal, consider adding a `0` after the period
1919
|
2020
LL | 2.0E1;
2121
| +
@@ -26,7 +26,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
2626
LL | 2.f32;
2727
| ^^^
2828
|
29-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
29+
help: if intended to be a floating point literal, consider adding a `0` after the period
3030
|
3131
LL | 2.0f32;
3232
| +
@@ -37,7 +37,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
3737
LL | 2.f64;
3838
| ^^^
3939
|
40-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
40+
help: if intended to be a floating point literal, consider adding a `0` after the period
4141
|
4242
LL | 2.0f64;
4343
| +
@@ -48,7 +48,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
4848
LL | 2.e+12;
4949
| ^
5050
|
51-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
51+
help: if intended to be a floating point literal, consider adding a `0` after the period
5252
|
5353
LL | 2.0e+12;
5454
| +
@@ -59,7 +59,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
5959
LL | 2.e-12;
6060
| ^
6161
|
62-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
62+
help: if intended to be a floating point literal, consider adding a `0` after the period
6363
|
6464
LL | 2.0e-12;
6565
| +
@@ -70,7 +70,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
7070
LL | 2.e1f32;
7171
| ^^^^^
7272
|
73-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
73+
help: if intended to be a floating point literal, consider adding a `0` after the period
7474
|
7575
LL | 2.0e1f32;
7676
| +

0 commit comments

Comments
 (0)