Skip to content

Commit cd01b13

Browse files
Rollup merge of rust-lang#121724 - nnethercote:LitKind-Err-for-floats, r=fmease
Use `LitKind::Err` for malformed floats rust-lang#121120 changed `StringReader::cook_lexer_literal` to return `LitKind::Err` for malformed integer literals. This commit does the same for float literals, for consistency. r? `@fmease`
2 parents 9704475 + 840c8d3 commit cd01b13

File tree

7 files changed

+176
-166
lines changed

7 files changed

+176
-166
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,11 @@ impl<'sess, 'src> StringReader<'sess, 'src> {
501501
(kind, self.symbol_from_to(start, end))
502502
}
503503
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
504+
let mut kind = token::Float;
504505
if empty_exponent {
505506
let span = self.mk_sp(start, self.pos);
506-
self.dcx().emit_err(errors::EmptyExponentFloat { span });
507+
let guar = self.dcx().emit_err(errors::EmptyExponentFloat { span });
508+
kind = token::Err(guar);
507509
}
508510
let base = match base {
509511
Base::Hexadecimal => Some("hexadecimal"),
@@ -513,9 +515,11 @@ impl<'sess, 'src> StringReader<'sess, 'src> {
513515
};
514516
if let Some(base) = base {
515517
let span = self.mk_sp(start, end);
516-
self.dcx().emit_err(errors::FloatLiteralUnsupportedBase { span, base });
518+
let guar =
519+
self.dcx().emit_err(errors::FloatLiteralUnsupportedBase { span, base });
520+
kind = token::Err(guar)
517521
}
518-
(token::Float, self.symbol_from_to(start, end))
522+
(kind, self.symbol_from_to(start, end))
519523
}
520524
}
521525
}

src/tools/clippy/tests/ui/crashes/ice-10912.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
//@no-rustfix
33
fn f2() -> impl Sized { && 3.14159265358979323846E }
44
//~^ ERROR: expected at least one digit in exponent
5-
//~| ERROR: long literal lacking separators
6-
//~| NOTE: `-D clippy::unreadable-literal` implied by `-D warnings`
75

86
fn main() {}

src/tools/clippy/tests/ui/crashes/ice-10912.stderr

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,5 @@ error: expected at least one digit in exponent
44
LL | fn f2() -> impl Sized { && 3.14159265358979323846E }
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: long literal lacking separators
8-
--> tests/ui/crashes/ice-10912.rs:3:28
9-
|
10-
LL | fn f2() -> impl Sized { && 3.14159265358979323846E }
11-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider: `3.141_592_653_589_793_238_46`
12-
|
13-
= note: `-D clippy::unreadable-literal` implied by `-D warnings`
14-
= help: to override `-D warnings` add `#[allow(clippy::unreadable_literal)]`
15-
16-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
178

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 6.674e−11; // m³⋅kg⁻¹⋅s⁻²
22
//~^ ERROR expected at least one digit in exponent
33
//~| ERROR unknown start of token: \u{2212}
4-
//~| ERROR cannot subtract `{integer}` from `{float}`
54

65
fn main() {}

tests/ui/did_you_mean/issue-49746-unicode-confusable-in-float-literal-expt.stderr

+1-20
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,5 @@ help: Unicode character '−' (Minus Sign) looks like '-' (Minus/Hyphen), but it
1515
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 6.674e-11; // m³⋅kg⁻¹⋅s⁻²
1616
| ~
1717

18-
error[E0277]: cannot subtract `{integer}` from `{float}`
19-
--> $DIR/issue-49746-unicode-confusable-in-float-literal-expt.rs:1:53
20-
|
21-
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 6.674e−11; // m³⋅kg⁻¹⋅s⁻²
22-
| ^ no implementation for `{float} - {integer}`
23-
|
24-
= help: the trait `Sub<{integer}>` is not implemented for `{float}`
25-
= help: the following other types implement trait `Sub<Rhs>`:
26-
<isize as Sub>
27-
<isize as Sub<&isize>>
28-
<i8 as Sub>
29-
<i8 as Sub<&i8>>
30-
<i16 as Sub>
31-
<i16 as Sub<&i16>>
32-
<i32 as Sub>
33-
<i32 as Sub<&i32>>
34-
and 48 others
35-
36-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
3719

38-
For more information about this error, try `rustc --explain E0277`.

tests/ui/parser/float-field.rs

+51-20
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,91 @@ struct S(u8, (u8, u8));
33
fn main() {
44
let s = S(0, (0, 0));
55

6-
s.1e1; //~ ERROR no field `1e1` on type `S`
7-
s.1.; //~ ERROR unexpected token: `;`
8-
s.1.1;
9-
s.1.1e1; //~ ERROR no field `1e1` on type `(u8, u8)`
6+
{ s.1e1; } //~ ERROR no field `1e1` on type `S`
7+
8+
{ s.1.; } //~ ERROR unexpected token: `;`
9+
10+
{ s.1.1; }
11+
12+
{ s.1.1e1; } //~ ERROR no field `1e1` on type `(u8, u8)`
13+
1014
{ s.1e+; } //~ ERROR unexpected token: `1e+`
1115
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e+`
1216
//~| ERROR expected at least one digit in exponent
17+
1318
{ s.1e-; } //~ ERROR unexpected token: `1e-`
1419
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e-`
1520
//~| ERROR expected at least one digit in exponent
21+
1622
{ s.1e+1; } //~ ERROR unexpected token: `1e+1`
1723
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e+1`
24+
1825
{ s.1e-1; } //~ ERROR unexpected token: `1e-1`
1926
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e-1`
27+
2028
{ s.1.1e+1; } //~ ERROR unexpected token: `1.1e+1`
2129
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1.1e+1`
30+
2231
{ s.1.1e-1; } //~ ERROR unexpected token: `1.1e-1`
2332
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1.1e-1`
24-
s.0x1e1; //~ ERROR no field `0x1e1` on type `S`
25-
s.0x1.; //~ ERROR no field `0x1` on type `S`
26-
//~| ERROR hexadecimal float literal is not supported
27-
//~| ERROR unexpected token: `;`
28-
s.0x1.1; //~ ERROR no field `0x1` on type `S`
29-
//~| ERROR hexadecimal float literal is not supported
30-
s.0x1.1e1; //~ ERROR no field `0x1` on type `S`
31-
//~| ERROR hexadecimal float literal is not supported
33+
34+
{ s.0x1e1; } //~ ERROR no field `0x1e1` on type `S`
35+
36+
{ s.0x1.; } //~ ERROR hexadecimal float literal is not supported
37+
//~| ERROR unexpected token: `0x1.`
38+
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `0x1.`
39+
40+
{ s.0x1.1; } //~ ERROR hexadecimal float literal is not supported
41+
//~| ERROR unexpected token: `0x1.1`
42+
//~| expected one of `.`, `;`, `?`, `}`, or an operator, found `0x1.1`
43+
44+
{ s.0x1.1e1; } //~ ERROR hexadecimal float literal is not supported
45+
//~| ERROR unexpected token: `0x1.1e1`
46+
//~| expected one of `.`, `;`, `?`, `}`, or an operator, found `0x1.1e1`
47+
3248
{ s.0x1e+; } //~ ERROR expected expression, found `;`
49+
3350
{ s.0x1e-; } //~ ERROR expected expression, found `;`
34-
s.0x1e+1; //~ ERROR no field `0x1e` on type `S`
35-
s.0x1e-1; //~ ERROR no field `0x1e` on type `S`
51+
52+
{ s.0x1e+1; } //~ ERROR no field `0x1e` on type `S`
53+
54+
{ s.0x1e-1; } //~ ERROR no field `0x1e` on type `S`
55+
3656
{ s.0x1.1e+1; } //~ ERROR unexpected token: `0x1.1e+1`
3757
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `0x1.1e+1`
3858
//~| ERROR hexadecimal float literal is not supported
59+
3960
{ s.0x1.1e-1; } //~ ERROR unexpected token: `0x1.1e-1`
4061
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `0x1.1e-1`
4162
//~| ERROR hexadecimal float literal is not supported
42-
s.1e1f32; //~ ERROR no field `1e1` on type `S`
43-
//~| ERROR suffixes on a tuple index are invalid
44-
s.1.f32; //~ ERROR no field `f32` on type `(u8, u8)`
45-
s.1.1f32; //~ ERROR suffixes on a tuple index are invalid
46-
s.1.1e1f32; //~ ERROR no field `1e1` on type `(u8, u8)`
47-
//~| ERROR suffixes on a tuple index are invalid
63+
64+
{ s.1e1f32; } //~ ERROR no field `1e1` on type `S`
65+
//~| ERROR suffixes on a tuple index are invalid
66+
67+
{ s.1.f32; } //~ ERROR no field `f32` on type `(u8, u8)`
68+
69+
{ s.1.1f32; } //~ ERROR suffixes on a tuple index are invalid
70+
71+
{ s.1.1e1f32; } //~ ERROR no field `1e1` on type `(u8, u8)`
72+
//~| ERROR suffixes on a tuple index are invalid
73+
4874
{ s.1e+f32; } //~ ERROR unexpected token: `1e+f32`
4975
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e+f32`
5076
//~| ERROR expected at least one digit in exponent
77+
5178
{ s.1e-f32; } //~ ERROR unexpected token: `1e-f32`
5279
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e-f32`
5380
//~| ERROR expected at least one digit in exponent
81+
5482
{ s.1e+1f32; } //~ ERROR unexpected token: `1e+1f32`
5583
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e+1f32`
84+
5685
{ s.1e-1f32; } //~ ERROR unexpected token: `1e-1f32`
5786
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1e-1f32`
87+
5888
{ s.1.1e+1f32; } //~ ERROR unexpected token: `1.1e+1f32`
5989
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1.1e+1f32`
90+
6091
{ s.1.1e-1f32; } //~ ERROR unexpected token: `1.1e-1f32`
6192
//~| ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `1.1e-1f32`
6293
}

0 commit comments

Comments
 (0)