jdk icon indicating copy to clipboard operation
jdk copied to clipboard

8328536: javac - crash on unknown type referenced in yield statement

Open SirYwell opened this issue 1 year ago • 10 comments

Pasting e.g.

I m(I i, int x) {
   return switch (x) {
        default -> i;
   };
}

in jshell will cause a crash if I is not declared already. This comes down to javac not creating an error type for the value of the (implicit) yield from the switch.

Javac will not crash but swallow the exception, and create a file containing the command line options.

I first thought about just checking for null of the type here https://github.com/openjdk/jdk/blob/9ca4ae3d3b746f1d75036d189ff98f02b73b948f/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#L1640 but after a closer look, the checkIdInternal method seems a better fit, as it also updates the type normally.


Progress

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

Warning

 ⚠️ Found leading lowercase letter in issue title for 8328536: javac - crash on unknown type referenced in yield statement

Issue

  • JDK-8328536: javac - crash on unknown type referenced in yield statement (Bug - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 18383

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

Using diff file

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

Webrev

Link to Webrev Comment

SirYwell avatar Mar 19 '24 17:03 SirYwell

:wave: Welcome back hgreule! 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.

bridgekeeper[bot] avatar Mar 19 '24 17:03 bridgekeeper[bot]

@SirYwell 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:

8328536: javac - crash on unknown type referenced in yield statement

Co-authored-by: Jan Lahoda <[email protected]>
Reviewed-by: jlahoda

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 219 new commits pushed to the master branch:

  • f23295ec1dde58d239a2625c9b1645534a2bb625: 8334600: TEST java/net/MulticastSocket/IPMulticastIF.java fails on linux-aarch64
  • 741a0f39dd1fffc1caaa8d69bfe3662dad830452: 8334241: Adjust API docs side bar dimensions
  • 4ce8822b6c53b8bd72713f1bfaf6673b91aabea4: 8334037: Local class creation in lambda in pre-construction context crashes javac
  • 7f6804ceb63568d72e825d45b02d08f314c9b0fc: 8334872: BigEndian: java/lang/invoke/condy Tests failing since JDK-8294960
  • e1390056c9dbf0a02a131864ebee23435e997852: 8333994: NMT: call stacks should show source information
  • b88af94269640a160fbacf25618f3a00756464aa: 8269870: PS: Membar in PSPromotionManager::copy_unmarked_to_survivor_space could be relaxed
  • a5f401f3a8534a64cf3c27c2ef67f17860de6d6b: 8334650: Add debug information about whether an Assertion Predicate is for the init or last value
  • 25c3845be270462388ee5e7330cc7315e5c738df: 8333133: Simplify QuickSort::sort
  • c66f785fb685d5c378fb4c4cdebdef29c01d321b: 8334505: RISC-V: Several tests fail when MaxVectorSize does not match VM_Version::_initial_vector_length
  • f101e153cee68750fcf1f12da10e29806875b522: 8333583: Crypto-XDH.generateSecret regression after JDK-8329538
  • ... and 209 more: https://git.openjdk.org/jdk/compare/75dc2f8518d0adea30f7065d6732b807c0220756...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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@lahodaj) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

openjdk[bot] avatar Mar 19 '24 17:03 openjdk[bot]

@SirYwell The following labels will be automatically applied to this pull request:

  • compiler
  • kulla

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

openjdk[bot] avatar Mar 19 '24 17:03 openjdk[bot]

Webrevs

mlbridge[bot] avatar Mar 19 '24 18:03 mlbridge[bot]

@SirYwell This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

bridgekeeper[bot] avatar Apr 25 '24 13:04 bridgekeeper[bot]

Hi, could someone please help review this change? Thanks.

SirYwell avatar May 13 '24 10:05 SirYwell

@vicente-romero-oracle Is it possible for you to quickly review this bugfix?

liach avatar May 14 '24 23:05 liach

I apologize for a belated reply. I agree with not adding a null check, that is usually a wrong/insufficient approach. And I think I agree with your approach, but I am not sure if it goes far enough.

I think I would try to go with something like:

diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
index d0f3ae7464a..4fe1b4430ec 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
@@ -4623,9 +4623,6 @@ Type checkIdInternal(JCTree tree,
                      Type pt,
                      Env<AttrContext> env,
                      ResultInfo resultInfo) {
-            if (pt.isErroneous()) {
-                return types.createErrorType(site);
-            }
             Type owntype; // The computed type of this identifier occurrence.
             switch (sym.kind) {
             case TYP:
@@ -4734,6 +4731,10 @@ else if (ownOuter.hasTag(CLASS) && site != ownOuter) {
                 chk.checkPreview(tree.pos(), env.info.scope.owner, sym);
             }
 
+            if (pt.isErroneous()) {
+                owntype = types.createErrorType(owntype);
+            }
+
             // If symbol is a variable, check that its type and
             // kind are compatible with the prototype and protokind.
             return check(tree, owntype, sym.kind.toSelector(), resultInfo);

I.e., let the checkIdInternal run as normally, just wrap the type with an error type. When I ran tests with this change, three failed for me:

FAILED: tools/javac/generics/diamond/7188968/T7188968.java
FAILED: tools/javac/lambda/methodReference/MethodRefToInnerWithoutOuter.java
FAILED: tools/javac/lambda/MethodReference23.java

For the tools/javac/lambda tests, the new errors seemed better/more appropriate than the original ones, so those are not a problem I think (but an independent check would be good).

The tools/javac/generics/diamond/7188968/T7188968.java is producing more unchecked warnings with that change, and I am not quite sure if that's appropriate.

As for a test - test inside JShell is good, but there should be a test inside somewhere tools/javac. A good place might be test/langtools/tools/javac/recovery/AttrRecovery.java:

    @Test
    public void testErroneousTarget() throws Exception {
        String code = """
                      public class C {
                          public Undefined g(Undefined u) {
                              return switch (0) {
                                  default -> u;
                              };
                          }
                      }
                      """;
        Path curPath = Path.of(".");
        List<String> actual = new JavacTask(tb)
                .options("-XDrawDiagnostics")
                .sources(code)
                .outdir(curPath)
                .run(Expect.FAIL, 1)
                .writeAll()
                .getOutputLines(OutputKind.DIRECT);

        List<String> expected = List.of(
                "C.java:2:24: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
                "C.java:2:12: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
                "2 errors"
        );

        if (!Objects.equals(actual, expected)) {
            error("Expected: " + expected + ", but got: " + actual);
        }
    }

(Note the explicit use of the expected exit code, which should guard against crashes. An alternative is to use -XDdev in the list of parameters, see other tests in the class - that should print the exception if it happens.)

lahodaj avatar May 16 '24 17:05 lahodaj

Thanks for the comment @lahodaj.

Not bailing out so early makes sense I guess. I also looked into the new warnings of T7188968 with your suggested changes. Adding a Object unknown = null; to make the code compile will cause the same warnings, so if they make sense with compiling code, I assume they also make sense with broken code.

Let me know if you want to go forward with your suggestion then. I will, however, most likely only find time to do the changes next week.

SirYwell avatar May 17 '24 07:05 SirYwell

We've discussed with @mcimadamore offline, and changing the output for T7188968 based on the change I've suggested yesterday is probably OK. I am pretty sure the output for the other two tests is better (more correct) with the suggested change. So, if possible, I would suggest to go with the that change, adjusting the tests.

Thanks!

lahodaj avatar May 17 '24 12:05 lahodaj

/contributor add @lahodaj

I adapted your suggested changes and adjusted the tests.

SirYwell avatar May 20 '24 14:05 SirYwell

@SirYwell Contributor Jan Lahoda <[email protected]> successfully added.

openjdk[bot] avatar May 20 '24 14:05 openjdk[bot]

@SirYwell this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout fix/switch-yield-unknown-type-crash
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

openjdk[bot] avatar Jun 06 '24 16:06 openjdk[bot]

Thanks for the review.

/integrate

SirYwell avatar Jun 26 '24 12:06 SirYwell

@SirYwell Your change (at version d45b717bb7e01c788f42040d4d712872e2817c6a) is now ready to be sponsored by a Committer.

openjdk[bot] avatar Jun 26 '24 12:06 openjdk[bot]

langtools tier 1+2 tests pass.

/sponsor

liach avatar Jun 26 '24 23:06 liach

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

  • 07bc523df85fde81bf736fedac62874d3cb11ee3: 8334670: SSLSocketOutputRecord buffer miscalculation
  • 4ebb77120af5a4ccbfde63b24cb50e05a3161f16: 8334769: Shenandoah: Move CodeCache_lock close to its use in ShenandoahConcurrentNMethodIterator
  • 817edcb697cbb8c608c9292cdc4b99db4f5844dc: 8331411: Shenandoah: Reconsider spinning duration in ShenandoahLock
  • bffc8484c32ad6c3205f7cebe4e262a2dc9de57e: 8333755: NumberFormat integer only parsing breaks when format has suffix
  • b5d589623c174757e946011495f771718318f1cc: 8335108: Build error after JDK-8333658 due to class templates
  • 5883a20b822bb8acb719076e4f7abee8403061cb: 8334437: De-duplicate ProxyMethod list creation
  • 8591eff78dbc9770b8d0a16e05040ac35c99881a: 8332103: since-checker - Add missing @ since tags to java.desktop
  • 8374d16504503c7441346c99045736b7ac72233f: 8335006: C2 SuperWord: add JMH benchmark VectorLoadToStoreForwarding.java
  • 4ffc5e60776353b03e9a557c39148e378b1690e2: 8326705: Test CertMsgCheck.java fails to find alert certificate_required
  • efb905e57ab7a5299952419fa9961316541056c2: 8334618: ubsan: support setting additional ubsan check options
  • ... and 220 more: https://git.openjdk.org/jdk/compare/75dc2f8518d0adea30f7065d6732b807c0220756...master

Your commit was automatically rebased without conflicts.

openjdk[bot] avatar Jun 26 '24 23:06 openjdk[bot]

@liach @SirYwell Pushed as commit 3796fdfcedc2b2202b72cca062218f840960414c.

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

openjdk[bot] avatar Jun 26 '24 23:06 openjdk[bot]