RFR: 8261006: 'super' qualified method references cannot occur in a static context [v7]

Maurizio Cimadamore mcimadamore at openjdk.java.net
Thu Dec 2 12:00:41 UTC 2021


On Thu, 15 Jul 2021 03:20:48 GMT, Vicente Romero <vromero at openjdk.org> wrote:

>> Please review this PR, currently javac is accepting code like:
>> 
>> 
>> import java.util.function.Supplier;
>> 
>> public class MethodReferenceInConstructorInvocation {
>>     interface Bar {
>>         default String getString() { return ""; }
>>     }
>> 
>>     static class Foo implements Bar {
>>         public Foo() {  this(Bar.super::getString);  }
>>         public Foo(Supplier<String> sString) {}
>>     }
>> }
>> 
>> 
>> but the spec states in `15.13 Method Reference Expressions`:
>> 
>> If a method reference expression has the form super :: [TypeArguments] Identifier
>> or TypeName . super :: [TypeArguments] Identifier, it is a compile-time error if
>> the expression occurs in a static context (§8.1.3).
>> 
>> and a constructor invocation is a static context. So method references of this form, qualified by `super`, should be rejected by the compiler if they appear in a static context.
>> 
>> TIA
>
> Vicente Romero has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains seven additional commits since the last revision:
> 
>  - Merge branch 'master' into JDK-8261006
>  - Merge branch 'master' into JDK-8261006
>  - addressing review comments
>  - updating comment
>  - addressing review comments
>  - Merge branch 'master' into JDK-8261006
>  - 8261006: fail to parse broken interface::method in lambda

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 4342:

> 4340: 
> 4341:         if (isType(sitesym)) {
> 4342:             if (sym.name == names._this || sym.name == names._super) {

I suggest to rewrite this as:


if (sym.name == names._this) {
   if (site.tsym == env.enclClass.sym) {
      // error
   }
} else if (sym.name == names._super) {
   if (sitesym.isInterface() || site.tsym == env.enclClass.sym) {
      // error
   }
}

As the compound expression is giving me headache :-)

Note that I've dropped the `constructorArgs` check, because I think this is set in Attr::visitApply, and can be assumed to be true if `isSelfCall` is set.

-------------

PR: https://git.openjdk.java.net/jdk/pull/4376


More information about the compiler-dev mailing list