Skip to content

Commit f17e3a3

Browse files
committed
Auto merge of rust-lang#137394 - petrochenkov:nounderexp, r=<try>
lexer: Disallow some leading underscores in float exponents A second, scaled down, attempt at rust-lang#114567. cc rust-lang#111628 (comment)
2 parents 9f48ded + f77e219 commit f17e3a3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

compiler/rustc_lexer/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,13 @@ impl Cursor<'_> {
964964
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
965965
if self.first() == '-' || self.first() == '+' {
966966
self.bump();
967+
// Reject floats like `1e+_2` and `1.2e+_3` to avoid introducing identifier
968+
// tokens into the possible "fine-grained" tokenization of floats.
969+
// (Note that `1e_2` and `1.2e_3` are still accepted below because
970+
// they don't introduce identifiers, only suffixed integers.)
971+
if self.first() == '_' {
972+
return false;
973+
}
967974
}
968975
self.eat_decimal_digits()
969976
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ fn main() {
3232
0o123.456; //~ ERROR: octal float literal is not supported
3333
0b101f64; //~ ERROR: binary float literal is not supported
3434
0b111.101; //~ ERROR: binary float literal is not supported
35+
1e_2; // OK for now
36+
1.2e_3; // OK for now
37+
1e+_2; //~ ERROR expected at least one digit in exponent
38+
1e-_2; //~ ERROR expected at least one digit in exponent
39+
1.2e+_3; //~ ERROR expected at least one digit in exponent
40+
1.2e-_3; //~ ERROR expected at least one digit in exponent
41+
0x539.0; //~ ERROR: hexadecimal float literal is not supported
3542
}

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ error: binary float literal is not supported
106106
LL | 0b111.101;
107107
| ^^^^^^^^^
108108

109+
error: expected at least one digit in exponent
110+
--> $DIR/lex-bad-numeric-literals.rs:37:5
111+
|
112+
LL | 1e+_2;
113+
| ^^^^^
114+
115+
error: expected at least one digit in exponent
116+
--> $DIR/lex-bad-numeric-literals.rs:38:5
117+
|
118+
LL | 1e-_2;
119+
| ^^^^^
120+
121+
error: expected at least one digit in exponent
122+
--> $DIR/lex-bad-numeric-literals.rs:39:5
123+
|
124+
LL | 1.2e+_3;
125+
| ^^^^^^^
126+
127+
error: expected at least one digit in exponent
128+
--> $DIR/lex-bad-numeric-literals.rs:40:5
129+
|
130+
LL | 1.2e-_3;
131+
| ^^^^^^^
132+
133+
error: hexadecimal float literal is not supported
134+
--> $DIR/lex-bad-numeric-literals.rs:41:5
135+
|
136+
LL | 0x539.0;
137+
| ^^^^^^^
138+
109139
error: octal float literal is not supported
110140
--> $DIR/lex-bad-numeric-literals.rs:5:5
111141
|
@@ -164,6 +194,6 @@ error: binary float literal is not supported
164194
LL | 0b101f64;
165195
| ^^^^^^^^ not supported
166196

167-
error: aborting due to 26 previous errors
197+
error: aborting due to 31 previous errors
168198

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

0 commit comments

Comments
 (0)