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

Wrong full-elimination on for stmt with var decl. #6547

Closed
7086cmd opened this issue Oct 14, 2024 · 1 comment · Fixed by #6551
Closed

Wrong full-elimination on for stmt with var decl. #6547

7086cmd opened this issue Oct 14, 2024 · 1 comment · Fixed by #6551
Assignees
Labels
A-minifier Area - Minifier C-bug Category - Bug good first issue Experience Level - Good for newcomers

Comments

@7086cmd
Copy link
Contributor

7086cmd commented Oct 14, 2024

Given the code:

for (var { c, x: [d] } = {}; 0;);

In reality, our actual behavior is to eliminate the entire code, which is incorrect. We need to eliminate it to:

var { c: a, x: [c] } = {};

Also given:

for (var se = [1, 2]; false;);

need we handle it to:

var se=[1,2];
@7086cmd 7086cmd added C-bug Category - Bug A-minifier Area - Minifier labels Oct 14, 2024
@7086cmd
Copy link
Contributor Author

7086cmd commented Oct 14, 2024

Related issue: rolldown/rolldown#2426 (comment).

I currently don't have much time to work on this since midterms are approaching. If anyone is interested in contributing, you're more than welcome!

Possible Solutions

We could hoist the initialization expression during the DCE AST pass. Since init is either a Variable Declaration or an Expression, this could be achieved by:

// variable declaration
ctx.ast.statement_declaration(SPAN, ctx.ast.declaration_from_variable(decl))
// expression
ctx.ast.statement_expression(SPAN, ctx.ast.move_expression(expr))

This code is just for reference; you’ll need to handle borrow and move semantics properly.

You can then join these statements with block statements. The DCE pass will handle eliminating any extra blocks, so you don't need to worry about that. Later, we can replace this with more complex statement handling, but for now, I've opted to use enter_statement instead of enter_statements.

@7086cmd 7086cmd added the good first issue Experience Level - Good for newcomers label Oct 14, 2024
Boshen pushed a commit that referenced this issue Oct 15, 2024
…` statements during DCE (#6551)

- Closes: #6547

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-minifier Area - Minifier C-bug Category - Bug good first issue Experience Level - Good for newcomers
Projects
None yet
2 participants