Skip to content

Lexer silently accepts non-decimal integer literals without digits #4490

@nsvke

Description

@nsvke

Summary

While refactoring the non-decimal integer literal parsing logic in the lexer, I noticed that gccrs does not enforce the presence of valid digits after a base prefix (0x, 0b, 0o). It silently accepts the empty prefix as a valid literal and evaluates it to 0.

Reproducer

I tried this code:

fn main() {
    let _a: u32 = 0x;
    let _b: i32 = 0b;
    let _c: usize = 0o;
}

Actual behavior

gccrs compiles the above code without emitting any errors. The lexer produces a valid token with the value 0 because the internal string parsing loop naturally terminates, and the empty string is evaluated as 0 by the underlying conversion logic.

Expected behavior

error[E0768]: no valid digits found for number
 --> src/main.rs:2:19
  |
2 |     let _a: u32 = 0x;
  |                   ^^
error[E0768]: no valid digits found for number
 --> src/main.rs:3:19
  |
3 |     let _b: i32 = 0b;
  |                   ^^
error[E0768]: no valid digits found for number
 --> src/main.rs:4:21
  |
4 |     let _c: usize = 0o;
  |                     ^^
For more information about this error, try `rustc --explain E0768`.
error: could not compile `t` (bin "t") due to 3 previous errors

GCC Version

commit-hash: 0c1834c

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions