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

fix(semantic): connect test expression of for statements to the cfg. #3122

Merged

Conversation

rzvxa
Copy link
Collaborator

@rzvxa rzvxa commented Apr 28, 2024

I don't know if it is correct or not, Fixes my issues with dangling cfg nodes created in for statements. #3071

Copy link

codspeed-hq bot commented Apr 28, 2024

CodSpeed Performance Report

Merging #3122 will improve performances by 18.42%

Comparing 04-28-fix_connect_test_expression_of_for_statements_to_the_cfg (2a8148a) with main (9525653)

Summary

⚡ 1 improvements
✅ 26 untouched benchmarks

Benchmarks breakdown

Benchmark main 04-28-fix_connect_test_expression_of_for_statements_to_the_cfg Change
semantic[RadixUIAdoptionSection.jsx] 411 µs 347.1 µs +18.42%

@rzvxa
Copy link
Collaborator Author

rzvxa commented Apr 28, 2024

Given this block of code:

            function t1() {
              const res = [];
              const additionalCond = true;
              for (let i = 0; i !== 10 && additionalCond; ++i ) {
                res.push(i);
              }
              React.useLayoutEffect(() => {});
            }

the cfg used to look like this:

                                  (0) Program--------------------------- (9) Dummy cfg
                                        |
                                        |
                                        V
                                  (1) Function
                                        |
                                        |
                                        V
   |------------------------->(2) Logical Expression
   |                                  /       \--------------------------------------------------------------------------------------------------
   |                                 /         \                                                                  \                              \
   |                                /           \                                                                  \                              \
   |                               /             \                                                                  \                              \
   |                              /               \                                                                  \                              \
   |                             /                 \                                                                  \                              \
   |                            V                   V                                                                  \                              \
   |      (3) Ident(`additionalCond`)------------>(4) Dummy cfg Created by the Binary Expression or others              \                              \
   |                                                                                                                     \                              \
   |                                                                                                                      \                              \
   |                                                                                                                       \                              \
   |                                                                                                                        \                              \
   |                                                                                                                         \                              \
   |                                                                                                                          \                              \
   |                                                                                                                          V                               V
   ---------------(5) UpdateExpression<------------------------------------------------------------------------------(6) BlockStatement            (7) ExpressionStatement
                                                                                                                                                               \
                                                                                                                                                                \
                                                                                                                                                                 \
                                                                                                                                                           (8) ArrowFunctionExpression

With this change, it would look like this:

                                  (0) Program--------------------------- (9) Dummy cfg
                                        |
                                        |
                                        V
                                  (1) Function
                                        |
                                        |
                                        V
   |------------------------->(2) Logical Expression
   |                                  /       \
   |                                 /         \
   |                                /           \
   |                               /             \
   |                              /               \
   |                             /                 \
   |                            V                   V
   |      (3) Ident(`additionalCond`)------------>(4) Dummy cfg Created by the Binary Expression or others
   |                                                            /              \
   |                                                           /                \
   |                                                          /                  \
   |                                                         /                    \
   |                                                        /                      \
   |                                                       /                        \
   |                                                      V                         V
   ---------------(5) UpdateExpression<----------(6) BlockStatement            (7) ExpressionStatement
                                                                                            \
                                                                                             \
                                                                                              \
                                                                                        (8) ArrowFunctionExpression

I'm unsure if this is illegal, Or would break anything in the type system, etc. But it would simplify the cfg traversal since every node has a follower node and we can check if any 2 nodes share a subtree by checking both are connected to the same parent, and If they have a connection they are also always on the same subtree.

@Boshen Please give it a look I'm not really familiar with what a cfg should look like so it might be just wrong.

What I'm looking for is simplifying checks but I can always find another way to solve it. Right now I'm implementing #3071 with off-the-shelf algorithms so if this PR doesn't work I can just write a specialized one.

@rzvxa rzvxa marked this pull request as ready for review April 28, 2024 22:16
@rzvxa rzvxa marked this pull request as draft April 28, 2024 22:45
@rzvxa
Copy link
Collaborator Author

rzvxa commented Apr 28, 2024

Please don't merge it yet, Since if it gets approved there are other places that need the same treatment

@Boshen
Copy link
Member

Boshen commented Apr 29, 2024

There's also a graphical output from https://github.com/oxc-project/oxc/blob/main/crates/oxc_semantic/examples/cfg.rs

Your change seems to be more correct than the previous one.

This can be verifed with the biome cfg,

image

There's a loop and a exit edge, which is your graph after the change.

@rzvxa
Copy link
Collaborator Author

rzvxa commented Apr 29, 2024

There's also a graphical output from https://github.com/oxc-project/oxc/blob/main/crates/oxc_semantic/examples/cfg.rs

Your change seems to be more correct than the previous one.

This can be verifed with the biome cfg,

image

There's a loop and a exit edge, which is your graph after the change.

Oh, I didn't know about either of these visualization tools, They sure would've come in handy last day when I was trying to figure out this.

I'll check the biome control flow and compare it with ours to find other edge cases that can be improved.

PS: I might mark this one "ready" a bit sooner than the rest since it is a dependency of the react hooks rule

@rzvxa rzvxa force-pushed the 04-28-fix_connect_test_expression_of_for_statements_to_the_cfg branch from fef3ca3 to d24c226 Compare April 29, 2024 06:14
@rzvxa rzvxa changed the title fix: connect test expression of for statements to the cfg. fix(semantic): connect test expression of for statements to the cfg. Apr 29, 2024
I don't know if it is correct or not, Fixes my issues with dangling cfg
nodes created in for statements.
@rzvxa rzvxa force-pushed the 04-28-fix_connect_test_expression_of_for_statements_to_the_cfg branch from d24c226 to 2a8148a Compare May 9, 2024 12:14
@rzvxa rzvxa marked this pull request as ready for review May 10, 2024 12:49
Copy link
Member

Boshen commented May 10, 2024

Merge activity

  • May 10, 10:11 AM EDT: @Boshen started a stack merge that includes this pull request via Graphite.
  • May 10, 10:11 AM EDT: @Boshen merged this pull request with Graphite.

@Boshen Boshen merged commit c91d261 into main May 10, 2024
34 of 51 checks passed
@Boshen Boshen deleted the 04-28-fix_connect_test_expression_of_for_statements_to_the_cfg branch May 10, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-semantic Area - Semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants