Skip to content

Commit 99c21f2

Browse files
committed
Disallow leading underscores in float exponents.
Such as `1e_3`, `1E+__3`, `1e-_________3_3`. - They are ugly and never used in practice. (The test suite and compiler code have no examples of them.) - They don't match normal decimal literals. (You can't write `x = _3;`.) - They complicate attempts to allow integers with suffixes beginning with `e`, such as `1em` (currently disallowed, but desired in #111615). Because when given a char sequence like `1e` the lexer must decide whether what follows the `e` is a decimal integer (in which case it's a float with exponent) or something else (in which case it's an integer with a suffix). But unbounded char lookahead is required to get past the possibly unlimited number of leading underscores. Disallowing the leading underscores reduces the lookahead to two: one for a possible `+`/`-`, and then one more for a digit or non-digit.
1 parent d12c6e9 commit 99c21f2

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,14 @@ impl Cursor<'_> {
869869
if self.first() == '-' || self.first() == '+' {
870870
self.bump();
871871
}
872-
self.eat_decimal_digits()
872+
match self.first() {
873+
'0'..='9' => {
874+
self.bump();
875+
self.eat_decimal_digits();
876+
true
877+
}
878+
_ => false,
879+
}
873880
}
874881

875882
// Eats the suffix of the literal, e.g. "u8".

tests/ui/lexer/lex-bad-numeric-literals.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ fn main() {
1212
0x9.0e-9; //~ ERROR: hexadecimal float literal is not supported
1313
0o; //~ ERROR: no valid digits
1414
1e+; //~ ERROR: expected at least one digit in exponent
15+
1e_3;
16+
//~^ ERROR: invalid suffix `_3` for float literal
17+
//~| ERROR: expected at least one digit in exponent
18+
1E+__3;
19+
//~^ ERROR: invalid suffix `__3` for float literal
20+
//~| ERROR: expected at least one digit in exponent
21+
1e-______3_3;
22+
//~^ ERROR: invalid suffix `______3_3` for float literal
23+
//~| ERROR: expected at least one digit in exponent
24+
1em;
25+
//~^ ERROR: invalid suffix `m` for float literal
26+
//~| ERROR: expected at least one digit in exponent
1527
0x539.0; //~ ERROR: hexadecimal float literal is not supported
1628
9900000000000000000000000000999999999999999999999999999999;
1729
//~^ ERROR: integer literal is too large

tests/ui/lexer/lex-bad-numeric-literals.stderr

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,50 +58,74 @@ error: expected at least one digit in exponent
5858
LL | 1e+;
5959
| ^^^
6060

61-
error: hexadecimal float literal is not supported
61+
error: expected at least one digit in exponent
6262
--> $DIR/lex-bad-numeric-literals.rs:15:5
6363
|
64+
LL | 1e_3;
65+
| ^^^^
66+
67+
error: expected at least one digit in exponent
68+
--> $DIR/lex-bad-numeric-literals.rs:18:5
69+
|
70+
LL | 1E+__3;
71+
| ^^^^^^
72+
73+
error: expected at least one digit in exponent
74+
--> $DIR/lex-bad-numeric-literals.rs:21:5
75+
|
76+
LL | 1e-______3_3;
77+
| ^^^^^^^^^^^^
78+
79+
error: expected at least one digit in exponent
80+
--> $DIR/lex-bad-numeric-literals.rs:24:5
81+
|
82+
LL | 1em;
83+
| ^^^
84+
85+
error: hexadecimal float literal is not supported
86+
--> $DIR/lex-bad-numeric-literals.rs:27:5
87+
|
6488
LL | 0x539.0;
6589
| ^^^^^^^
6690

6791
error[E0768]: no valid digits found for number
68-
--> $DIR/lex-bad-numeric-literals.rs:26:5
92+
--> $DIR/lex-bad-numeric-literals.rs:38:5
6993
|
7094
LL | 0x;
7195
| ^^
7296

7397
error[E0768]: no valid digits found for number
74-
--> $DIR/lex-bad-numeric-literals.rs:27:5
98+
--> $DIR/lex-bad-numeric-literals.rs:39:5
7599
|
76100
LL | 0xu32;
77101
| ^^
78102

79103
error[E0768]: no valid digits found for number
80-
--> $DIR/lex-bad-numeric-literals.rs:28:5
104+
--> $DIR/lex-bad-numeric-literals.rs:40:5
81105
|
82106
LL | 0ou32;
83107
| ^^
84108

85109
error[E0768]: no valid digits found for number
86-
--> $DIR/lex-bad-numeric-literals.rs:29:5
110+
--> $DIR/lex-bad-numeric-literals.rs:41:5
87111
|
88112
LL | 0bu32;
89113
| ^^
90114

91115
error[E0768]: no valid digits found for number
92-
--> $DIR/lex-bad-numeric-literals.rs:30:5
116+
--> $DIR/lex-bad-numeric-literals.rs:42:5
93117
|
94118
LL | 0b;
95119
| ^^
96120

97121
error: octal float literal is not supported
98-
--> $DIR/lex-bad-numeric-literals.rs:32:5
122+
--> $DIR/lex-bad-numeric-literals.rs:44:5
99123
|
100124
LL | 0o123.456;
101125
| ^^^^^^^^^
102126

103127
error: binary float literal is not supported
104-
--> $DIR/lex-bad-numeric-literals.rs:34:5
128+
--> $DIR/lex-bad-numeric-literals.rs:46:5
105129
|
106130
LL | 0b111.101;
107131
| ^^^^^^^^^
@@ -112,58 +136,90 @@ error: octal float literal is not supported
112136
LL | 0o2f32;
113137
| ^^^^^^ not supported
114138

139+
error: invalid suffix `_3` for float literal
140+
--> $DIR/lex-bad-numeric-literals.rs:15:5
141+
|
142+
LL | 1e_3;
143+
| ^^^^ invalid suffix `_3`
144+
|
145+
= help: valid suffixes are `f32` and `f64`
146+
147+
error: invalid suffix `__3` for float literal
148+
--> $DIR/lex-bad-numeric-literals.rs:18:5
149+
|
150+
LL | 1E+__3;
151+
| ^^^^^^ invalid suffix `__3`
152+
|
153+
= help: valid suffixes are `f32` and `f64`
154+
155+
error: invalid suffix `______3_3` for float literal
156+
--> $DIR/lex-bad-numeric-literals.rs:21:5
157+
|
158+
LL | 1e-______3_3;
159+
| ^^^^^^^^^^^^ invalid suffix `______3_3`
160+
|
161+
= help: valid suffixes are `f32` and `f64`
162+
163+
error: invalid suffix `m` for float literal
164+
--> $DIR/lex-bad-numeric-literals.rs:24:5
165+
|
166+
LL | 1em;
167+
| ^^^ invalid suffix `m`
168+
|
169+
= help: valid suffixes are `f32` and `f64`
170+
115171
error: integer literal is too large
116-
--> $DIR/lex-bad-numeric-literals.rs:16:5
172+
--> $DIR/lex-bad-numeric-literals.rs:28:5
117173
|
118174
LL | 9900000000000000000000000000999999999999999999999999999999;
119175
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120176
|
121177
= note: value exceeds limit of `340282366920938463463374607431768211455`
122178

123179
error: integer literal is too large
124-
--> $DIR/lex-bad-numeric-literals.rs:18:5
180+
--> $DIR/lex-bad-numeric-literals.rs:30:5
125181
|
126182
LL | 9900000000000000000000000000999999999999999999999999999999;
127183
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128184
|
129185
= note: value exceeds limit of `340282366920938463463374607431768211455`
130186

131187
error: integer literal is too large
132-
--> $DIR/lex-bad-numeric-literals.rs:20:5
188+
--> $DIR/lex-bad-numeric-literals.rs:32:5
133189
|
134190
LL | 0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110;
135191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136192
|
137193
= note: value exceeds limit of `0b11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111`
138194

139195
error: integer literal is too large
140-
--> $DIR/lex-bad-numeric-literals.rs:22:5
196+
--> $DIR/lex-bad-numeric-literals.rs:34:5
141197
|
142198
LL | 0o37777777777777777777777777777777777777777770;
143199
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
144200
|
145201
= note: value exceeds limit of `0o3777777777777777777777777777777777777777777`
146202

147203
error: integer literal is too large
148-
--> $DIR/lex-bad-numeric-literals.rs:24:5
204+
--> $DIR/lex-bad-numeric-literals.rs:36:5
149205
|
150206
LL | 0xffffffffffffffffffffffffffffffff0;
151207
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152208
|
153209
= note: value exceeds limit of `0xffffffffffffffffffffffffffffffff`
154210

155211
error: octal float literal is not supported
156-
--> $DIR/lex-bad-numeric-literals.rs:31:5
212+
--> $DIR/lex-bad-numeric-literals.rs:43:5
157213
|
158214
LL | 0o123f64;
159215
| ^^^^^^^^ not supported
160216

161217
error: binary float literal is not supported
162-
--> $DIR/lex-bad-numeric-literals.rs:33:5
218+
--> $DIR/lex-bad-numeric-literals.rs:45:5
163219
|
164220
LL | 0b101f64;
165221
| ^^^^^^^^ not supported
166222

167-
error: aborting due to 26 previous errors
223+
error: aborting due to 34 previous errors
168224

169225
For more information about this error, try `rustc --explain E0768`.

0 commit comments

Comments
 (0)