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

Unintended compressed with if_return flag #8324

Closed
kakao-aiden-ahn opened this issue Nov 21, 2023 · 1 comment · Fixed by #8328 or #8342
Closed

Unintended compressed with if_return flag #8324

kakao-aiden-ahn opened this issue Nov 21, 2023 · 1 comment · Fixed by #8328 or #8342
Assignees
Labels
Milestone

Comments

@kakao-aiden-ahn
Copy link

kakao-aiden-ahn commented Nov 21, 2023

Describe the bug

Assume that the Deferred contained in window.current is resolved outside the script.

setTimeout(() => window.current.resolve('result'), 3000);

If normal, 'result' should appear when the logic below is executed.

bug().then(alert)

However, minify with swc, the error below occurs.

Uncaught TypeError: Cannot read properties of undefined (reading 'resolve')

This is because the logic in the try statement changes to 'break' and window.current is immediately deleted in finally.

The problem is reproduced even when only the if_return option of compressor is enabled.
This problem does not occur with terser.

Input code

function Deferred() {
  const deferred = this;
  deferred.promise = new Promise(function (resolve, reject) {
    deferred.resolve = resolve;
    deferred.reject = reject;
  });
}

export async function bug() {
    const s = `next`;
    if (!window[s]) {
        for (window[s] = new Deferred();;)
            if (window.current) await window.current.promise;
            else {
                window.current = window[s];
                try {
                    return await window[s].promise // This line compressed to 'break'. I guess compressor intended jump to 23 line which is looks like same code.
                } finally {
                    delete window.current // Above 'break' makes unintended delete
                }
            }
    }
    return await window[s].promise
}

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": false,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": false,
        "negate_iife": false,
        "properties": false,
        "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": false,
        "passes": 3
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.99&code=H4sIAAAAAAAAA31SPU%2FDMBDd8yuOqamEUgnGiAGJhY2BDSHVTS6tm%2BQc%2BYO0qvLfOePEkAbwENn37r17fk7lqLBSETxhhVpjma7hkgAUioyFcizCA9iDNDkDUynrtGqlQYYIe3gJp7Sa9FKNRjUfeAsaj1jYIPuDP%2BLMH3f5Ne5pX7DfeHRY58mQJHjqlLYgzJkKiAN3bp9OQ4J7w%2BQt4clug7SsIL3pJZWqfzPvU69fldKQRmS80ncieb6OrZNQ6M4Kxy3EtxO9kBbm1SmjfMbGhlO7zEp%2BzansIfrJF71Wn39R8EujdZpmblghPtZmA6%2F8kNBIQk6p7Th7w%2B9rFax2GkW9yuAZ9o6rEeZsJFmkkvuOru188919kOgPsjiAF1Sq9rI1ghGt1y4xWzgcoJIkmuYv9yU2aPE6Cjb9uFP8q4wWoRU1GnAUbQXeclyyPIXv%2FzElwyeFShVWFwMAAA%3D%3D&config=H4sIAAAAAAAAA32VTY7bMAyF9zlF4HUXRQrMogformcQFItylFqiIVKZGIPcfei%2FTNpS3sX8%2BMzokaI%2FDsdjc6W2%2BXn8kJ%2FyMNhMkJ%2FPEqExsb1LpIE2WmpzGLj5ttErTcjbnmAOPRbSsM0d8Kyi0%2FfTaVU0PSLBplhjMaTgx9eaLcYhA9FLTKLyyhIhMf2tX1nGdw2cEXuwaQ8ZSyYkhg6yltZi39uBwNxshUdxLRCqRSZaGJwZMg56QnKBAyaJK9iBdaZFBxoLGVoON1CFUk%2BEieSQNezgXLpu7ve%2FHG62L5Y1Kdzn5shfVuAFA7HxRTVjgTUnFlpxOXiTgUueanIur%2BiKIdVkfwDEhd4SJRtVn%2BYML7NVh7vSkLzML48Kl2FXD5qgE2dNCF6zd7IHMge1ZAZXWpj8bXdwxQwKDgx4L0Oj4vfA7UUty%2BMA6DUijbbqMRZgqhdz5dP92MG%2F5KCsT9qaES1f6pTGeEb1Ym1y4Au6nQRpB2MdZ1kb96HOS3IgAwJOTSmkgunasmE0%2FbxD%2F58QuSjyTtP1eNbXxiATP3fyxxp7PFd2tKn7WgnL1j6sCU1EV2a4fg%2Bmxi9b%2FK35StoW9rNqE%2Bj3ppxu5%2BHxCZo7moVbBgAA

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:23 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6020
    CPU: (12 cores)
        Models: Apple M2 Max

Binaries:
    Node: 18.15.0
    npm: 9.5.0
    Yarn: N/A
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.3.99
    @swc/helpers: N/A
    @swc/types: 0.1.5


SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

The logic inside the try statement remains.

Actual behavior

The logic inside the try statement is replaced with 'break'.

Version

1.3.99

Additional context

No response

@kdy1 kdy1 self-assigned this Nov 21, 2023
@kdy1 kdy1 added this to the Planned milestone Nov 21, 2023
kdy1 added a commit that referenced this issue Nov 23, 2023
kdy1 pushed a commit that referenced this issue Nov 25, 2023
…8342)

**Related issue:**

 - Closes #8337.
 - Closes #8324.
 - Reverts #8328.
@kdy1 kdy1 modified the milestones: Planned, v1.3.100 Nov 30, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Dec 30, 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 Dec 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.