Skip to content

Commit b09af8e

Browse files
nsvkeP-E-P
authored andcommitted
lex: Emit E0768 for empty non-decimal literals
Previously, the lexer evaluated empty non-decimal literals (like 0x, 0b, 0o) as 0. Now, it emits error E0768 when there are no valid digits. gcc/rust/ChangeLog: * lex/rust-lex.cc (Lexer::parse_non_decimal_int_literal): Emit E0768. gcc/testsuite/ChangeLog: * rust/compile/empty-non-decimal.rs: New test. Signed-off-by: Enes Cevik <nsvke@proton.me>
1 parent b992076 commit b09af8e

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

gcc/rust/lex/rust-lex.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,15 +2150,22 @@ Lexer::parse_non_decimal_int_literal (location_t loc, IsDigitFunc is_digit_func,
21502150
raw_str += current_char; // x, o, b
21512151
skip_input ();
21522152

2153-
int length = 1;
2153+
int length = 2;
2154+
bool has_valid_digit = false;
21542155

21552156
current_char = peek_input ();
21562157

2157-
length++;
2158-
21592158
// loop through to add entire number to string
2160-
while (is_digit_func (current_char.value) || current_char == '_')
2159+
while (true)
21612160
{
2161+
if (is_digit_func (current_char.value))
2162+
{
2163+
has_valid_digit = true;
2164+
}
2165+
else if (current_char != '_')
2166+
{
2167+
break;
2168+
}
21622169
length++;
21632170

21642171
raw_str += current_char;
@@ -2176,6 +2183,11 @@ Lexer::parse_non_decimal_int_literal (location_t loc, IsDigitFunc is_digit_func,
21762183

21772184
current_column += length;
21782185

2186+
if (!has_valid_digit)
2187+
{
2188+
rust_error_at (loc, ErrorCode::E0768, "no valid digits found for number");
2189+
}
2190+
21792191
loc += length - 1;
21802192

21812193
return Token::make_int (loc, std::move (raw_str), suffix_start, base,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
fn main() {
5+
let _a = 0x; // { dg-error "no valid digits found for number" }
6+
let _b = 0b; // { dg-error "no valid digits found for number" }
7+
let _c = 0o; // { dg-error "no valid digits found for number" }
8+
let _d = 0x_; // { dg-error "no valid digits found for number" }
9+
let _e = 0x_u32; // { dg-error "no valid digits found for number" }
10+
}

0 commit comments

Comments
 (0)