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

[minifier] Incorrect minification with a bigint value created by a binary operator inside conditions #10013

Open
sapphi-red opened this issue Feb 7, 2025 · 3 comments
Labels
Milestone

Comments

@sapphi-red
Copy link

Describe the bug

(a | b) === 0 is compressed into (a | b) == 0 and causing the code to behave differently after minification.

Input code

export function foo(a, b) {
  if ((a | b) === 0) {
    throw new Error()
  }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2021",
    "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": {
        "toplevel": false,
        "keep_classnames": false,
        "keep_fnames": false,
        "keep_private_props": false,
        "ie8": false,
        "safari10": false
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": true,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.10.14&code=H4sIAAAAAAAAA0utKMgvKlFIK81LLsnMz1NIy8%2FXSNRRSNJUqOblUlDITFPQ0EhUqAEJ2NraKhhAxRUUSjKK8ssV8lLLFVyLivKLNDRBwrW8XLUAKvcWwFIAAAA%3D&config=H4sIAAAAAAAAA41VwW7bMAy99ysCn3doeyiGfcBu%2BwZBsShHmSwaIuUmKPLvoxU7TRva2CWI%2BfRIkXykPp52u%2BZIbfNr9yF%2F5WOwmSDfvsVC58T2JJYG2t5Sm8PAzY8FPdIEeRsJqulyRRq2uQOuLHp9fn2ZGU1EJFgYs60PKfjzfcwW%2ByED0Z1NrOKy9JCYvvJnLOP7BHAu9%2FY9YgSbNhBjyYTE0EHWHLcYox0IzGiz4mW6qc2BUAsxgYXBmSHjoOLJBQ6YJOYj6sA606IDBQoZWg4jaDSJJbREkp6ST4Ud7EvX1T5%2FY8NoY7GsxIRTbYncVvF6wEBsfElaCa%2FgSg2u4Fzc78zgTQYuOT3yjhjSSk%2F%2BAkgFoiVKtgfNbz3hRU9rbL%2FJDMmLZPms4KJvLcsEnRTVhOCVyk6VgcxB62YGV1qYKttq15nhlfJRcGDAe9GK4preA7cHLSifB0CvANJf6zVVXQFzm8IVfBqIDfi3ZMm6wOYTveXDOkrnfo9xI0APfEC3cUBawbgOZ9kSp2EdL8mBSAOceqRQBR6XgAwAo4l1Xz5oQ8ZDPJou4v5zTcwHLrc93NvU1Xm%2F25eMQ4QR4pqM%2F2NENlG52zipepnsh%2BmFn5oorQxeeHn%2B8mxIKk%2FLb02q6dGVu4SqJq%2FPyVvzeWh5OZaqNYH%2BLMRap8s%2FdrgRoOMGAAA%3D

SWC Info output

No response

Expected behavior

The output is

export function foo(o,r){if((o|r)===0)throw Error()}

Actual behavior

The output is

export function foo(o,r){if((o|r)==0)throw Error()}

Version

1.10.14

Additional context

No response

@sapphi-red sapphi-red changed the title Incorrect minification with a bigint value created by a binary operator inside conditions [minifier] Incorrect minification with a bigint value created by a binary operator inside conditions Feb 7, 2025
@Le0Developer
Copy link

For anyone wondering with which input these aren't equivalent (like me): BigInts.

function foo(a, b) {
  return (a | b) === 0
}

function foo2(a, b) {
  return (a | b) == 0
}

console.log(foo(0n, 0n)) // false
console.log(foo2(0n, 0n)) // true

@sapphi-red
Copy link
Author

Ah, my bad, I thought I wrote it.

@kdy1 kdy1 added this to the Planned milestone Feb 8, 2025
@Austaras
Copy link
Member

Austaras commented Feb 8, 2025

swc minifier hasn't been prepared for BigInt yet. Another example would be #9938.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants