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

Compiler crashes when casting exception in multi-catch block

XMLWordPrintable

    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.8.0_60-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_60-ea-b21)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b21, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Darwin 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      When a catch block catches multiple exceptions that implement the same interface and a (redundant) cast to the type of that interface is done within that block, the compiler crashes with the following trace:

      Information:Using javac 1.8.0_60-ea to compile java sources
      Information:java: An exception has occurred in the compiler (1.8.0_60-ea). Please file a bug at the Java Bug Database (http://bugreport.java.com/bugreport/) after checking the database for duplicates. Include your program and the following diagnostic in your report. Thank you.
      Information:java: java.lang.ClassCastException: com.sun.tools.javac.code.Type$UnionClassType cannot be cast to com.sun.tools.javac.code.Type$IntersectionClassType
      Information:java: at com.sun.tools.javac.code.Types$8.visitClassType(Types.java:1545)
      Information:java: at com.sun.tools.javac.code.Types$8.visitClassType(Types.java:1502)
      Information:java: at com.sun.tools.javac.code.Type$ClassType.accept(Type.java:778)
      Information:java: at com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4559)
      Information:java: at com.sun.tools.javac.code.Types.isCastable(Types.java:1493)
      Information:java: at com.sun.tools.javac.comp.Check.checkCastable(Check.java:568)
      Information:java: at com.sun.tools.javac.comp.Check.checkCastable(Check.java:565)
      Information:java: at com.sun.tools.javac.comp.Attr.visitTypeCast(Attr.java:3099)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCTypeCast.accept(JCTree.java:1814)
      Information:java: at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
      Information:java: at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:608)
      Information:java: at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1065)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
      Information:java: at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
      Information:java: at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651)
      Information:java: at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1114)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
      Information:java: at com.sun.tools.javac.comp.Attr.visitTry(Attr.java:1367)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCTry.accept(JCTree.java:1173)
      Information:java: at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1003)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
      Information:java: at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4341)
      Information:java: at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4249)
      Information:java: at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4178)
      Information:java: at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4153)
      Information:java: at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
      Information:java: at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
      Information:java: at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
      Information:java: at com.sun.tools.javac.main.Main.compile(Main.java:523)
      Information:java: at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
      Information:java: at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
      Information:java: at org.jetbrains.jps.javac.JavacMain.compile(JavacMain.java:168)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.compileJava(JavaBuilder.java:382)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.compile(JavaBuilder.java:296)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.doBuild(JavaBuilder.java:204)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.build(JavaBuilder.java:176)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1196)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:877)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:945)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:840)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:663)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:370)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:191)
      Information:java: at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:137)
      Information:java: at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:293)
      Information:java: at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:124)
      Information:java: at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:242)
      Information:java: at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
      Information:java: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      Information:java: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      Information:java: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      Information:java: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      Information:java: at java.lang.Thread.run(Thread.java:745)
      Information:java: Errors occurred while compiling module 'sub-war'
      Information:01-07-15 09:33 - Compilation completed with 1 error and 0 warnings in 2s 840ms
      Error:java: Compilation failed: internal java compiler error

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Minimal example to reproduce the problem.
      (Note the similarity with https://bugs.openjdk.java.net/browse/JDK-8071291)

      interface A { }
      class Exception1 extends Exception implements A { }
      class Exception2 extends Exception implements A { }

      class Test {
          void test(boolean cond) {
              try {
                  if (cond) {
                      throw new Exception1();
                  } else {
                      throw new Exception2();
                  }
              }
              catch (Exception1|Exception2 x) {
                  A a = (A) x;
              }
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected the code to be compiled without errors.
      ACTUAL -
      The compiler crashes and no .class is generated.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Information:Using javac 1.8.0_60-ea to compile java sources
      Information:java: An exception has occurred in the compiler (1.8.0_60-ea). Please file a bug at the Java Bug Database (http://bugreport.java.com/bugreport/) after checking the database for duplicates. Include your program and the following diagnostic in your report. Thank you.
      Information:java: java.lang.ClassCastException: com.sun.tools.javac.code.Type$UnionClassType cannot be cast to com.sun.tools.javac.code.Type$IntersectionClassType
      Information:java: at com.sun.tools.javac.code.Types$8.visitClassType(Types.java:1545)
      Information:java: at com.sun.tools.javac.code.Types$8.visitClassType(Types.java:1502)
      Information:java: at com.sun.tools.javac.code.Type$ClassType.accept(Type.java:778)
      Information:java: at com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4559)
      Information:java: at com.sun.tools.javac.code.Types.isCastable(Types.java:1493)
      Information:java: at com.sun.tools.javac.comp.Check.checkCastable(Check.java:568)
      Information:java: at com.sun.tools.javac.comp.Check.checkCastable(Check.java:565)
      Information:java: at com.sun.tools.javac.comp.Attr.visitTypeCast(Attr.java:3099)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCTypeCast.accept(JCTree.java:1814)
      Information:java: at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
      Information:java: at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:608)
      Information:java: at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1065)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
      Information:java: at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
      Information:java: at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651)
      Information:java: at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1114)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
      Information:java: at com.sun.tools.javac.comp.Attr.visitTry(Attr.java:1367)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCTry.accept(JCTree.java:1173)
      Information:java: at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1003)
      Information:java: at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
      Information:java: at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4341)
      Information:java: at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4249)
      Information:java: at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4178)
      Information:java: at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4153)
      Information:java: at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
      Information:java: at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
      Information:java: at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
      Information:java: at com.sun.tools.javac.main.Main.compile(Main.java:523)
      Information:java: at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
      Information:java: at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
      Information:java: at org.jetbrains.jps.javac.JavacMain.compile(JavacMain.java:168)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.compileJava(JavaBuilder.java:382)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.compile(JavaBuilder.java:296)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.doBuild(JavaBuilder.java:204)
      Information:java: at org.jetbrains.jps.incremental.java.JavaBuilder.build(JavaBuilder.java:176)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1196)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:877)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:945)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:840)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:663)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:370)
      Information:java: at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:191)
      Information:java: at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:137)
      Information:java: at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:293)
      Information:java: at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:124)
      Information:java: at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:242)
      Information:java: at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
      Information:java: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      Information:java: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      Information:java: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      Information:java: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      Information:java: at java.lang.Thread.run(Thread.java:745)
      Information:java: Errors occurred while compiling module 'sub-war'
      Information:01-07-15 09:33 - Compilation completed with 1 error and 0 warnings in 2s 840ms
      Error:java: Compilation failed: internal java compiler error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      interface A { }
      class Exception1 extends Exception implements A { }
      class Exception2 extends Exception implements A { }

      class Test {
          void test(boolean cond) {
              try {
                  if (cond) {
                      throw new Exception1();
                  } else {
                      throw new Exception2();
                  }
              }
              catch (Exception1|Exception2 x) {
                  A a = (A) x;
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Avoid the cast.

        1. Test.java
          0.4 kB
          Pardeep Sharma

            pardesha Pardeep Sharma
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: