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

Nashorn AST is missing nodes when a for-loop contains a VariableDeclarationList

    XMLWordPrintable

Details

    • b25
    • generic
    • generic

    Description

      FULL PRODUCT VERSION :
      java version "9"
      Java(TM) SE Runtime Environment (build 9+178)
      Java HotSpot(TM) 64-Bit Server VM (build 9+178, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      Putting a "var" declaration in a for-loop produces only part of the expected tree and ignores the rest of the script code.

      I have no clue about ECMA Script 6, but according to the spec 13.7.4.4 a VariableDeclarationList is a valid element in a for loop.

      The parser should never fail quietly. It should either give a full tree or throw an exception.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Parse this script and use jdk.nashorn.api.tree.SimpleTreeVisitorES6 to inspect the tree.

      for (var i = 0; i < c.length; i++) {
          console.log(c[i]);
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      0-61 jdk.nashorn.api.tree.ForLoopTreeImpl


      Note: this is the tree for a version without the "var"
      for (i = 0; i < c.length; i++) {
          console.log(c[i]);
      }

      The actual expected result should either have VARIABLE instead of ASSIGNMENT or show an ErroneousTree node.

      Tree:
      @0-0 COMPILATION_UNIT
       @0-57 FOR_LOOP for (i = 0; i < c.length; i++) { console.log(c[i]); }
        @5-10 ASSIGNMENT i = 0
         @5-6 IDENTIFIER i
         @9-10 NUMBER_LITERAL 0
        @12-24 LESS_THAN i < c.length
         @12-13 IDENTIFIER i
         @16-24 MEMBER_SELECT c.length
          @16-17 IDENTIFIER c
        @26-29 POSTFIX_INCREMENT i++
         @26-27 IDENTIFIER i
        @31-57 BLOCK { console.log(c[i]); }
         @37-54 EXPRESSION_STATEMENT console.log(c[i])
          @37-54 FUNCTION_INVOCATION console.log(c[i])
           @37-48 MEMBER_SELECT console.log
            @37-44 IDENTIFIER console
           @49-53 ARRAY_ACCESS c[i]
            @49-50 IDENTIFIER c
            @51-52 IDENTIFIER i

      ACTUAL -
      5-14 jdk.nashorn.api.tree.VariableTreeImpl

      Tree:
      @0-0 COMPILATION_UNIT
       @5-14 VARIABLE var i = 0
        @9-10 IDENTIFIER i
        @13-14 NUMBER_LITERAL 0

      No other nodes in CompilationUnit.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) {
      String script = "for (var i = 0; i < c.length; i++) {\n console.log(c[i]);\n}\n";
              DiagnosticListener listener = (Diagnostic diag) -> { System.err.println(diag.toString()); };

      Parser parser = Parser.create("--language=es6", "--empty-statements");
      Tree astRoot = parser.parse("unknown", script, listener);
      astRoot.accept(new SimpleTreeVisitorES6<Boolean, Void>() {
      @Override
      public Boolean visitCompilationUnit(CompilationUnitTree stmt, Void none) {
      for (Tree item : stmt.getSourceElements()) {
      System.out.println(item.getStartPosition() + "-" + item.getEndPosition() + " " + item.getClass().getName());
      }
      return super.visitCompilationUnit(stmt, none);
      }

      }, null);
      }

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

      Attachments

        Activity

          People

            sdama Srinivas Dama (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: