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

Function param override gets incorrectly inlined #8161

Closed
dgreif opened this issue Oct 20, 2023 · 1 comment · Fixed by #8168
Closed

Function param override gets incorrectly inlined #8161

dgreif opened this issue Oct 20, 2023 · 1 comment · Fixed by #8168
Assignees
Labels
Milestone

Comments

@dgreif
Copy link

dgreif commented Oct 20, 2023

Describe the bug

SWC compress seems to be inlining values for a param override, even when there are paths where the override might not happen. This does not happen for local variables (e.g. let output = 'a output'), only for variables which come from function params.

Input code

function run(flag: 'a' | 'b', output = 'a output') {
  if (flag === 'b') {
    output = 'b output'
  }

  console.log(output)
}

run('a')
run('b')

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.79&code=H4sIAAAAAAAAA02NUQqAIBBE%2Fz3F%2FK1CdIDAw2hkBOJG6Vd199Ys6GcZ3szshJLGvHDCVpIO0c0DyBFOkKcOXPJaMqywV5PBoYAl4EnDWlujjeJX8F9B%2BKXkjJx2jlMfedbNMkqMuiuLpgn5pG7xOwn0kwAAAA%3D%3D&config=H4sIAAAAAAAAA4VWO2%2FbMBDe8ysCzS4cZOjQqWiDoF26dCwKgSaP8iV8COQxsRDkv%2FdIKZZtUe5m8%2Ft4j%2B8e1NvN7W3zFGXz5faNf%2FIfOBAEJ8wPMD2EyIAWJsKmoIz3IkQIR345i4MjceCzhoYeogzYU7OZcYoZpJDg5FANTliUP23vA1VgkD4I8iWEjE3Q%2B0kktI%2FngQQQkj4pb7d98BoNuo4Jf44EpmxffXiOvZAQtx3SPu22zitorVfJ8NnXksL2aKk5Xv67iOAZoP9uRIy%2FhIV4kUNDInSQM2sg3t%2Fd3zUzEoSL2gd7Hn2K8AAaHTz6UMw%2BIhi1KAEzDXRCDg8fEi3VK%2BGfWedDLFL%2F9ilIyGGNpM0pJSRHaAsqEnkrCOU5g4P8ltAQuovCsDALgSw61MN5ltLbPkCMl9GxWsmCo0q%2BBQ3%2B9VLiguy8NyDcVawVsUVH0FU6egrKGNFHaF9EqFrKUYuA0dcdZTgRqJb7rl9hOIWEnkeriisQqpXciVUQA0jCl0WTjSj75KsucrLV7ApBwS51HSybJQ%2F9izBJUNU3HEq5OPKq7b3HSK1Ori7sCK%2BqMsKT6MvbqNsAlIKr3X3y6FbrlUezlXmI3DSbS%2BuFo7nv1i3o%2F9xGx%2FOKNFQZxq9k7Xh8CVpEXVU8qwWBsF7tACpJyIrLemATYVXUiLzuQGvuqKqD%2BIok93XneTl6XYW4A4Su998ItccZXmXkMbpKeOSsaa0VJw4vrf01PA52581VNxZo72ubd6ZwkchfIwTeNof%2BGiM5Vda9WiHxqlUrq4SHhnxrYPlwlv7hoWK7bWf8bl43854%2BeSiscN28NT4W%2BM1EasZXcf46yPUfX7TPzUz62PKjjfd%2FTAene1UIAAA%3D

SWC Info output

No response

Expected behavior

In the code above, flag can be either a or b. I would expect the flag === 'b' check to remain in the output:

function run(flag, output = 'a output') {
    'b' === flag && (output = 'b output'), console.log(output);
}
run('a'), run('b');

Actual behavior

With SWC compress turned on, it appears to assume that flag === 'b' is always true and inlines the output override value.

function run(flag, output = 'a output') {
    console.log('b output');
}
run('a'), run('b');

Version

1.3.79+

Additional context

In the playground I was able to confirm correct behavior in 1.3.78, with 1.3.79+ having issues. I looked through the changelog for 1.3.79 but nothing immediately jumped out to me as a definite cause. Maybe #7839?

@swc-bot
Copy link
Collaborator

swc-bot commented Nov 23, 2023

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-project swc-project locked as resolved and limited conversation to collaborators Nov 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.