Unexpected use of "ConditionalExpression" in JLS grammars
Leon Linhart
themrmilchmann at gmail.com
Thu Mar 9 12:15:41 UTC 2023
Hi,
Initially, I believed to have found a mistake in the JLS (Java 19
Edition) that
was introduced with JEP 361. However, while further looking into the
spec, this
has developed into more of a question than a report.
In several places in the grammar where I'd expect to find
`ConstantExpression`,
`ConditionalExpression` is used instead. Specifically, in §14.11.1 [1], the
grammar for a `CaseConstant` is defined through a
`ConditionalExpression`. I'd
tend to think this should at least be a `ConstantExpression` instead as this
would be more in line with the textual description of a case constant:
> Every case constant must be either a constant expression (§15.29) or
the name
of an enum constant (§8.9.1), or a compile-time error occurs.
Although, from a quick reading of the JLS, I'm not sure if
`ConstantExpression`
covers names of enum constants. However, neither does the current
definition.
Interestingly, the same production is used for the definition of
`ElementValue`
for annotations in §9.7.1 [2], which seems similarly out-of-place to me.
My opinion is further reinforced by the fact that the following snippet
compiles despite using a constant expression for a case label that is not a
conditional expression:
public static final int BASE = 42;
public void printForBasePlusOne(int i) {
switch (i) {
case BASE + 1 -> System.out.println("Test");
default -> {}
}
}
Now, I'm curious about the reasoning behind using `ConditionalExpression` in
these places in the JLS and if this might actually be a mistake. If this
is not
the right place to ask about this, please point me in the right direction.
Regards
Leon Linhart
[1]
https://docs.oracle.com/javase/specs/jls/se19/html/jls-14.html#jls-14.11.1
[2] https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.7.1
More information about the amber-dev
mailing list