RFR: 8347989: Trees.getScope may crash for not-yet attributed source
Jan Lahoda
jlahoda at openjdk.org
Fri Jan 17 18:27:05 UTC 2025
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.visitClassDef`. So, the `Trees.getScope` running before `Attr` cannot use it.
The proposal in this PR is to set the `JCClassDecl`'s type (also) in `Enter`.
-------------
Commit messages:
- Improving test.
- 8347989: Trees.getScope may crash for not-yet attributed source
Changes: https://git.openjdk.org/jdk/pull/23179/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23179&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8347989
Stats: 91 lines in 2 files changed: 87 ins; 0 del; 4 mod
Patch: https://git.openjdk.org/jdk/pull/23179.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/23179/head:pull/23179
PR: https://git.openjdk.org/jdk/pull/23179
More information about the compiler-dev
mailing list