Last Comment Bug 383902 - No "... is read-only" strict warning for assignment to declared const
: No "... is read-only" strict warning for assignment to declared const
: testcase
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: Trunk
: All All
: -- normal (vote)
: ---
Assigned To: Brendan Eich [:brendan]
: Jason Orendorff [:jorendorff]
Depends on:
Blocks: 296661 611388
  Show dependency treegraph
Reported: 2007-06-09 22:30 PDT by Jesse Ruderman
Modified: 2011-06-13 10:58 PDT (History)
6 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

fix (2.65 KB, patch)
2011-06-01 17:08 PDT, Brendan Eich [:brendan]
no flags Details | Diff | Splinter Review
rebase, hoist var c so it's a declared global, fix test indentation (2.63 KB, patch)
2011-06-05 11:36 PDT, Brendan Eich [:brendan]
jwalden+bmo: review+
Details | Diff | Splinter Review

Description Jesse Ruderman 2007-06-09 22:30:39 PDT
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).
Comment 1 Jesse Ruderman 2007-06-09 22:33:26 PDT
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.
Comment 2 Jesse Ruderman 2007-06-09 22:40:02 PDT
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.
Comment 3 Jesse Ruderman 2007-06-09 22:45:21 PDT
I wonder if the "eliminate assignment to const" optimization will survive the fixing of bug 229756 at all.
Comment 4 Brian Crowder 2007-06-12 13:32:00 PDT
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.
Comment 5 Jesse Ruderman 2007-06-12 14:14:36 PDT
That's the wrong warning, and it's completely incorrect.  Have you updated since the patch for bug 383674 went in?
Comment 6 Brendan Eich [:brendan] 2007-06-12 14:46:34 PDT
This bug should really just depend on bug 229756, which is the real bug to fix.

Comment 7 Jesse Ruderman 2007-06-12 16:44:10 PDT
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; })()
Comment 8 Brendan Eich [:brendan] 2011-05-31 15:30:56 PDT
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.

Comment 9 Brendan Eich [:brendan] 2011-06-01 17:08:04 PDT
Created attachment 536794 [details] [diff] [review]
Comment 10 Brendan Eich [:brendan] 2011-06-05 11:36:26 PDT
Created attachment 537466 [details] [diff] [review]
rebase, hoist var c so it's a declared global, fix test indentation

Waldo, this is still tiny -- any chance of review by tomorrow?

Comment 11 Jeff Walden [:Waldo] (remove +bmo to email) 2011-06-06 17:53:44 PDT
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]:
Comment 12 Brendan Eich [:brendan] 2011-06-07 16:25:28 PDT

Comment 13 Chris Leary [:cdleary] (not checking bugmail) 2011-06-13 10:58:52 PDT
cdleary-bot mozilla-central merge info:

Note You need to log in before you can comment on or make changes to this bug.