RFR: 8278078: Cannot reference super before supertype constructor has been called [v4]

Maurizio Cimadamore mcimadamore at openjdk.java.net
Thu Dec 2 12:57:26 UTC 2021


On Thu, 2 Dec 2021 12:23:59 GMT, Adam Sotona <asotona at openjdk.org> wrote:

>> Pull request #4376 (with fix of 8261006: 'super' qualified method references cannot occur in a static context) regressed compilation of all inner classes using <enclosing class>.super pattern in their constructor argument to fail with: 
>>  error: cannot reference super before supertype constructor has been called 
>> 
>> For example following source fragment cannot be compiled since that: 
>> 
>>     class EnclClass { 
>>         class InnerClass extends Exception { 
>>             InnerClass() { 
>>                 super(EnclClass.super.toString()); 
>>             } 
>>         } 
>>     } 
>> 
>> 
>> This patch keeps throwing "cannot reference super" error for calls of <interface>.super and permits calls of <enclosing class>.super
>> 
>> Plus it adds a new test.
>> 
>> Thanks,
>> Adam
>
> Adam Sotona has updated the pull request incrementally with one additional commit since the last revision:
> 
>   aded copyright notice and method reference test

Marked as reviewed by mcimadamore (Reviewer).

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

> 4349:                         ((sym.name == names._this &&
> 4350:                         site.tsym == env.enclClass.sym) ||
> 4351:                         sym.name == names._super && env.info.constructorArgs &&

Maybe for later - let's refactor this to:

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


The `isSelfCall` should imply that `env.info.constructorArgs` is true, probably no need to check twice.

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

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


More information about the compiler-dev mailing list