Bug 156673 - We allow assignments to const variables when in a for-in/for-of loop
Summary: We allow assignments to const variables when in a for-in/for-of loop
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: JF Bastien
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-17 00:22 PDT by Saam Barati
Modified: 2016-08-17 16:58 PDT (History)
15 users (show)

See Also:


Attachments
patch (6.72 KB, patch)
2016-08-17 12:21 PDT, JF Bastien
fpizlo: review-
buildbot: commit-queue-
Details | Formatted Diff | Diff
Archive of layout-test-results from ews100 for mac-yosemite (952.04 KB, application/zip)
2016-08-17 12:53 PDT, Build Bot
no flags Details
patch (6.82 KB, patch)
2016-08-17 14:59 PDT, JF Bastien
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Saam Barati 2016-04-17 00:22:13 PDT
```
const x = 20;
for (x in/of [1,2,3]) ;
// We don't throw an exception even though we should.
```
Comment 1 Marcus Buffett 2016-04-24 13:14:23 PDT
Tried to reproduce with the following: 
```
const x = 20;
for (x in [1,2,3]) {
}
```
A syntax error was thrown, specifically `TypeError: Assignment to constant variable`. Close?
Comment 2 Saam Barati 2016-04-24 16:09:21 PDT
(In reply to comment #1)
> Tried to reproduce with the following: 
> ```
> const x = 20;
> for (x in [1,2,3]) {
> }
> ```
> A syntax error was thrown, specifically `TypeError: Assignment to constant
> variable`. Close?

Did you try:
```
function foo() {
    const x = 20; 
    for (x in [1,2,3]) { }
}
foo()
```
?
Comment 3 Marcus Buffett 2016-04-24 18:10:10 PDT
I did not, confirmed that reproduces the issue. Thank you!
Comment 4 JF Bastien 2016-08-17 12:21:10 PDT
Created attachment 286315 [details]
patch
Comment 5 Build Bot 2016-08-17 12:53:31 PDT
Comment on attachment 286315 [details]
patch

Attachment 286315 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.webkit.org/results/1888656

New failing tests:
webgl/1.0.2/conformance/extensions/webgl-compressed-texture-s3tc.html
js/arguments-iterator.html
inspector/formatting/formatting-json.html
Comment 6 Build Bot 2016-08-17 12:53:35 PDT
Created attachment 286318 [details]
Archive of layout-test-results from ews100 for mac-yosemite

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews100  Port: mac-yosemite  Platform: Mac OS X 10.10.5
Comment 7 Filip Pizlo 2016-08-17 12:56:11 PDT
Comment on attachment 286315 [details]
patch

Looks like you have some test failures in layout tests.  Some of our JS test coverage comes from layout tests, especially weird things like the WebGL tests (which have lots of weird JS) and the inspector tests (giant pile of JS).  You'll either need to rebase the tests or figure out if maybe you created a legit regression.
Comment 8 Saam Barati 2016-08-17 14:40:33 PDT
Comment on attachment 286315 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286315&action=review

> JSTests/stress/for-in-of-const.js:5
> +    print("Executing: " + why);

Remove this.

> JSTests/stress/for-in-of-const.js:10
> +    print("Executing: " + why);

remove this.
Comment 9 JF Bastien 2016-08-17 14:57:05 PDT
Comment on attachment 286315 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286315&action=review

>> JSTests/stress/for-in-of-const.js:5
>> +    print("Executing: " + why);
> 
> Remove this.

Done.

>> JSTests/stress/for-in-of-const.js:10
>> +    print("Executing: " + why);
> 
> remove this.

Done.
Comment 10 JF Bastien 2016-08-17 14:59:54 PDT
Created attachment 286332 [details]
patch

Updated patch, checks for var.isReadOnly() because isStrictMode otherwise fires and throws when it shouldn't. I'll file a separate bug to refactor that (isReadOnly versus isConst).
Comment 11 Saam Barati 2016-08-17 15:02:32 PDT
Comment on attachment 286332 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286332&action=review

> Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:2562
> +            if (var.isReadOnly())
> +                generator.emitReadOnlyExceptionIfNeeded(var);

I think you want these before emitExpressionInfo().

> Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:2795
> +                if (var.isReadOnly())
> +                    generator.emitReadOnlyExceptionIfNeeded(var);

Ditto.
Comment 12 Saam Barati 2016-08-17 16:37:22 PDT
(In reply to comment #11)
> Comment on attachment 286332 [details]
> patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=286332&action=review
> 
> > Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:2562
> > +            if (var.isReadOnly())
> > +                generator.emitReadOnlyExceptionIfNeeded(var);
> 
> I think you want these before emitExpressionInfo().
> 
> > Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:2795
> > +                if (var.isReadOnly())
> > +                    generator.emitReadOnlyExceptionIfNeeded(var);
> 
> Ditto.

I spoke with JF in person about this.
My above comments are wrong. Basically, we either emit code to throw an exception, in which case, this code is correct. Or, we don't emit code to throw an exception, in which case, we have the same bytecode as we had before this patch. So it's OK to put the emitExpressionInfos where they are.
Comment 13 WebKit Commit Bot 2016-08-17 16:58:40 PDT
Comment on attachment 286332 [details]
patch

Clearing flags on attachment: 286332

Committed r204586: <http://trac.webkit.org/changeset/204586>
Comment 14 WebKit Commit Bot 2016-08-17 16:58:45 PDT
All reviewed patches have been landed.  Closing bug.