Integrated: 8347989: Trees.getScope may crash for not-yet attributed source

Jan Lahoda jlahoda at openjdk.org
Fri Jan 31 07:58:50 UTC 2025


On Fri, 17 Jan 2025 18:21:54 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider code like this:
> 
> class Test {
>     private int test(boolean b) {
>         int v = b ? test(!b) : 0;
>         return v;
>     }
> }
> 
> 
> and consider `Trees.getScope` being called for a `TreePath` corresponding to "return" after the "enter" phase, but before `Attr`. E.g. from an annotation processor. This will lead to an exception like:
> 
> 
> Caused by: java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Type.accept(com.sun.tools.javac.code.Type$Visitor, Object)" because "t" is null
> at jdk.compiler/com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4936)
> at jdk.compiler/com.sun.tools.javac.code.Types.memberType(Types.java:2306)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.isBooleanOrNumeric(Attr.java:2133)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.isBooleanOrNumeric(Attr.java:2122)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.visitConditional(Attr.java:2060)
> 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)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:723)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1320)
> at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1066)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:677)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStat(Attr.java:751)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStats(Attr.java:770)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1454)
> at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1136)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:677)
> at jdk.compiler/com.sun.tools.javac.comp.DeferredAttr.attribSpeculative(DeferredAttr.java:515)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribToTree(Attr.java:428)
> at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStatToTree(Attr.java:421)
> at jdk.compiler/com.sun.tools.javac.api.JavacTrees.attribStatToTree(JavacTrees.java:882)
> at jdk.compiler/com.sun.tools.javac.api.JavacTrees.getAttrContext(JavacTrees.java:857)
> at jdk.compiler/com.sun.tools.javac.api.JavacTrees.getScope(JavacTrees.java:723)
> at jdk.compiler/com.sun.tools.javac.api.JavacTrees.getScope(JavacTrees.java:159)
> 
> 
> The reason is that the `Attr.isBooleanOrNumeric` in this case will refer to `env.encClass.type`, but the type of the enclosing class is filled at the end of `Attr.visitCl...

This pull request has now been integrated.

Changeset: 5a45de5e
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/5a45de5e1ec5ab3e6ed1f5cefa7b320353bb523f
Stats:     91 lines in 2 files changed: 87 ins; 0 del; 4 mod

8347989: Trees.getScope may crash for not-yet attributed source

Reviewed-by: asotona

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

PR: https://git.openjdk.org/jdk/pull/23179


More information about the compiler-dev mailing list