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

Maurizio Cimadamore mcimadamore at openjdk.org
Fri May 3 10:27:53 UTC 2024


On Thu, 2 May 2024 10:58:01 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> 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.

Maybe we should also set `SYNTHETIC` in `synthetizeEmptyInterfaceIfMissing` ? That method was mostly added so that a "real" type was available to javac. That said, if you didn't want to mess with that I understand, but the general approach seem to remain valid - e.g. in principle we should be able to wrap the `IO`'s symbol completer, so that if a completion error occurs, the error is swallowed (and then we know the type doesn't exist). In other words, a deferred attribution seems an heavy way to see if a type is there?

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

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


More information about the compiler-dev mailing list