Closed Bug 1138736 Opened 8 years ago Closed 7 years ago

Invalid const binding reassignment in for loop's "next" expression should be TypeError, not Syntax Error

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 1215341

People

(Reporter: rwaldron, Unassigned)

Details

The following produces a runtime error, not an early error: 

  for (const i = 0; i < 1; i++) { }


Actual: SyntaxError 

Expected: TypeError 



https://people.mozilla.org/~jorendorff/es6-draft.html#sec-for-statement-runtime-semantics-labelledevaluation

Under: 

IterationStatement : for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement

Step 5.a.1
I think this bug goes deeper than IterationStatements. For instance, SpiderMonkey also incorrectly throws a static SyntaxError when a constant reference appears as the left-hand side of an AssignmentExpression (in strict mode):

    $ js -s -e "const x = 1; x = 2;"
    -e:1:13 SyntaxError: invalid assignment to const x:
    -e:1:13 const x = 1; x = 2;
    -e:1:13 .............^

12.14.4 (AssignmentExpression) Runtime Semantics: Evaluation [1]:

> 1.f. Let status be PutValue(lref, rval).

6.2.3.2 PutValue (V, W) [2]:

> 7.a. Return base.SetMutableBinding(GetReferencedName(V), W, IsStrictReference(V))

8.1.1.1.5 SetMutableBinding (N,V,S) [3]:

> 6. Else this must be an attempt to change the value of an immutable
>    binding so if S is true throw a TypeError exception.

This behavior extends to non-strict (or "sloppy") mode although in that case, no error should be thrown at all.

Should this bug be re-named? Or a new bug created?

[1] https://people.mozilla.org/~jorendorff/es6-draft.html#sec-assignment-operators-runtime-semantics-evaluation
[2] https://people.mozilla.org/~jorendorff/es6-draft.html#sec-putvalue
[3] https://people.mozilla.org/~jorendorff/es6-draft.html#sec-declarative-environment-records-setmutablebinding-n-v-s
At one time the ES6 spec. defined all such assignments as "early errors".  However, we backed off that requirement and now all attempts to assign to a const (an immutable binding) are specified to be runtime TypeErrors.
These are TypeErrors now. Shu-yu Guo fixed it a couple weeks ago. Duping forward to the bug where it was fixed.
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 1215341
You need to log in before you can comment on or make changes to this bug.