RFR: 8347989: Trees.getScope may crash for not-yet attributed source
Adam Sotona
asotona at openjdk.org
Thu Jan 30 16:07:52 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...
Looks good to me.
-------------
Marked as reviewed by asotona (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/23179#pullrequestreview-2584348395
More information about the compiler-dev
mailing list