swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.96k stars 1.21k forks source link

`dead_branch_remover` incorrectly removes `init` part of `VariableDeclarator` node #9427

Closed levi-nz closed 1 month ago

levi-nz commented 1 month ago

Describe the bug

The variable instantiatedVar14 in this script has an init value, but after running simplifier on this script, the init part is removed, leaving the variable's value as undefined. The culprit appears to be dead_branch_remover from my testing.

I will try make a minimal re-production when I have free time.

I have uploaded the script to Ghostbin for now, found at https://pst.innomi.net/paste/uv7882egh49frqfg64a72z8p , since it exceeded the character limit here.

Input code

No response

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": false,
        "booleans": false,
        "booleans_as_integers": false,
        "collapse_vars": false,
        "comparisons": false,
        "computed_props": false,
        "conditionals": false,
        "dead_code": false,
        "directives": false,
        "drop_console": false,
        "drop_debugger": false,
        "evaluate": false,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": false,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": false,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": false,
        "switches": false,
        "typeofs": false,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": false,
        "const_to_let": false,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs

SWC Info output

No response

Expected behavior

Variable should be left as-is, e.g. var instantiatedVar14 = MDT() - C2T();

Actual behavior

Variable's init part is removed, becoming var instantiatedVar14 (it is merged with other variables that have no init part)

Version

1.7.10

Additional context

You can paste the input code above on play.swc.rs, the script is too large to create a link here.

levi-nz commented 1 month ago

Btw the variable's init field returns a number, not undefined, so this is definitely invalid. Even if both CallExpressions returned undefined, the result would be NaN.

kdy1 commented 1 month ago

dead_branch_remover is (wrongly) considering it as an unreachable branch for some reason, so the initializer does not matter.

kdy1 commented 1 month ago
image

Okay, I think return init.bind(this)(); is the reasoning of dead_branch_remover.

kdy1 commented 1 month ago

It seems like it's correct

swc-bot commented 3 days ago

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.