Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8337976: Insufficient error recovery in parser for switch inside class body #20526

Closed
wants to merge 5 commits into from

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Aug 9, 2024

Consider this code:

public class T9999999 {
    switch (0) { default: }
}

When trying to compile this, javac crashes:

$ .../jdk-22/bin/javac -XDdev /tmp/T9999999.java
/tmp/T9999999.java:2: error: switch expression does not have any result expressions
    switch (0) { default: } f;
    ^
1 error
An exception has occurred in the compiler (22.0.1-internal). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com/) after checking the Bug Database (https://bugs.java.com/) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.AssertionError: Unexpected tree: switch (0) {
default:

} with kind: SWITCH_EXPRESSION within: switch (0) {
default:

} with kind: SWITCH_EXPRESSION
        at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:162)
        at jdk.compiler/com.sun.tools.javac.comp.Attr$TypeAnnotationsValidator.validateAnnotatedType(Attr.java:5881)
        at jdk.compiler/com.sun.tools.javac.comp.Attr$TypeAnnotationsValidator.visitVarDef(Attr.java:5727)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1022)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
        at jdk.compiler/com.sun.tools.javac.comp.Attr$TypeAnnotationsValidator.visitClassDef(Attr.java:5780)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:814)
        at jdk.compiler/com.sun.tools.javac.comp.Attr.validateTypeAnnotations(Attr.java:5677)
        at jdk.compiler/com.sun.tools.javac.code.TypeAnnotations.lambda$validateTypeAnnotationsSignatures$1(TypeAnnotations.java:144)
        at jdk.compiler/com.sun.tools.javac.comp.Annotate.flush(Annotate.java:200)
        at jdk.compiler/com.sun.tools.javac.comp.Annotate.unblockAnnotations(Annotate.java:144)
        at jdk.compiler/com.sun.tools.javac.comp.Annotate.enterDone(Annotate.java:157)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.enterDone(JavaCompiler.java:1827)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:1081)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:947)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
        at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:66)
        at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:52)
printing javac parameters to: /home/jlahoda/src/jdk/jdk/javac.20240809_163551.args

The problem is that while parsing a the class body, javac will try to parse a type (as a type of a field or method), but finds the switch. Which will get parsed as an expression, and used as the type of the field f. This then crashes type annotation validation, but generally does not make much sense.

The proposal here is to check if there's a token that inevitably is a start of a statement used inside a class (or interface, enum, annotation type or record), and if yes, parse the input as a statement.

As a result, the parser tolerates statements inside a class body. The statement's tree is wrapped inside an ErroneousTree, so that it is obvious it is not a correct AST.

The attribution is also tweaked a bit to handle the augmented AST better.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8337976: Insufficient error recovery in parser for switch inside class body (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/20526/head:pull/20526
$ git checkout pull/20526

Update a local copy of the PR:
$ git checkout pull/20526
$ git pull https://git.openjdk.org/jdk.git pull/20526/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20526

View PR using the GUI difftool:
$ git pr show -t 20526

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20526.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 9, 2024

👋 Welcome back jlahoda! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 9, 2024

@lahodaj This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8337976: Insufficient error recovery in parser for switch inside class body

Reviewed-by: vromero

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 113 new commits pushed to the master branch:

  • 61d1dc5: 8334466: Ambiguous method call with generics may cause FunctionDescriptorLookupError
  • 89a15f1: 8337681: PNGImageWriter uses much more memory than necessary
  • a6c0630: 8337938: ZUtils::alloc_aligned allocates without reporting to NMT
  • 0320460: 8337958: Out-of-bounds array access in secondary_super_cache
  • 692f5cb: 8338101: remove old remap assertion in map_or_reserve_memory_aligned after JDK-8338058
  • 0e7c1c1: 8338112: Test testlibrary_tests/ir_framework/tests/TestPrivilegedMode.java fails with release build
  • 6a3d045: 8337709: Use allocated states for chunking large array processing
  • 358d77d: 8337657: AArch64: No need for acquire fence in safepoint poll during JNI calls
  • 60fa08f: 8337797: Additional ExternalAddress cleanup
  • 3cf3f30: 8330191: Fix typo in precompiled.hpp
  • ... and 103 more: https://git.openjdk.org/jdk/compare/9b428dda8fb86ed595b05f3c930b3ce9c341db9b...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 9, 2024
@openjdk
Copy link

openjdk bot commented Aug 9, 2024

@lahodaj The following label will be automatically applied to this pull request:

  • compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the compiler compiler-dev@openjdk.org label Aug 9, 2024
@mlbridge
Copy link

mlbridge bot commented Aug 9, 2024

Webrevs


private boolean isDefiniteStatementStartToken() {
return switch (token.kind) {
case IF, WHILE, DO, SWITCH, RETURN, TRY, FOR, ASSERT, BREAK,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should yield, synchronized be included?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

synchronized may be part of an method declaration (synchronized void t() {}). For yield, even though yield cannot be used as a type name, I thought it would be better to let the rest of error recovery mechanisms to handle it, hopefully producing some sensible error in more cases.

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 12, 2024
@lahodaj
Copy link
Contributor Author

lahodaj commented Aug 14, 2024

/integrate

@openjdk
Copy link

openjdk bot commented Aug 14, 2024

Going to push as commit fbe4f05.
Since your change was applied there have been 148 commits pushed to the master branch:

  • 38bd8a3: 8338236: Compile error in cgroup code on Linux when using clang
  • 9fe1777: 8338280: Parallel: Inline ParallelCompactData::verify_clear
  • 66bee25: 8338315: G1: G1CardTableEntryClosure:do_card_ptr remove unused parameter worker_id
  • 3dd07b9: 8336163: Remove declarations of some debug-only methods in release build
  • 720b446: 8335181: Incorrect handling of HTTP/2 GOAWAY frames in HttpClient
  • f132b34: 8336854: CAInterop.java#actalisauthenticationrootca conflicted with /manual and /timeout
  • 90527a5: 8336742: Shenandoah: Add more verbose logging/stats for mark termination attempts
  • 8e682ac: 8338158: Cleanup ShouldNotXXX uses in machnode.cpp
  • ca99f37: 8338156: Fix -Wzero-as-null-pointer-constant warnings in jvmciCompilerToVM.cpp
  • 84c3065: 8335865: Shenandoah: Improve THP pretouch after JDK-8315923
  • ... and 138 more: https://git.openjdk.org/jdk/compare/9b428dda8fb86ed595b05f3c930b3ce9c341db9b...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 14, 2024
@openjdk openjdk bot closed this Aug 14, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 14, 2024
@openjdk
Copy link

openjdk bot commented Aug 14, 2024

@lahodaj Pushed as commit fbe4f05.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

2 participants