fix(semantic): correctly visit IfStmt test when building cfg#9864
Merged
graphite-app[bot] merged 1 commit intomainfrom Mar 19, 2025
Merged
Conversation
Contributor
Author
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
CodSpeed Performance ReportMerging #9864 will not alter performanceComparing Summary
|
618998a to
45c87a6
Compare
Contributor
Author
|
cc @hulin32 hopefully that description makes sense |
45c87a6 to
ee6bb75
Compare
|
@camc314 thx a lot for your explanation. This part I understood. Need to look into it to get more ideas. |
Member
Merge activity
|
This PR fixes the construction of the cfg within `oxc_semantic`, by marking the `test` of `IfStatement` as unconditionally visited.
Given the following:
```ts
if (foo) {
bar();
} else {
baz();
}
```
Would produce the following graph:
```mermaid
graph TD
0["bb0"]
1["bb1 IfStatement"]
2["bb2 Condition(IdentifierReference(foo))"]
3["bb3 BlockStatement ExpressionStatement"]
4["bb4 BlockStatement ExpressionStatement"]
5["bb5"]
1 -- "Error(Implicit)" --> 0
2 -- "Error(Implicit)" --> 0
3 -- "Error(Implicit)" --> 0
4 -- "Error(Implicit)" --> 0
5 -- "Error(Implicit)" --> 0
1 -- "Normal" --> 2
1 -- "Normal" --> 4
3 -- "Normal" --> 5
2 -- "Jump" --> 3
4 -- "Normal" --> 5
```
After this change, it produces the following graph:
```mermaid
graph TD
0["bb0"]
1["bb1 IfStatement"]
2["bb2 Condition(IdentifierReference(foo))"]
3["bb3 BlockStatement ExpressionStatement"]
4["bb4 BlockStatement ExpressionStatement"]
5["bb5"]
1 -- "Error(Implicit)" --> 0
2 -- "Error(Implicit)" --> 0
3 -- "Error(Implicit)" --> 0
4 -- "Error(Implicit)" --> 0
5 -- "Error(Implicit)" --> 0
1 -- "Normal" --> 2
3 -- "Normal" --> 5
2 -- "Jump" --> 3
2 -- "Jump" --> 4
4 -- "Normal" --> 5
```
Rather than jumping from the if statment (`bb1\nIfStatement`) directly to the consequent (`bb4 BlockStatement ExpressionStatement`), it now unconditionally visits the `test` of the if statement. This can be seen in the after graph as the jump to `bb4 BlockStatement ExpressionStatement"` comes **from** `bb2 Condition(IdentifierReference(foo))` rather than from `bb1 IfStatement`.
As a result of this change, `rules_of_hooks`, does not have false positives when a hook is in a position such as:
```ts
if (useMe) {
```
As the cfg would previously think that `useMe` was called conditionally when in fact it was not
fixes #9795
related #9807
ee6bb75 to
3d4c5f3
Compare
This was referenced Mar 20, 2025
Boshen
added a commit
that referenced
this pull request
Mar 20, 2025
## [0.61.0] - 2025-03-20 - c631291 parser: [**BREAKING**] Parse `TSImportAttributes` as `ObjectExpression` (#9902) (Boshen) - eef7eb6 minifier: [**BREAKING**] Rename `CompressOptions::all_true`/`all_false` to `smallest`/`safest` (#9866) (sapphi-red) ### Features - 38ad787 data_structures: Add `assert_unchecked!` macro (#9885) (overlookmotel) - dcd356e minifier: Support `keep_names` option (#9867) (sapphi-red) - 6565fc4 napi: Feature gate allocator (#9921) (Boshen) - 2cedfe4 napi: Add codeframe to napi error (#9893) (Boshen) - a9a47a6 parser: Add regex cargo feature to oxc_parser (#9879) (Toshit) - d4a83ba parser: Report duplicate modifier `Accessibility modifier already seen.` (#9890) (Boshen) - 59c8f71 parser,codegen: Handle lone surrogate in string literal (#9918) (Boshen) ### Bug Fixes - 28a2ed3 estree/ast: Fix `IdentifierName` and `IdentifierReference` (#9863) (hi-ogawa) - 3d4c5f3 semantic: Correctly visit `IfStmt` `test` when building cfg (#9864) (camc314) - 1774225 transformer/using: Incorrect scope ids for bindings (#9871) (camc314)- 68018e1 Ast changes (Boshen) ### Performance - 5f97f28 ast/estree: Speed up raw deser for `JSXElement` (#9895) (overlookmotel) - b272893 mangler, minifier: Initialize a Vec with a specific value using `Vec::from_iter_in` combined with `repeat_with` (#9908) (Dunqing) - f7d078c semantic: Use `reserve_exact` instead of `reserve` to save memory in pre-reserve (#9910) (Dunqing) ### Documentation - 590a258 napi/parser: Add stackblitz link for wasm build (Boshen) ### Refactor - 62e2859 ast/ast_builder: Use `self.vec_from_iter` instead of `Vec::from_iter_in` for consistency (#9909) (Dunqing) - b2f3d23 isolated_declarations: Remove unused `self` params (#9868) (overlookmotel) - 961b95d napi: Move common code to `oxc_napi` (#9875) (Boshen) - 233c1fc napi/playground: Add JSON.parse wrapper (#9880) (Hiroshi Ogawa) - dbe61c5 transformer/module-runner-transform: Remove redundant converison (#9912) (Dunqing) - ecdfe2e transformer/using: Move work to `exit_static_block` (#9713) (camc314) ### Testing - 9314147 data_structures: Enable doc tests for `oxc_data_structures` crate (#9884) (overlookmotel) - 040e993 napi: Refactor NAPI parser benchmarks (#9911) (overlookmotel) - e637e2e napi/parser: Tweak vitest config (#9878) (Hiroshi Ogawa) Co-authored-by: Boshen <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This PR fixes the construction of the cfg within
oxc_semantic, by marking thetestofIfStatementas unconditionally visited.Given the following:
Would produce the following graph:
graph TD 0["bb0"] 1["bb1 IfStatement"] 2["bb2 Condition(IdentifierReference(foo))"] 3["bb3 BlockStatement ExpressionStatement"] 4["bb4 BlockStatement ExpressionStatement"] 5["bb5"] 1 -- "Error(Implicit)" --> 0 2 -- "Error(Implicit)" --> 0 3 -- "Error(Implicit)" --> 0 4 -- "Error(Implicit)" --> 0 5 -- "Error(Implicit)" --> 0 1 -- "Normal" --> 2 1 -- "Normal" --> 4 3 -- "Normal" --> 5 2 -- "Jump" --> 3 4 -- "Normal" --> 5After this change, it produces the following graph:
graph TD 0["bb0"] 1["bb1 IfStatement"] 2["bb2 Condition(IdentifierReference(foo))"] 3["bb3 BlockStatement ExpressionStatement"] 4["bb4 BlockStatement ExpressionStatement"] 5["bb5"] 1 -- "Error(Implicit)" --> 0 2 -- "Error(Implicit)" --> 0 3 -- "Error(Implicit)" --> 0 4 -- "Error(Implicit)" --> 0 5 -- "Error(Implicit)" --> 0 1 -- "Normal" --> 2 3 -- "Normal" --> 5 2 -- "Jump" --> 3 2 -- "Jump" --> 4 4 -- "Normal" --> 5Rather than jumping from the if statment (
bb1\nIfStatement) directly to the consequent (bb4 BlockStatement ExpressionStatement), it now unconditionally visits thetestof the if statement. This can be seen in the after graph as the jump tobb4 BlockStatement ExpressionStatement"comes frombb2 Condition(IdentifierReference(foo))rather than frombb1 IfStatement.As a result of this change,
rules_of_hooks, does not have false positives when a hook is in a position such as:As the cfg would previously think that
useMewas called conditionally when in fact it was notfixes #9795
related #9807