Array destructuring throws error when swapping variables but works with debugger
Reported by
gma...@gmail.com,
Mar 22 2017
|
|||||
Issue description
UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Steps to reproduce the problem:
Consider this code
```
const manualFibbonacci = () => {
let prev1 = 1, prev2 = 1, step = 0
return {
next: () => {
if(step > 100)
return {value: undefined, done: true}
step += 1
if(step <= 2)
return {value: 1, done: false}
[prev1, prev2] = [prev2, prev1 + prev2]
return {value: prev2, done: false}
}
}
}
const it = manualFibbonacci()
console.log(it.next())
console.log(it.next())
console.log(it.next())
console.log(it.next())
```
This will result in a rather confusing error on
```
[prev1, prev2] = [prev2, prev1 + prev2]
```
So trying to debug it
```
debugger;
[prev1, prev2] = [prev2, prev1 + prev2]
```
Now there will be no error
What is the expected behavior?
No error, everything works. Or at least, that a `debugger;` statement doesn't trigger different behavior
What went wrong?
Seems like there is some sort of optimization happening that breaks basic array destructuring.
Did this work before? N/A
Chrome version: 56.0.2924.87 Channel: n/a
OS Version: OS X 10.12.3
Flash Version:
,
Mar 22 2017
It isn't due to missing semi-colons as far as I know, why wouldn't automatic semi-colon insertion work here? But....ugh, I double and triple tested this last night. Made a jsbin and everything. This morning, can no longer recreate. I have no idea...
,
Mar 22 2017
Thank you for providing more feedback. Adding requester "kochi@chromium.org" to the cc list and removing "Needs-Feedback" label. For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
,
Mar 23 2017
I think the following lines are concatenated together, without semicolon:
if(step <= 2)
return {value: 1, done: false}
[prev1, prev2] = [prev2, prev1 + prev2]
which will be interpreted (by v8) as
if(step <= 2)
return {value: 1, done: false} [prev1, prev2] = [prev2, prev1 + prev2];
I'm not an expert on JavaScript grammar so could be wrong, as far as I
understand, for the first two it.next() invocations, `[prev2, prev1 + prev2]`
(evaluates [1, 2]) is returned, and `{value: 1, done: false} [prev1, prev2] = `
part is ignored (?). For 3rd and 4th it.next() invocations, the if-statement
becomes false and the array destrucuring code is never executed.
If you observe anything different from what I saw, feel free to reopen this
with verbose code and console output you saw.
Thanks!
,
Mar 23 2017
I may be wrong about the assumption in the comment above - can someone familiar with JavaScript automatic semicolon insertion, check the code above?
,
Mar 23 2017
To be more precise,
1 if(step > 100)
2 return {value: undefined, done: true}
3 step += 1
4 if(step <= 2)
5 return {value: 1, done: false}
6 [prev1, prev2] = [prev2, prev1 + prev2]
I assumed line 5 and 6 are concatenated in comment 4, but it can be wrong -
if that is the case, line 2 and 3 also should, and automatic semicolon
insertion should happen on the end of return statement line?
For reference, I copied the code to jsbin:
http://jsbin.com/wefafemime/edit?js,console,output
and I saw console output:
[1, 2]
[1, 2]
[object Object] {
done: false,
value: 1
}
[object Object] {
done: false,
value: 1
}
,
Mar 30 2017
I copy and pasted the initial code it into my console and I got no error. What Chrome version were you using? Mine is 58 beta with I+TF enabled.
,
Mar 30 2017
Mine is 57.0.2987.110 (Official Build) (64-bit) on Linux (Ubuntu). I still reproduce the output in comment #6 with the jsbin link. Also tried on console and the same result. Using 59.0.3054.0 (Official Build) canary didn't change the result (which should have I+TF enabled).
,
Mar 30 2017
Specifically, if I add a semicolon on the second return (the line 5 in comment6), the result changes. Demo: http://jsbin.com/kayurep/edit?js,console,output console output: [object Object] { done: false, value: 1 } [object Object] { done: false, value: 1 } [object Object] { done: false, value: 2 } [object Object] { done: false, value: 3 } hablich@, the original report says "error", but I don't see any error either. The problem is that the automatic semicolon insertion is expected between line 5 and 6, or not. Probably this is working as expected, I also tested the semicolon-less version on Safari and Firefox, and got the same result. |
|||||
►
Sign in to add a comment |
|||||
Comment 1 by kochi@chromium.org
, Mar 22 2017