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

NPE in Scope.includes with exception inference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8u20, 8u40
    • tools
    • x86_64
    • linux_ubuntu

      FULL PRODUCT VERSION :
      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux 3.13.0-35-generic #62-Ubuntu SMP Fri Aug 15 01:58:42 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      The compiler exits with a NullPointerException for the provided source file, unless an exception type parameter is provided explicitly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Save the provided source as ExceptionInference.java
      2. javac ExceptionInference.java
      3. Apply workaround.
      4. Code compiles (and runs) successfully.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Normal compilation, ending either in errors or successful compilation.
      ACTUAL -
      NullPointerException during compilation.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      An exception has occurred in the compiler (1.8.0_20). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
      java.lang.NullPointerException
      at com.sun.tools.javac.code.Scope.includes(Scope.java:296)
      at com.sun.tools.javac.comp.Flow$1.trackable(Flow.java:247)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.visitVarDef(Flow.java:1832)
      at com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitVarDef(Flow.java:2569)
      at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
      at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scan(Flow.java:1376)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.visitLambda(Flow.java:2256)
      at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624)
      at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scan(Flow.java:1376)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scanExpr(Flow.java:1627)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scanExprs(Flow.java:1639)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.visitApply(Flow.java:2236)
      at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
      at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scan(Flow.java:1376)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.analyzeTree(Flow.java:2423)
      at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.analyzeTree(Flow.java:2406)
      at com.sun.tools.javac.comp.Flow.analyzeLambdaThrownTypes(Flow.java:250)
      at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2423)
      at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.DeferredAttr$2.complete(DeferredAttr.java:284)
      at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:245)
      at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:232)
      at com.sun.tools.javac.comp.Resolve$MethodResultInfo.check(Resolve.java:993)
      at com.sun.tools.javac.comp.Resolve$4.checkArg(Resolve.java:826)
      at com.sun.tools.javac.comp.Resolve$AbstractMethodCheck.argumentsAcceptable(Resolve.java:731)
      at com.sun.tools.javac.comp.Resolve$4.argumentsAcceptable(Resolve.java:835)
      at com.sun.tools.javac.comp.Infer.instantiateMethod(Infer.java:162)
      at com.sun.tools.javac.comp.Resolve.rawInstantiate(Resolve.java:564)
      at com.sun.tools.javac.comp.Resolve.checkMethod(Resolve.java:601)
      at com.sun.tools.javac.comp.Attr.checkMethod(Attr.java:3809)
      at com.sun.tools.javac.comp.Attr.checkIdInternal(Attr.java:3615)
      at com.sun.tools.javac.comp.Attr.checkMethodIdInternal(Attr.java:3522)
      at com.sun.tools.javac.comp.Attr.checkMethodId(Attr.java:3501)
      at com.sun.tools.javac.comp.Attr.checkId(Attr.java:3488)
      at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:3370)
      at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1897)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1843)
      at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:656)
      at com.sun.tools.javac.comp.Attr.visitExec(Attr.java:1611)
      at com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1296)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
      at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
      at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
      at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
      at com.sun.tools.javac.comp.Attr.visitTry(Attr.java:1372)
      at com.sun.tools.javac.tree.JCTree$JCTry.accept(JCTree.java:1173)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
      at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
      at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
      at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
      at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1035)
      at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
      at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
      at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
      at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4342)
      at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4252)
      at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4181)
      at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4156)
      at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
      at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
      at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
      at com.sun.tools.javac.main.Main.compile(Main.java:523)
      at com.sun.tools.javac.main.Main.compile(Main.java:381)
      at com.sun.tools.javac.main.Main.compile(Main.java:370)
      at com.sun.tools.javac.main.Main.compile(Main.java:361)
      at com.sun.tools.javac.Main.compile(Main.java:56)
      at com.sun.tools.javac.Main.main(Main.java:42)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;

      public class ExceptionInference {
          public static void main(String[] args) {
              try {
                  Either.left("This will explode!").with(s -> { throw new IOException(s); }, i -> System.out.println("" + i));
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }

          public interface Either<T, U> {

              static <T, U> Either<T, U> left(T t) {
                  return new Either<T, U>() {
                      @Override
                      public <R, E extends Exception> R map(FunctionE<T, R, E> withLeft, FunctionE<U, R, E> withRight) throws E {
                          return withLeft.apply(t);
                      }
                      @Override
                      public <E extends Exception> void with(ConsumerE<T, E> withLeft, ConsumerE<U, E> withRight) throws E {
                          withLeft.accept(t);
                      }
                  };
              }

              static <T, U> Either<T, U> right(U u) {
                  return new Either<T, U>() {
                      @Override
                      public <R, E extends Exception> R map(FunctionE<T, R, E> withLeft, FunctionE<U, R, E> withRight) throws E {
                          return withRight.apply(u);
                      }
                      @Override
                      public <E extends Exception> void with(ConsumerE<T, E> withLeft, ConsumerE<U, E> withRight) throws E {
                          withRight.accept(u);
                      }
                  };
              }

              <R, E extends Exception> R map(FunctionE<T, R, E> withLeft, FunctionE<U, R, E> withRight) throws E;

              <E extends Exception> void with(ConsumerE<T, E> withLeft, ConsumerE<U, E> withRight) throws E;
          }

          public interface ConsumerE<T, E extends Exception> {
              void accept(T t) throws E;
          }
          public interface FunctionE<T, R, E extends Exception> {
              R apply(T elt) throws E;
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Explicitly specifying the exception type parameter allows compilations to complete successfully. That is, change `.with(...)` to `.<IOException>with(...)`, and there is no error.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: