JDK-8324859 : Improve error recovery
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 22,23
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2024-01-29
  • Updated: 2024-09-02
  • Resolved: 2024-08-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 24
24 b13Fixed
Related Reports
Relates :  
Description
In some cases, there are some extraneous error messages when an opening brace is missing.   Error recovery could be improved to reduce the number of error messages for a single syntax error.

i.e. - in JDK 22, the following missing open brace on a method generates 4 error messages, one more than with JDK 21.

package tests; // TestB.java:1: error: implicitly declared class should not have package declaration
public class TestB {
    public static boolean test() // <--- missing open brace
        return true;
    }
    public static boolean test2() {
        return true;
    }
}
Comments
Changeset: 0b4a7d53 Branch: master Author: Jan Lahoda <jlahoda@openjdk.org> Date: 2024-08-29 06:25:27 +0000 URL: https://git.openjdk.org/jdk/commit/0b4a7d534204b7b3b041f5117282dd13b1c7c62f
29-08-2024

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/20070 Date: 2024-07-08 10:03:03 +0000
09-07-2024

Basically, the main issue here is that since the opening brace is missing, the parser will use the first closing brace ('}') as the closing brace for the class, and as a consequence, the rest of the file is parsed in a wrong context. That is a somewhat more general problem - the parser handles missing closing braces better than missing opening braces. The original errors were also not particularly good: --- $ javac /tmp/TestB.java /tmp/TestB.java:3: error: ';' expected public static boolean test() // <--- missing open brace ^ /tmp/TestB.java:6: error: class, interface, enum, or record expected public static boolean test2() { ^ /tmp/TestB.java:8: error: class, interface, enum, or record expected } ^ 3 errors --- So, it seems to me it would be good to try to improve the parser to better handle (some) missing opening braces. I've started with a prototype here: https://github.com/openjdk/jdk/compare/master...lahodaj:jdk:JDK-8324859?expand=1 But it will need more work and more testing. So, I've afraid this looks unlikely for JDK 23.
30-05-2024

The issue with the fix as is - if the user declares a class before the main method then they are hosed. Example: record Point (float x, float y) {} void main() { ... } So canBeImplicitClass = false; has to be more sophisticated, with a check that the primary class has been seen.
09-02-2024