RFR: 8325440: Confusing error reported for octal literals with wrong digits [v2]
Jan Lahoda
jlahoda at openjdk.org
Thu Feb 8 09:13:11 UTC 2024
> Consider code like:
>
> int i = 08;
>
>
> Currently, javac will produce an error like:
>
> /tmp/Octal.java:2: error: ';' expected
> int i = 08;
> ^
>
>
> Which is fairly confusing. The cause is that this gets lexed as two tokens: `0` and `8`, as `8` is not a valid digit for an octal literal. The current behavior is a consequence of:
> https://bugs.openjdk.org/browse/JDK-8267361
>
> Before that fix, the error was:
>
> /tmp/Octal.java:2: error: integer number too large
> int i = 08;
> ^
>
>
> which was not better.
>
> Please see the original PR:
> https://github.com/openjdk/jdk/pull/4111
> for more information on the decision there.
>
> It seems to me that it would be better to have an error pinpointing the problem (i.e. wrong digit in what is the octal literal). Which is what this patch is trying to do. It would be possible to do that in the lexer, but that is slightly complex. So, this patch:
> - permits non-octal (and non-binary) digits in octal (and binary) literals, while still preserving the fact the literals are octal (binary)
> - when the parser tries to convert the textual literal representation to a number, and fails to do that, get a `NumberFormatException`. The handling of this exception is improved to provide better errors.
>
> Note that, for consistency, this patch works not only for octal literals (which is likely the most problematic case in practice), but also for binary literals, although it seems relatively unlikely code would contain literals like `0b123` (unlike `08`, which may be a simple mistake).
>
> The new error is:
>
> /tmp/Octal.java:2: error: illegal digit in an octal literal
> int i = 08;
> ^
>
>
> (the error message can be re-phrased as needed/desired, of course.)
Jan Lahoda has updated the pull request incrementally with one additional commit since the last revision:
Fixing typo.
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/17760/files
- new: https://git.openjdk.org/jdk/pull/17760/files/8d20415e..4fc4b63b
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=17760&range=01
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=17760&range=00-01
Stats: 1 line in 1 file changed: 0 ins; 0 del; 1 mod
Patch: https://git.openjdk.org/jdk/pull/17760.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/17760/head:pull/17760
PR: https://git.openjdk.org/jdk/pull/17760
More information about the compiler-dev
mailing list