Closed Bug 383902 Opened 17 years ago Closed 13 years ago

No "... is read-only" strict warning for assignment to declared const


(Core :: JavaScript Engine, defect)

Not set





(Reporter: jruderman, Assigned: brendan)



(Keywords: testcase, Whiteboard: fixed-in-tracemonkey)


(1 file, 1 obsolete file)

js> const c;
js> c = 6;
typein:2: strict warning: c is read-only


js> function f(a) { const c; c = a + a; }
js> f(3);

I'm guessing this is because the assignment is optimized away in the latter case.  The code that optimizes it away should emit a compile-time warning similar to the existing run-time warning (using the same warning text would be fine).
Note that if the RHS of the assignment is simple, you can currently get a "useless expression" warning:

js> (function() { const c; c = 3 })()
typein:1: strict warning: useless expression

Ideally, this would trigger a "read-only" warning and *not* trigger a "useless expression" warning, because a "read-only" warning is easier to understand.
Crowder points out that my examples are wrong, because ES4 allows one-time assignment to a const if the const is not initialized.  See bug 229756.  "const c;" should be replaced with "const c = 1;" in each example.
I wonder if the "eliminate assignment to const" optimization will survive the fixing of bug 229756 at all.
Works for me on recent trunk:

js> function (a) { const c; c = a + a }
typein:5: strict warning: useless expression
function (a) {
    const c;

Not sure what changed.
Closed: 17 years ago
Resolution: --- → WORKSFORME
That's the wrong warning, and it's completely incorrect.  Have you updated since the patch for bug 383674 went in?
Resolution: WORKSFORME → ---
This bug should really just depend on bug 229756, which is the real bug to fix.

Depends on: 229756
Fixed examples:

(From comment 0) This should trigger a "read-only" warning, either at compile time or at run time:

function f(a) { const c = 1; c = a + a; }

(From comment 1) This should trigger a "read-only" warning and not trigger a "useless expression" warning:

(function() { const c = 1; c = 3; })()
Blocks: 296661
Depends on: 611388
No longer depends on: 229756
This bug is a stop-gap and tree-shaker in advance of the full fix for bug 611388, which depends on the const semantics being pinned down better (but they are likely to be unsurprising: no non-initializer assignment allowed, an initializer is required, early use in "temporal dead zone" is an error).

The patch for this bug is easy. Attaching next.

Assignee: general → brendan
OS: Mac OS X → All
Hardware: x86 → All
Summary: No "... is read-only" strict warning for assignment to local const → No "... is read-only" strict warning for assignment to declared const
Attached patch fix (obsolete) — Splinter Review
Attachment #536794 - Flags: review?(jwalden+bmo)
Blocks: 611388
No longer depends on: 611388
Waldo, this is still tiny -- any chance of review by tomorrow?

Attachment #536794 - Attachment is obsolete: true
Attachment #537466 - Flags: review?(jwalden+bmo)
Attachment #536794 - Flags: review?(jwalden+bmo)
Comment on attachment 537466 [details] [diff] [review]
rebase, hoist var c so it's a declared global, fix test indentation

Review of attachment 537466 [details] [diff] [review]:
Attachment #537466 - Flags: review?(jwalden+bmo) → review+

Whiteboard: fixed-in-tracemonkey
Closed: 17 years ago13 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.