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

java.lang.AssertionError when compiling JDK 1.4 code in JDK 8

    Details

    • Subcomponent:
    • Resolved In Build:
      b01

      Backports

        Description

        I am able to reproduce the issue using following sample code:

        TestEnum.java
        --------------------------------------------
        import java.util.Enumeration;
        import java.util.Hashtable;
        public class TestEnum {
                public static void main(String[] args) {
                        Hashtable hTable = new Hashtable();
                        hTable.put("hello", "value");
                        for (Enumeration enum = hTable.keys();;){
                                if(!enum.hasMoreElements())
                                        break;
                                enum.nextElement();
                        }
                }
        }
        --------------------------------------------
        Compile using "-source 1.4" option
        This code compiles fine with jdk6 but fails with error with jdk8

          Issue Links

            Activity

            Hide
            rcalnan Roger Calnan added a comment -
            Ask submitter to try 8u40
            Show
            rcalnan Roger Calnan added a comment - Ask submitter to try 8u40
            Hide
            rcalnan Roger Calnan added a comment -
            Code fails to compile with 8u40

            rscalnan$ java -version
            java version "1.8.0_40-ea"
            Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b21)
            Java HotSpot(TM) 64-Bit Server VM (build 25.40-b24, mixed mode)
            rscalnan$ javac -source 1.4 TestEnum.java
            warning: [options] bootstrap class path not set in conjunction with -source 1.4
            warning: [options] source value 1.4 is obsolete and will be removed in a future release
            warning: [options] target value 1.4 is obsolete and will be removed in a future release
            warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
            TestEnum.java:7: warning: as of release 5, 'enum' is a keyword, and may not be used as an identifier
                             for (Enumeration enum = hTable.keys();;){
                                              ^
              (use -source 5 or higher to use 'enum' as a keyword)
            TestEnum.java:8: warning: as of release 5, 'enum' is a keyword, and may not be used as an identifier
                                     if(!enum.hasMoreElements())
                                         ^
              (use -source 5 or higher to use 'enum' as a keyword)
            TestEnum.java:10: warning: as of release 5, 'enum' is a keyword, and may not be used as an identifier
                                     enum.nextElement();
                                     ^
              (use -source 5 or higher to use 'enum' as a keyword)
            An exception has occurred in the compiler (1.8.0_40-ea). 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.AssertionError
            at com.sun.tools.javac.util.Assert.error(Assert.java:126)
            at com.sun.tools.javac.util.Assert.check(Assert.java:45)
            at com.sun.tools.javac.comp.Attr.attribIdentAsEnumType(Attr.java:821)
            at com.sun.tools.javac.comp.MemberEnter.visitVarDef(MemberEnter.java:647)
            at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
            at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:437)
            at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1015)
            at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
            at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
            at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
            at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651)
            at com.sun.tools.javac.comp.Attr.visitForLoop(Attr.java:1129)
            at com.sun.tools.javac.tree.JCTree$JCForLoop.accept(JCTree.java:1000)
            at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
            at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
            at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651)
            at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1105)
            at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
            at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
            at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
            at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:994)
            at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
            at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
            at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
            at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4330)
            at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4240)
            at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4169)
            at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4144)
            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)
            Show
            rcalnan Roger Calnan added a comment - Code fails to compile with 8u40 rscalnan$ java -version java version "1.8.0_40-ea" Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b21) Java HotSpot(TM) 64-Bit Server VM (build 25.40-b24, mixed mode) rscalnan$ javac -source 1.4 TestEnum.java warning: [options] bootstrap class path not set in conjunction with -source 1.4 warning: [options] source value 1.4 is obsolete and will be removed in a future release warning: [options] target value 1.4 is obsolete and will be removed in a future release warning: [options] To suppress warnings about obsolete options, use -Xlint:-options. TestEnum.java:7: warning: as of release 5, 'enum' is a keyword, and may not be used as an identifier                  for (Enumeration enum = hTable.keys();;){                                   ^   (use -source 5 or higher to use 'enum' as a keyword) TestEnum.java:8: warning: as of release 5, 'enum' is a keyword, and may not be used as an identifier                          if(!enum.hasMoreElements())                              ^   (use -source 5 or higher to use 'enum' as a keyword) TestEnum.java:10: warning: as of release 5, 'enum' is a keyword, and may not be used as an identifier                          enum.nextElement();                          ^   (use -source 5 or higher to use 'enum' as a keyword) An exception has occurred in the compiler (1.8.0_40-ea). 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.AssertionError at com.sun.tools.javac.util.Assert.error(Assert.java:126) at com.sun.tools.javac.util.Assert.check(Assert.java:45) at com.sun.tools.javac.comp.Attr.attribIdentAsEnumType(Attr.java:821) at com.sun.tools.javac.comp.MemberEnter.visitVarDef(MemberEnter.java:647) at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852) at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:437) at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1015) at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635) at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651) at com.sun.tools.javac.comp.Attr.visitForLoop(Attr.java:1129) at com.sun.tools.javac.tree.JCTree$JCForLoop.accept(JCTree.java:1000) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635) at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651) at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1105) at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635) at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:994) at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635) at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4330) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4240) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4169) at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4144) 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)
            Hide
            mcimadamore Maurizio Cimadamore added a comment - - edited
            The problem has always been latent in javac code; code like:

            for (Foo enum = null : ... ) {
            ...
            }

            Has always been parsed slightly incorrectly, as the parser erroneously added the 'ENUM' flag to the 'enum' variable because of a bogus call to modifiersOpt() which was likely meant to return an empty list of modifiers; unfortunately if the variable is named 'enum' modifersOpt() is not a no-op, and results in surprising behavior.

            This behavior has been harmless until recently, where a fix for type-checking enum constants was added (JDK-7160084). Since that fix relies heavily on ENUM flags, the above parser bug started to cause spurious javac crashes (as javac erroneously thinks that a variable called 'enum' was an enum constant declaration).

            Also, since JDK 9 javac dropped support for JDK 1.4 features, this problem is no longer reproducible on latest JDK 9 branch. Since 'enum' can never be a valid variable name in JDK 9 compiler, the parser can be considered correct there, and no fix is needed in JDK 9.

            All JDKs prior to 9 in which a fix for JDK-7160084 has been backported are prone to this failure mode; according to the info in JBS, that means JDK 7 and 8.
            Show
            mcimadamore Maurizio Cimadamore added a comment - - edited The problem has always been latent in javac code; code like: for (Foo enum = null : ... ) { ... } Has always been parsed slightly incorrectly, as the parser erroneously added the 'ENUM' flag to the 'enum' variable because of a bogus call to modifiersOpt() which was likely meant to return an empty list of modifiers; unfortunately if the variable is named 'enum' modifersOpt() is not a no-op, and results in surprising behavior. This behavior has been harmless until recently, where a fix for type-checking enum constants was added ( JDK-7160084 ). Since that fix relies heavily on ENUM flags, the above parser bug started to cause spurious javac crashes (as javac erroneously thinks that a variable called 'enum' was an enum constant declaration). Also, since JDK 9 javac dropped support for JDK 1.4 features, this problem is no longer reproducible on latest JDK 9 branch. Since 'enum' can never be a valid variable name in JDK 9 compiler, the parser can be considered correct there, and no fix is needed in JDK 9. All JDKs prior to 9 in which a fix for JDK-7160084 has been backported are prone to this failure mode; according to the info in JBS, that means JDK 7 and 8.
            Hide
            mcimadamore Maurizio Cimadamore added a comment - - edited
            Confirmed also with JDK 7 b65 (default on my Ubuntu setup).
            Show
            mcimadamore Maurizio Cimadamore added a comment - - edited Confirmed also with JDK 7 b65 (default on my Ubuntu setup).
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk8u/jdk8u-dev/langtools/rev/584566b6d5e4
            User: mcimadamore
            Date: 2015-01-21 10:43:35 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk8u/jdk8u-dev/langtools/rev/584566b6d5e4 User: mcimadamore Date: 2015-01-21 10:43:35 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/rev/584566b6d5e4
            User: lana
            Date: 2015-01-26 23:01:48 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/rev/584566b6d5e4 User: lana Date: 2015-01-26 23:01:48 +0000
            Hide
            akosarev Artem Kosarev added a comment -
            Label escape-old is added, since bug which cause problem (JDK-7160084), was introduced in JDK 8. (See Maurizio's comment: "This behavior has been harmless until recently, where a fix for type-checking enum constants was added (JDK-7160084).")
            Show
            akosarev Artem Kosarev added a comment - Label escape-old is added, since bug which cause problem ( JDK-7160084 ), was introduced in JDK 8. (See Maurizio's comment: "This behavior has been harmless until recently, where a fix for type-checking enum constants was added ( JDK-7160084 ).")

              People

              • Assignee:
                mcimadamore Maurizio Cimadamore
                Reporter:
                shadowbug Shadow Bug
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: