swc-project / swc

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

Unexpected behavior with function redefinition #6903

Closed LeshaTat closed 1 year ago

LeshaTat commented 1 year ago

Describe the bug

This issue was encountered in real code and distilled into the provided minimal example. It appears to stem from the compress feature, which is not correctly handling function redefinition and sticking to the first definition of the wrapper function.

Input code

function test(a, b) {
    let wrapper = (e) => e
    wrapper = (e) => (["not", e])
    if (a) {
        return wrapper(b)
    }
    return wrapper(1)
}
console.log(test(true, "bad"))

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "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": true
}

Playground link

https://play.swc.rs/?version=1.3.32&code=H4sIAAAAAAAAA22OwQrCMBBE7%2FsVQ067EAQ%2FoP6IeEjqVgohKekGD9J%2Fr0Z60jnOvBlmanm0uWSYrsbBIwpehLeSGp41LItWDGAVDBdoj35svrpczHnoTToxT%2BBwLH1U1VrNR5PjF9voT3YW2mgseS1JT6k8uF%2Bz2tTDxXB3IrQDQID8p7cAAAA%3D&config=H4sIAAAAAAAAA22TQXLDIAxF7%2BJ1tu2iB%2BiuZ2AUEA4tRh4k0ngyuXuFY%2BM46c56fMniS1y7b7bdx7UbITPm%2BsVTErh0Hx3aAdjmMEp3UJkiD5HxdugEco9SJfymZ5GIcTk9dENIwU%2B1kqVhzMhcvzWjDJiEmw5ypl8NJReNjkQRIb3EBtiEJNhj3lItxQgjozlDbhn1b5ADU9qhIujMmGl8oMkFCZS02MocgjOWHDYQMloJZ9wkWkMlibWx1skMHR5L31f37ko8QywgrRZeZhv0jy3vRIHF%2BJL4Ce06vaP7JRdV8CajlJxWzTeFtPPhB1H7jMCcYMAtc%2BZe57BX%2Bn9UIXkdokyN6oS3rhL2ejkTgm83rF1jlrC5ldEVi%2FWGdiu%2BwN2FODg06L263ZL5N4g9bcVkGpF8C9U18NsM7qFZ92lP6wq8wE%2FtSh7HsfAB5PTMeBqOFF9KDCgnci9YjRB6hlm39zI%2B05Icqs3oHg4K17CtqQ5fyMT61FafdSE0x%2FSRjm199UUOkPq2lrcKyJUKrrN581N9727b61zXib8W4VzoD8zDcjEPBAAA

Expected behavior

The compiled code should log ["not", "bad"].

Actual behavior

It outputs "bad".

Version

1.3.32

Additional context

No response

swc-bot commented 1 year 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.