jdk icon indicating copy to clipboard operation
jdk copied to clipboard

8295020: javac emits incorrect code for for-each on an intersection type.

Open sadayapalam opened this issue 3 years ago • 5 comments

Eliminate needless casts and ensure invoked method is looked up against the appropriate receiver type


Progress

  • [x] 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

Issue

  • JDK-8295020: javac emits incorrect code for for-each on an intersection type.

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10710

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

Using diff file

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

sadayapalam avatar Oct 14 '22 04:10 sadayapalam

:wave: Welcome back sadayapalam! 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 Oct 14 '22 04:10 bridgekeeper[bot]

Notes for the reviewer:

The loop from the test case viz:

for (Object s : (MyIterable & Serializable) null) {}

gets transformed by Lower into:

for (Iterator i$ = ((Iterable)(Main$MyIterable & Serializable)null).iterator(); i$.hasNext(); ) {
    Object s = (Object)i$.next();
    {
    }
}

This transformation is problematic in a couple of ways:

The cast of the expression (Main$MyIterable & Serializable)null into (Iterable) is not
required at all. After erasure the type of the intersection cast node is actually
Main$MyIterable which is already an Iterable. As a matter of Gen#visitTypeCast correctly
deduces this to be a redundant cast and does not emit a checkcast in the class file.

The iterator() method to be invoked is looked up against the JCEnhancedForLoop#expr.type
i.e from Main$MyIterable while the type of the receiver expression for the iterator()
method invocation is the casted type of (Iterable)(Main$MyIterable & Serializable)null).
As a result, the method
com.sun.tools.javac.jvm.Gen#binaryQualifier((Symbol sym, Type site)
attempts to coalesce the method iterator() (sym) as owned by Iterator (site) by calling
com.sun.tools.javac.code.Symbol.MethodSymbol#clone which simply changes ownership of
the method symbol iterator() without accommodating for descriptor difference that arises
due to covariant return type.

The fix comes in two parts: (a) Ensuring that a cast is inserted only when absolutely necessary and not just because JCEnhancedForLoop#expr.type happens to be an intersection type (b) Ensuring that where a type cast is inserted, the iterator() method is looked up against the modified receiver type.

(An open question is whether MethodSymbol#clone should be fixed to not just rewire ownership, but also account for differences in descriptor - if deemed needed this can be followed up in a separate ticket.)

sadayapalam avatar Oct 14 '22 04:10 sadayapalam

@sadayapalam 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[bot] avatar Oct 14 '22 04:10 openjdk[bot]

Webrevs

mlbridge[bot] avatar Oct 14 '22 04:10 mlbridge[bot]

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

8295020: javac emits incorrect code for for-each on an intersection type.

Reviewed-by: mcimadamore

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 1 new commit pushed to the master branch:

  • 74a51ccc86525eb4b1eb2e5cb11e605ca9e9fc5d: 8292698: Improve performance of DataInputStream

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch. 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[bot] avatar Oct 14 '22 10:10 openjdk[bot]

/integrate

sadayapalam avatar Oct 17 '22 07:10 sadayapalam

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

  • b3bb3e6ed89f3abcaae584fcbe75688141e886cb: 8295325: tools/jlink/plugins/SaveJlinkArgfilesPluginTest.java fails on Linux ppc64le
  • 9005af3b90fbd3607aeb83efe1c4a6ffa5d104f0: 8295110: RISC-V: Mark out relocations as incompressible
  • 74a51ccc86525eb4b1eb2e5cb11e605ca9e9fc5d: 8292698: Improve performance of DataInputStream

Your commit was automatically rebased without conflicts.

openjdk[bot] avatar Oct 17 '22 07:10 openjdk[bot]

@sadayapalam Pushed as commit cf07eaeb9291da725181832b8bb1dc54957ba886.

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

openjdk[bot] avatar Oct 17 '22 07:10 openjdk[bot]