RFR: 8372336: javac fails with an exception when a class is missing while evaluating conditional expression

Vicente Romero vromero at openjdk.org
Tue Nov 25 16:10:24 UTC 2025


On Tue, 25 Nov 2025 12:21:36 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider library code like:
> 
> package test;
> public class Intermediate extends Base {}
> package test;
> public class Base {}
> 
> 
> this is compiled, but the `Base` is missing/deleted. Now consider use of this library:
> 
> package test;
> public class Test {
>     private void test(Intermediate i) {
>         int j = i != null ? i.get() : -1;
>     }
> }
> 
> 
> This fails with:
> 
> java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Symbol.flags()" because "sym" is null
>         at jdk.compiler/com.sun.tools.javac.code.Types.memberType(Types.java:2304)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.isBooleanOrNumeric(Attr.java:2143)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.isBooleanOrNumeric(Attr.java:2132)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.visitConditional(Attr.java:2070)
>         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1580)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:677)
> ...
> 
> 
> When attributing `i.get()`, there's a `CompletionFailure` thrown, for the missing `Base`. That is OK, but in this case, the `CompletionFailure` is caught by `Attr.attribTree`, but the problem is that the AST for the method invocation does not have the `Symbol`s filled in. But `isBooleanOrNumeric` relies on a non-`null` `Symbol`:
> https://github.com/openjdk/jdk/blob/49176e322bbb9ed1ef2f534b949b937770b54162/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#L2147
> 
> It would be possible to add a `null` check into `isBooleanOrNumeric`, but it seems more reliable to catch the `CompletionFailure` a bit sooner, and fill in the `Symbol` for the invocation. This is closer to what is being done in other similar cases.

lgtm, just a minor suggestion

test/langtools/tools/javac/recovery/AttrRecovery.java line 415:

> 413:                      """
> 414:                      package test;
> 415:                      public class Base {}

nit, would it make sense to actually add a `get` method to `Base`, it won't change the outcome but it will clear any confusion that the error is not related to the absence of this method

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

Marked as reviewed by vromero (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/28490#pullrequestreview-3505814323
PR Review Comment: https://git.openjdk.org/jdk/pull/28490#discussion_r2560576510


More information about the compiler-dev mailing list