RFR: 8325324: Implement Implicitly Declared Classes and Instance Main Methods (Third Preview) [v3]

Jan Lahoda jlahoda at openjdk.org
Thu May 2 11:00:55 UTC 2024


On Thu, 2 May 2024 10:15:03 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> Jan Lahoda has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains 14 commits:
>> 
>>  - Fixing typo.
>>  - Adjusting IO class name.
>>  - Merge branch 'module-imports' into JDK-8325324
>>  - Merge branch 'module-imports' into JDK-8325324
>>  - Merge branch 'module-imports' into JDK-8325324
>>  - Merge branch 'module-imports' into JDK-8325324
>>  - Adding test verifying that the new implicit imports are not used for ordinary classes.
>>  - Merge branch 'module-imports' into JDK-8325324
>>  - Fixing test.
>>  - Merge branch 'master' into JDK-8325324
>>  - ... and 4 more: https://git.openjdk.org/jdk/compare/43a1e0f6...a37f8d27
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java line 354:
> 
>> 352:         private boolean peekTypeExists(Env<AttrContext> env, TypeSymbol type) {
>> 353:             JCExpression expr = make.Select(make.QualIdent(type), names._class);
>> 354:             return !deferredAttr.attribSpeculative(expr, env, attr.unknownExprInfo)
> 
> Is this code meant to be transient?
> 
> Note that we have an handy method in Symtab called `synthesizeEmptyInterfaceIfMissing` which will lookup a type, then, if such a type is missing, it will replace it with an empty interface. Since IO is a class, perhaps we can just check if the symbol in the symbol table is an interface or not, and avoid deferred attribution?

It was meant to be permanent, even though now transiently it is more important. Long term, it is much less important for the implicit imports in implicit classes. The String Templates code was trying to detect if the `StringTemplate` class exists and avoid the implicit import if it didn't. That didn't work very well, and was causing issues in some corner cases. So, I used the speculative attribution, and used it for implicitly declared classes as well.

The reason why I used the speculative attribution is that it does not have side-effects - i.e. if the class does not exist, and the user types `java.io.IO` explicitly, there will still be an error. With `synthetizeEmptyInterfaceIfMissing`, there will be no error reported, even for explicit uses of the type, I believe.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18633#discussion_r1587445437


More information about the compiler-dev mailing list