Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lexer: Disallow some leading underscores in float exponents #137394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Loading