Closed Bug 383902 Opened 17 years ago Closed 13 years ago

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

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: jruderman, Assigned: brendan)

References

Details

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

Attachments

(1 file, 1 obsolete file)

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

But:

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.
Status: NEW → RESOLVED
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?
Status: RESOLVED → REOPENED
Resolution: WORKSFORME → ---
This bug should really just depend on bug 229756, which is the real bug to fix.

/be
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; }
f(3);

(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 ES.next/Harmony 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.

/be
Assignee: general → brendan
OS: Mac OS X → All
Hardware: x86 → All
Status: REOPENED → ASSIGNED
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?

/be
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+
http://hg.mozilla.org/tracemonkey/rev/8d21b9711d6b

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

Attachment

General

Created:
Updated:
Size: