Conversation
|
Interestingly, since this checks at compile time (not at runtime as in JS) for any possible lazy use of this kind, bootstrap finds that let fn = foo;
fn(); // (1) possibly
let a = 1;
function foo() {
a = 2; // if (1): ReferenceError: Cannot access 'a' before initialization
}The Overall, my expectation is that this change will break some code in the wild, similar to what happened in the compiler. I do not see a good alternative, though. In fact, the ability to diagnose this at compile time is a lucky coincidence as a side-product of categorical tree-shaking, whereas, unless I am missing something, anything else would involve a bunch of invisible runtime checks. |
|
Does this a breaking change? I mean, it could potentially break existing code |
|
Yes, this is a breaking change, and I'd expect it to break some existing code. |
Fixes #2622, where it was discovered that global variables annotated as
@globalmay not yet have been initialized when used, leading to subtle issues.Turns out that the underlying problem is even broader, in that uses of global variables currently unconditionally compile these and their initializers out-of-band, possibly leading to subtle side-effects like the one seen in imported files declaring a
@globalvariable. Hence this PR adds a check whenever a global is to be compiled lazily, and unless the global is annotated as@lazy, is built-in or ambient (where order is not relevant), diagnoses uses before their declaration.cc @HerrCai0907, as this also supersedes #2624.