swc icon indicating copy to clipboard operation
swc copied to clipboard

`es/minifier`: Unnecessary variable when inlining

Open Le0Developer opened this issue 7 months ago • 5 comments
trafficstars

Describe the feature

function square(x) {
  return x * x;
}

function cube(x) {
  return square(x) * x;
}
console.log(cube(a));

Produces:

var x, x1;
console.log((x1 = x = a) * x1 * x);

x1 is unnecessary here.

Weirdly enough, when enabling mangling x1 gets renamed to x which looks funny:

var x, x;
console.log((x = x = a) * x * x);

There may be some scoping issues with inlining which causes this (incorrect?) mangling behavior.

Babel plugin or link to the feature description

https://play.swc.rs/?version=1.11.18&code=H4sIAAAAAAAAA0srzUsuyczPUyguLE0sStWo0FSo5lJQKEotKS3KU6hQ0FKosOaq5eJKg6lLLk1CV4XQClWdnJ9XnJ%2BTqpeTn64BVp%2BoqWkNAMwliaVqAAAA&config=H4sIAAAAAAAAA32US3LbMAyG9zmFR%2Bts00UP0F3PwKFFUGYqEhoCdKzJ%2BO4F9bCdGMxOxAcI5I%2FH58vh0L1T3%2F0%2BfMqnHCabCfLtLBaaE9uLWDqeJ6A%2Bh4m7150yVeTtSLCYrivp2OYBuEYBvW3u3YhIsLtvthhS8PNjwh7jlIHowSZW%2BV%2BJkLiaORd4fUQZPxT7EXEEm34gxpIJiWGArHj1OI52IjBnq%2BMoagVCLUOFhcGZKeOk8uQCB0wixTN1YJ3p0YGCQoaewxm0MMklYYnkdV9lvmMHxzIMS4m%2FRcPZjsWykhMuS0Hkts%2FshIHY%2BLKI8D3jChsSrFDXNniTgUtWEr5jSI2ofwDy%2FtESJRtBu9Di4aWXWtH%2Bx8iQvLQrzwqX3tYemWAQSU0IXtG1CgOZg1bLDK70UIXt21SXgYIDA95Ln2j0I3B%2F0lLW%2BUavACmu1e6%2F2k1r%2FjZcR6FN%2F8gDWe2szSFaPjUhzfGI2gTtwcAndG0uFWBs0iyL4TI1cUkOpB%2FAaR6FNHsdTjaMZlyW41M%2FyETID80w4vG%2BGDaH623pRpuG%2B4Sve%2Fdlc%2BgiurLAbaPXqq57%2BFd3d9q37q2Nu0B%2F98gl6fU%2FUub13x0GAAA%3D

Additional context

No response

Le0Developer avatar Apr 09 '25 08:04 Le0Developer

another note: thanks to the "Acesssing top-level identifiers do not have side effects." assumption, there should be no need for an intermediate variable at all, but that's another issue.

Le0Developer avatar Apr 09 '25 12:04 Le0Developer

@kdy1 we have is_unresolved_ref_safe in every part of minifier. I believe the documented assumption is wrong for this.

Austaras avatar Apr 10 '25 03:04 Austaras

@Austaras Do we need additional assumptions?

kdy1 avatar Apr 12 '25 05:04 kdy1

I believe current ones are enough.

Austaras avatar Apr 12 '25 05:04 Austaras

Oh we still need to fix mangle

Austaras avatar Apr 12 '25 05:04 Austaras

And the mangle issue is because playground set mangle.top_level to false. Using mangle: true would solve this issue.

Austaras avatar Aug 05 '25 12:08 Austaras

It still has unnecessary variable, no?

var o, l;
console.log((l = o = a) * l * o);

Although this applies generally, so may be a different transform.

Le0Developer avatar Aug 05 '25 12:08 Le0Developer

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Sep 20 '25 12:09 swc-bot