Skip to content

Commit

Permalink
Auto merge of rust-lang#137394 - petrochenkov:nounderexp, r=<try>
Browse files Browse the repository at this point in the history
lexer: Disallow some leading underscores in float exponents

A second, scaled down, attempt at rust-lang#114567.
cc rust-lang#111628 (comment)
  • Loading branch information
bors committed Feb 21, 2025
2 parents 9f48ded + f77e219 commit f17e3a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,13 @@ impl Cursor<'_> {
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
if self.first() == '-' || self.first() == '+' {
self.bump();
// Reject floats like `1e+_2` and `1.2e+_3` to avoid introducing identifier
// tokens into the possible "fine-grained" tokenization of floats.
// (Note that `1e_2` and `1.2e_3` are still accepted below because
// they don't introduce identifiers, only suffixed integers.)
if self.first() == '_' {
return false;
}
}
self.eat_decimal_digits()
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/lexer/lex-bad-numeric-literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ fn main() {
0o123.456; //~ ERROR: octal float literal is not supported
0b101f64; //~ ERROR: binary float literal is not supported
0b111.101; //~ ERROR: binary float literal is not supported
1e_2; // OK for now
1.2e_3; // OK for now
1e+_2; //~ ERROR expected at least one digit in exponent
1e-_2; //~ ERROR expected at least one digit in exponent
1.2e+_3; //~ ERROR expected at least one digit in exponent
1.2e-_3; //~ ERROR expected at least one digit in exponent
0x539.0; //~ ERROR: hexadecimal float literal is not supported
}
32 changes: 31 additions & 1 deletion tests/ui/lexer/lex-bad-numeric-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ error: binary float literal is not supported
LL | 0b111.101;
| ^^^^^^^^^

error: expected at least one digit in exponent
--> $DIR/lex-bad-numeric-literals.rs:37:5
|
LL | 1e+_2;
| ^^^^^

error: expected at least one digit in exponent
--> $DIR/lex-bad-numeric-literals.rs:38:5
|
LL | 1e-_2;
| ^^^^^

error: expected at least one digit in exponent
--> $DIR/lex-bad-numeric-literals.rs:39:5
|
LL | 1.2e+_3;
| ^^^^^^^

error: expected at least one digit in exponent
--> $DIR/lex-bad-numeric-literals.rs:40:5
|
LL | 1.2e-_3;
| ^^^^^^^

error: hexadecimal float literal is not supported
--> $DIR/lex-bad-numeric-literals.rs:41:5
|
LL | 0x539.0;
| ^^^^^^^

error: octal float literal is not supported
--> $DIR/lex-bad-numeric-literals.rs:5:5
|
Expand Down Expand Up @@ -164,6 +194,6 @@ error: binary float literal is not supported
LL | 0b101f64;
| ^^^^^^^^ not supported

error: aborting due to 26 previous errors
error: aborting due to 31 previous errors

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

0 comments on commit f17e3a3

Please sign in to comment.