dillon1000/react
Commit
Browse files [rust][sema] Statically detect some TDZ violations
The [temporal dead zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz), often abbreviated TDZ, is the period between the start of its declaring block and the line that contains the let/const/class declaration. Not all instances of TDZ can be detected statically, because whether or not a TDZ error will occur at runtime is a property of which control flow path is taken and whether some other code has initialized the value. However, a subset of cases can be detected, and that's what we implement here. When we encounter a variable reference we record the next declaration id at that point in time. Then when resolving references after visiting the program, we can check: did the reference end up referring to a let/const/class binding whose id is equal or greater to that "next declaration"? If so, it means the reference refers to a variable that is provably declared later and is a known TDZ violation. The catch is that when resolving references, we reset the "next declaration" limit value when we bubble up out of a function scope. That's because references to let/const within a function may occur after the declaration, and we can't statically validate them.
Changed paths4 files
First-parent comparisoncompiler/forget/crates/forget_semantic_analysis/src/analyzer.rs ModifiedM compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs ModifiedA compiler/forget/crates/forget_semantic_analysis/tests/fixtures/tdz.js AddedA compiler/forget/crates/forget_semantic_analysis/tests/snapshots/[email protected] AddedPatch
Files changed
Rendering syntax-highlighted changes…