Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let assigment breaks typeof #33462

Closed
mariorodriguespt opened this issue May 19, 2020 · 7 comments
Closed

let assigment breaks typeof #33462

mariorodriguespt opened this issue May 19, 2020 · 7 comments
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. repl Issues and PRs related to the REPL subsystem.

Comments

@mariorodriguespt
Copy link

What steps will reproduce the bug?

> let a = a || 2;
Uncaught ReferenceError: Cannot access 'a' before initialization
> typeof a !== 'undefined'
Uncaught ReferenceError: a is not defined
> typeof b !== 'undefined'
false

How often does it reproduce? Is there a required condition?

Every single time. If you use let to define a variable and in the assignment part you check if the same variable exists, you won't be able to use typeof after.

What is the expected behavior?

I would expect typeof should always work regardless of the variable a existing or not.

What do you see instead?

Uncaught ReferenceError: a is not defined

Additional information

This appear to be an issue in Chromium since it works as expected in Firefox.

@devsnek devsnek added v8 engine Issues and PRs related to the V8 dependency. and removed v8 engine Issues and PRs related to the V8 dependency. labels May 19, 2020
@devsnek
Copy link
Member

devsnek commented May 19, 2020

Duplicate of #8309, albeit a much more confusing one.

@devsnek devsnek closed this as completed May 19, 2020
@devsnek devsnek added duplicate Issues and PRs that are duplicates of other issues or PRs. repl Issues and PRs related to the REPL subsystem. labels May 19, 2020
@devsnek
Copy link
Member

devsnek commented May 19, 2020

Also, you've technically found a bug in firefox. If you feel like it you can report it here: https://bugzilla.mozilla.org/

@mariorodriguespt
Copy link
Author

mariorodriguespt commented May 19, 2020

This is working well for me on Firefox 76.0.1 (64-bit)
Screenshot from 2020-05-19 03-49-58

Could you share your reproduction steps?

@devsnek
Copy link
Member

devsnek commented May 19, 2020

@mariorodriguespt it should be throwing in firefox. This is the same case as { typeof x; let x; }

@mariorodriguespt
Copy link
Author

It is not throwing on Firefox and it is correct.
It throws an error when you try let a = a || 2 but the typeof works as expected.

On your example, Firefox does throw an error: ReferenceError: "can't access lexical declaration x' before initialization"` which is correct due to the order.

As far as I can tell, V8 is not handling this correctly: The assignment failed but the typeof got broken, this issue is more relevant on a browser environment where you have a second script checking a var using typeof.

@devsnek
Copy link
Member

devsnek commented May 19, 2020

When you do let x = /* something that throws an error */, the variable is created, but is never initialized. This state is called the "temporal dead zone" (https://snek.dev/blag/2019-07_js_hoisting might help). While a variable is in the temporal dead zone, typeof x will throw, because the variable exists and is resolvable, but getting its value to try to get the type of it throws a reference error. V8 is behaving correctly here, and firefox has a bug.

@mariorodriguespt
Copy link
Author

Just opened a bug on Firefox, I'll leave the link for those interested in the follow up: https://bugzilla.mozilla.org/show_bug.cgi?id=1639078

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants