Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8347989

Trees.getScope may crash for not-yet attributed source

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 25
    • 24, 25
    • tools
    • None
    • b09

      Consider the following code:
      ---
      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 `JCClassDecl` for the class does not have a `type` filled during enter - it is only filled in `Attr`:
      https://github.com/openjdk/jdk/blob/f5573f5cbdcae5d1303c8b58d2946c168b977326/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#L985

      But, `isBooleanOrNumeric` relies on the `type` being filled.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: