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

8296171: Compiler incorrectly rejects code with variadic method references #11093

Closed

Conversation

vicente-romero-oracle
Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle commented Nov 10, 2022

Please review this PR that is syncing javac with the spec. Javac is rejecting this code:

import java.util.function.*;
interface Intf {
    Object apply(String... args);
}
                
public class Test {
    public static Object foo(Object o) { return "bar"; }
    public final Object foo(Object... o) { return "foo"; }
                
    public void test() {
        Intf f = this::foo;
    }
}

javac indicates that the method reference is invalid when according to the spec the second foo method should be selected by the compiler. The related section of the spec can be found at section 15.13.1 Compile-Time Declaration of a Method Reference:

  – For all other forms of method reference expression, one search for a most
  specific applicable method is performed. The search is as specified in
  §15.12.2.2 through §15.12.2.5, with the clarifications below.
  The method reference is treated as if it were an invocation with argument
  expressions of types P 1 , ..., P n ; the type arguments, if any, are given by the
  method reference expression.
  If the search results in an error as specified in §15.12.2.2 through §15.12.2.5,
  or if the most specific applicable method is static , there is no compile-time
  declaration.
  Otherwise, the compile-time declaration is the most specific applicable
  method.

Actually the method search is correct but later on javac is seeing that both methods are applicable and given that they have different staticness, it is making wrong assumptions down the road.

TIA


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-8296171: Compiler incorrectly rejects code with variadic method references

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11093

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

Using diff file

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

Sorry, something went wrong.

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 10, 2022

👋 Welcome back vromero! 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 Nov 10, 2022

@vicente-romero-oracle 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 Nov 10, 2022
Copy link
Contributor

@mcimadamore mcimadamore left a comment

Choose a reason for hiding this comment

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

Looks good - watch out for whitespaces

@@ -3111,7 +3111,8 @@ Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(Env<AttrContext> env,
boundSearchResolveContext.methodCheck = methodCheck;
Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(),
site.tsym, boundSearchResolveContext, boundLookupHelper);
ReferenceLookupResult boundRes = new ReferenceLookupResult(boundSym, boundSearchResolveContext);
boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good minimal fix I believe.
Moving forward (e.g. maybe in a separate PR) I wonder if it would be useful to make this code less general. E.g. the method reference lookup code comes from a time where we always did two lookups - but the JL:S has later been rectified, so that now we only do two lookups for unbound method references (e.g. those with static qualifier). I think that the current code is not the best in terms of achieving good JLS parity w/ a good readability, because it's trying to do too much. But of course that's an issue that was there before your fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the review. Yes I agree that a follow up issue should be filed to track this [1]

[1] https://bugs.openjdk.org/browse/JDK-8296948

@openjdk
Copy link

openjdk bot commented Nov 14, 2022

@vicente-romero-oracle 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:

8296171: Compiler incorrectly rejects code with variadic method references

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

  • 749335d: 8291911: java/io/File/GetXSpace.java fails with "53687091200 != 161051996160"
  • 95b8405: 8296431: PushbackInputStream should override transferTo
  • e269dc0: 8293681: ResponseAPDU getData() method javadoc
  • 8c472e4: 8294217: Assertion failure: parsing found no loops but there are some
  • 0fe2bf5: 8296805: ctw build is broken
  • e1d298c: 8296741: Illegal X400Address and EDIPartyName should not be created
  • b0edfc1: 8164464: Consistent failure of java/awt/dnd/MissingEventsOnModalDialog/MissingEventsOnModalDialogTest.java
  • 9c39932: 8294899: Process.waitFor() throws IllegalThreadStateException when a process on Windows returns an exit code of 259
  • 3f401b3: 8296670: G1: Remove unused G1GCPhaseTimes::record_preserve_cm_referents_time_ms
  • 68301cd: 8296665: IGV: Show dialog with stack trace for exceptions
  • ... and 29 more: https://git.openjdk.org/jdk/compare/4a68210d9f6c59ec4289b2e2412a1ae0df17fd81...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 ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 14, 2022
@vicente-romero-oracle
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 14, 2022

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

  • 749335d: 8291911: java/io/File/GetXSpace.java fails with "53687091200 != 161051996160"
  • 95b8405: 8296431: PushbackInputStream should override transferTo
  • e269dc0: 8293681: ResponseAPDU getData() method javadoc
  • 8c472e4: 8294217: Assertion failure: parsing found no loops but there are some
  • 0fe2bf5: 8296805: ctw build is broken
  • e1d298c: 8296741: Illegal X400Address and EDIPartyName should not be created
  • b0edfc1: 8164464: Consistent failure of java/awt/dnd/MissingEventsOnModalDialog/MissingEventsOnModalDialogTest.java
  • 9c39932: 8294899: Process.waitFor() throws IllegalThreadStateException when a process on Windows returns an exit code of 259
  • 3f401b3: 8296670: G1: Remove unused G1GCPhaseTimes::record_preserve_cm_referents_time_ms
  • 68301cd: 8296665: IGV: Show dialog with stack trace for exceptions
  • ... and 29 more: https://git.openjdk.org/jdk/compare/4a68210d9f6c59ec4289b2e2412a1ae0df17fd81...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Nov 14, 2022

@vicente-romero-oracle Pushed as commit 3eb789a.

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

@vicente-romero-oracle vicente-romero-oracle deleted the JDK-8296171 branch November 14, 2022 18:32
@mlbridge
Copy link

mlbridge bot commented Nov 14, 2022

Webrevs

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.

None yet

2 participants