New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 596061 link

Starred by 3 users

Issue metadata

Status: WontFix
Owner:
Last visit > 30 days ago
Closed: Mar 2016
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Mac
Pri: 2
Type: Bug



Sign in to add a comment

ReferenceError during a const assignment leaves the variable in a half-declared state

Reported by m.go...@gmail.com, Mar 18 2016

Issue description

UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36

Steps to reproduce the problem:
const a = b; // Uncaught ReferenceError: b is not defined
a; // Uncaught ReferenceError: a is not defined
const a = 2; // Uncaught TypeError: Identifier 'a' has already been declared
a = 3; // Uncaught TypeError: Identifier 'a' has already been declared

What is the expected behavior?
A const-declared variable should either be declared & have a value or not declared at all

What went wrong?
The variable is declared & can't be re-created in the current scope but it's not defined.

Did this work before? N/A 

Chrome version: 51.0.2682.0  Channel: canary
OS Version: OS X 10.11.3
Flash Version: Shockwave Flash 21.0 r0

In Chrome <50 block scoping in DevTools is broken (each instruction is wrapped in a block so declarations are invisible in subsequent code) so this can't be observed there.
 

Comment 1 by caseq@chromium.org, Mar 18 2016

Cc: kozyatinskiy@chromium.org caseq@chromium.org
Components: -Platform>DevTools Blink>JavaScript
This appears to be due to the fact that const is lexically scoped, so it also happens in case of let. kozyatinskiy@ also managed to reproduce this without getting DevTools involved simply by having two scripts:

<script>
let a = nonexistent; // Uncaught ReferenceError: nonexistent is not defined
</script>
<script>
a = 1; // Uncaught ReferenceError: a is not defined
</script>

The second error looks rather counter-intuitive and I'm having a hard time figuring whether this is according to the spec, so over to V8 team to triage.

Labels: Te-NeedsFurtherTriage
Cc: littledan@chromium.org adamk@chromium.org
Components: -Blink>JavaScript Blink>JavaScript>Language

Comment 4 by adamk@chromium.org, Mar 21 2016

Owner: littledan@chromium.org
Status: Assigned (was: Unconfirmed)
I'm pretty sure this is the correct behavior per spec (uninitialized lexical declarations throw ReferenceErrors when accessed), but assigning to littledan for confirmation.
Status: WontFix (was: Assigned)
Yes, I agree that this is correct behavior per spec. You can also achieve it via classes!

class A extends B { } // where B doesn't exist
// now A is in limbo too
Note that this has previously been called out as a concern for counter-intuitive behavior in the spec, both inside and outside of the committee. If enough developers complain about this issue, it could be changed in a future revision. But any change would start on the spec side, not in V8.

Comment 7 by m.go...@gmail.com, Mar 22 2016

Thanks for the explanation! I agree it seems counter-intuitive but the spec is the spec so as long as it doesn't change there v8 is fine.

Comment 8 by caseq@chromium.org, May 13 2016

 Issue 610817  has been merged into this issue.

Sign in to add a comment