swc-project / swc

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

Minifier ignores that ReferenceError can trigger the catch block #9497

Closed hyp3rflow closed 3 weeks ago

hyp3rflow commented 3 weeks ago

Describe the bug

Currently minifier ignores side effects in the catch block if expressions in try block seem not to throw exceptions (e.g. identifier lookup). But we can deceive this logic with referencing variable in TDZ which can make ReferenceError.

Input code

let x;
try {
    c;
} catch (e) {
    x = "SUCCESS";
}
const c = 2;
console.log(x);

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "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": true,
        "switches": true,
        "typeofs": true,
        "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": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.18&code=H4sIAAAAAAAAA8tJLVGosOYqKapUqOZSAIJka65aheTEkuQMBY1UTahghYKtglJwqLOza3CwElABV3J%2BXnGJQjJQ2MgazMnPSdXLyU%2FXqNC0BgCpMQs%2BUwAAAA%3D%3D&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCozrFjostcoDtcgYOLYIyvSShIUDHmozvvtDDj40hdRI%2B%2FAAJgPh%2B2%2B2aE7XNx%2B5bPuWnt4Wg3P%2FFQkNmexFLA22y1JbQc%2FN%2BoycakbeRYDJdZ9KwLR3wpKL9r%2F1%2BUTQRkeCmWGwp5OCH55wtpr4A0ZNNrBKyJshM%2F%2BsXVvBrBFzqs%2F2AGMHmDWIsmZAZOiha4BZjtD2BOduiRBlPaksg1FKMsDI40xfsVZ5d4IBZcr5SB9aZFh0oKBRoOZxBk0kukWWS6yn3mbCDQ%2B26qc8%2F1HC2sVpWcsJlaomcVol6xEBsfM1aCWe4UoMZLsX9qQzeFOBa8qvuhCGv9OQvgFQgWqJsE2hxJw8v87Sm9pvKkL2MLA8Kl%2FnWbpmhk6KaELxS2bEyUDho3SzgagtjZVvtOAteKR8FBwa8l1lRQtNX4PaoJeWhB%2FQKkP5ar03VDMz9Fa7w8UFs4D9yS9YHbPFIlo%2FrlIZ0wLiRIAEf0W04SCsY13GRLXHp13nNDmQ0wKkulSbwugTkATCaOO3Ll9mQ5yERTRfx8FgTi8P1voeTzd3jvc%2Br%2BG1xaBK6OsFlyY%2F9nVfz7%2BbhdNvC94M3gT5vyjns9R%2F7ntJqMQYAAA%3D%3D

SWC Info output

No response

Expected behavior

Ignoring side effects in catch block should not happen when referencing variables in TDZ.

Actual behavior

No response

Version

1.7.18

Additional context

No response

kdy1 commented 3 weeks ago

Ignoring TDZ is intentional