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

javac allows local variables to be accessed from a static context

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 17
    • 16
    • tools
    • b23

    Description

      Compiling the following fragment with JDK 16 yields a NPE.

      public class LocalClasses {
          public static void main(String[] args) {
              int i = 5;
              class Local {
                  static void m() {
                      System.out.println("Value of i = " + i);
                  }
              }
              Local.m();
          }
      }

      NPE--
      An exception has occurred in the compiler (16-ea). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
      java.lang.NullPointerException: Cannot read field "sym" because "this.lvar[0]" is null
      at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop0(Code.java:568)
      at jdk.compiler/com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:369)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2257)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2407)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:864)
      at jdk.compiler/com.sun.tools.javac.jvm.StringConcat$IndyConstants.emit(StringConcat.java:444)
      at jdk.compiler/com.sun.tools.javac.jvm.StringConcat$Indy.makeConcat(StringConcat.java:275)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBinary(Gen.java:2122)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBinary.accept(JCTree.java:2104)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:864)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:889)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitApply(Gen.java:1855)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1745)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:864)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitExec(Gen.java:1723)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1532)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:597)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:632)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:618)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:669)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1084)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1047)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:597)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:632)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:954)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:917)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:893)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:597)
      at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2395)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:756)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1644)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1612)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:973)
      at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)
      at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
      at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
      at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)

      The compiler should fail with a compilation error as local variable `i` is being accessed from a static context

      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              mtrudeau Michel Trudeau
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: