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

SyntaxError in constant folding with block scoped vars

    Details

    • Type: Bug
    • Status: New
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd_minor
    • Component/s: core-libs
    • Labels:
      None

      Description

      Reported on nashorn-dev:

      http://mail.openjdk.java.net/pipermail/nashorn-dev/2019-October/007514.html

      We have encountered another bug in Nashorn. It can be reproduced by evaluation of this script with "jjs --language=es6":

      if (true) { throw "test"; } { let x = "1"; } { let x = 2; }

      It results in compilation error:

      ECMAScript Exception: SyntaxError: <shell>:1:34 Variable "x" has already been declared
      if (true) { throw "test"; } { let x = "1"; } { let x = 2; }
                                        ^
      While expected out was to get runtime exception "test".

      Looks like the problem is in dead code elimination logic of FoldConstants.extractVarNodesFromDeadCode function.

      And here is the patch to fix it:

      diff -r 6e287efa5fa3 src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java
      --- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java Wed Oct 23 09:53:07 2019 +0200
      +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java Wed Oct 23 14:23:48 2019 +0300
      @@ -195,7 +195,9 @@
               deadCodeRoot.accept(new SimpleNodeVisitor() {
                   @Override
                   public boolean enterVarNode(final VarNode varNode) {
      - statements.add(varNode.setInit(null));
      + if (!varNode.isBlockScoped()) {
      + statements.add(varNode.setInit(null));
      + }
                       return false;
                   }

        Attachments

          Activity

            People

            • Assignee:
              hannesw Hannes Wallnoefer
              Reporter:
              hannesw Hannes Wallnoefer
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: