JDK-8073707 : const re-assignment should not reported as a "early error"
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u60,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2015-02-24
  • Updated: 2015-09-29
  • Resolved: 2015-02-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u60Fixed 9 b54Fixed
Description
const x = 1;
print("hello world");
x = 3;


Should the output "hello world" be seen or not? Right now, nashorn treats this as "early error".

But, see this:

https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-11/nov-18.md#43-assignment-to-a-const-static-error

http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts

Has these lines

/ Assigning to a const declared binding throw, even in non-strict mode code.
Eliminated requirement to statically find and report assign to const declared names./

under section "December 6, 2014 Draft Rev 29"

So, it appears that the (as of now!)  const re-assignment is reported as TypeError - but not an early error.
Comments
I prepared this webrev: http://cr.openjdk.java.net/~sundar/8073707/ based on discussion, I came to know it does not cover local constants. For example, the following does not throw TypeError as expected: function func() { const x = 2; x = 4; } func();
24-02-2015

hg diff diff -r 7477f3456800 src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java --- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java Fri Feb 20 17:18:47 2015 +0100 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java Tue Feb 24 13:40:28 2015 +0530 @@ -713,11 +713,12 @@ } private void checkConstAssignment(final IdentNode ident) { + /* // Check for reassignment of constant final Symbol symbol = ident.getSymbol(); if (symbol.isConst()) { throwParserException(ECMAErrors.getMessage("syntax.error.assign.constant", symbol.getName()), ident); - } + }*/ } fixes this issue.
24-02-2015