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

Inlining function call w/ object assignment causes variable to be accessed before assignment #6914

Closed
arshaw opened this issue Feb 7, 2023 · 1 comment · Fixed by #6918
Closed
Assignees
Labels
Milestone

Comments

@arshaw
Copy link

arshaw commented Feb 7, 2023

Describe the bug

Something is going wrong with function inlining and object property assignment. Please see my expected/actual and input/output below:

Input code

console.log(doSomething())

function doSomething() {
  return fabricateEvent()
}

function fabricateEvent() {
  let def = createEventDef()

  return {
    def,
    ui: compileEventUi(def),
  }
}

function compileEventUi(def) {
  let uis = []
  uis.push(def.ui)
  return uis
}

function createEventDef() {
  return {
    id: 'fakeId',
    ui: 'something'
  }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2018",
    "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,
        "passes": 2
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.32&code=H4sIAAAAAAAAA22Quw6CMBSG9z7F2aCJ4QFI3HRwNk7GAcspnFha04uL4d1tQeQStybffzsVRjujsFCmyWtzNh36lnSTc86YDFp4MhpWAN4MwKIPVoOs7pZE5fH4Qu1zzvqFawsHo0IPNUrYg7A4sQPK6J1jkxCSbDc8ApUgTPckNcovlEfGE%2BxXjX9Ev9JALpZebyzlueIZXJsERSA%2BF0eyDtxsXN4%2BjqS6hExWDzzV2bw2c9N%2FZd%2BRH343a9FoAQAA&config=H4sIAAAAAAAAA22TQZLjIAxF7%2BJ1FjO9mJrqA8yuz0ApIBy6MXIhkY4rlbu3cGwcJ7Oznr%2BE%2BBLX7pNt937tRsiMuX7xlAQu3XuHdgC2OYzSHVSmyENkvB06gdyjVAm%2F%2Ffr9V39HIsZFcOiGkIKfajFLw5iRuX5rUhkwCTcd5EzfGkouGh2JIkJ6iQ2wCUmwx7ylWooRRkZzhtwy6mmQA1PaoSLozJhpfKDJBQmUtNjKHIIzlhw2EDJaCWfcJFpDJYm1sdbJDB0eS99XA%2B9KPEMsIK0WXmYb9MSWd6LAYnxJ%2FIR2nd7R%2FZKLKniTUUpOq%2BaTQtr58IWofUZgTjDgljlzr3PYK%2F1%2FVCF5HaJMjeqEt64S9no5E4JvN6xdY5awuZXRFYv1hnYrvsDdhTg4NOi9ut2S%2BTuIPW3FZBqRfAvVNfDbDO6hWfdpT%2BsKvMB%2F2pU8jmPhA8jpmfE0HCm%2BlBhQTuResBoh9Ayzbu9lfKYlOVSb0T38KFzDtqY6fCET62tbfdaF0BzTRzo%2BrO%2Bo0652ven7HCD1bUNvFZArFVxnH%2BeH%2B6e7bQ913Sz%2BWIS15u0HsrmeNh0EAAA%3D

Expected behavior

When running, something like {def: {…}, ui: Array(1)} should be outputted. The def property should be populated.

Actual behavior

When running, the output is {def: undefined, ui: Array(1)}. The def property is undefined.

The cause of the bug is obvious when looking at the compiled code. The def1 variable is assigned to an object property before it is populated.

console.log(function() {
    var def;
    let def1, uis;
    return {
        def: def1,
        ui: (def = def1 = {
            id: 'fakeId',
            ui: 'something'
        }, (uis = []).push(def.ui), uis)
    };
}());

There is some sort of bug with inlining functions and object literal property assignment.

Version

1.3.32

Additional context

This bug is affecting:

@swc-bot
Copy link
Collaborator

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

Successfully merging a pull request may close this issue.

3 participants