RFR: JDK-8228647: Broken enum produce inconvenient errors and AST

Jan Lahoda jan.lahoda at oracle.com
Mon Jul 29 11:31:21 UTC 2019


Hi,

The javac's parser does not recover well from enums that have missing 
',' between the enum constants, or are missing the ';' between constants 
and other members. For example, consider:
---
$ cat E.java
public enum E {
     A
     B,
     C;
     public void t1() {}
     public void t2() {}
}
$ javac E.java
E.java:3: error: ',', '}', or ';' expected
     B,
     ^
E.java:3: error: '}' expected
     B,
      ^
E.java:5: error: class, interface, or enum expected
     public void t1() {}
            ^
E.java:6: error: class, interface, or enum expected
     public void t2() {}
            ^
4 errors
---

The reason for this is that the parser stops looking for enum constants 
if there is no ',' after a constant, and does not try to parse other 
members if there is no ';' after the constants block. I.e. in the 
example above, it will stop looking for enum constants after "A" (no 
comma), and won't look for further members because there is no 
semicolon, after which the parser is somewhat lost.

The patch proposed herein is trying to improve the situation by parsing 
member until the closing semicolon, estimating whether the upcoming 
member is an enum constant, other member, or "unknown". Unknown members 
are parsed either as enum constants or ordinary members, depending on 
whether they are before or after ';' that separates enum constants and 
members. One limitation of this estimate are members which start with an 
annotation - these will be currently categorized as "unknown", and 
parsed based on their position. If needed, we could improve the behavior 
by estimating the category only after annotations are parsed.

This is a spin off from JDK-8228451.

JBS: https://bugs.openjdk.java.net/browse/JDK-8228647
Webrev: http://cr.openjdk.java.net/~jlahoda/8228647/webrev.00/index.html

How does this look?

Thanks,
     Jan


More information about the compiler-dev mailing list