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

The transformer generate incorrect code when referencing variables declared using using from closures #8895

Closed
hangxingliu opened this issue Apr 26, 2024 · 0 comments · Fixed by #8903
Assignees
Labels
Milestone

Comments

@hangxingliu
Copy link

hangxingliu commented Apr 26, 2024

Describe the bug

A reference error will be emitted when executing the code generated by swc that references a variable declared using using from closures. (See the sample input code below)

This issue can be re-produced in both of Typescript and JavaScript. The main part of a sample generated code looks like:

function main() {
    function readFile() {
        file.read();
    }
    try {
        var _usingCtx = _using_ctx();
        const file = _usingCtx.u(new File());
        readFile();
    } catch (_) {
        _usingCtx.e = _;
    } finally{
        _usingCtx.d();
    }
}

Executing this code can emit an reference error: file is not defined.

Moreover, here is the main part of the generated code from the official TypeScript transpiler (tsc), without this issue:

function main() {
    const env_1 = { stack: [], error: void 0, hasError: false };
    try {
        const file = __addDisposableResource(env_1, new File(), false);
        function readFile() {
            file.read();
            // ...
        }
        readFile();
    }
    catch (e_1) {
        env_1.error = e_1;
        env_1.hasError = true;
    }
    finally {
        __disposeResources(env_1);
    }
}

The link to this code on typescriptlang.org.

Input code

class File {
  read() {
    return 'content';
  }
  [Symbol.dispose]() {
    console.log(`closing the file ...`);
  }
}
function main() {
  using file = new File();
  function readFile() {
    file.read();
    // ...
  }
  readFile();
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false,
      "explicitResourceManagement": true
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.5.0&code=H4sIAAAAAAAAA0WOMQrDMAxFd59CW5zFOYDp2gt0LIW4jpIaHKnEDqGU3L22Y9JBIH2e%2Fv%2FWmxDg6jzCVwAsaAbZljUfcV0IGssUkWKjk7qnud8%2B85O9Glx4c8DH%2BZDAwB6V50n21nNwNEF8IYzZXynVt4fHLsaVbHRMMBtH1WAtfGEvQLiVWrK8nHgueMg1M%2BPqqK2L0HU5qVb941rsP5Y8dYDsAAAA&config=H4sIAAAAAAAAA2VPOw6DMAzdOQXy3KFi7B1YegMrNSgoP8VGAiHu3iRA2qqb38fv2VvTtjCxgke7pTGBgJEpVpwYXp3gkhggZZFV1EHgdqkTZ2lAw1Q5WoLRSsuT2M9RUY8OR7LkJFklzlSM%2B%2BEHwTiSlHzu7l13ZoPxnuk3G6x2eli%2Fr1PehkjMf0dYdKOp%2B0djc7aC9a%2B5iOfXsoaMcpr1bmL4OK%2FGmg6a%2B2u9PLO%2FAcfgY45GAQAA

SWC Info output

No response

Expected behavior

The expected behavior is for the code to be generated in a correct way like what tsc does that it runs without emitting reference errors

Actual behavior

No response

Version

1.5.0

Additional context

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management

@kdy1 kdy1 added this to the Planned milestone Apr 27, 2024
@kdy1 kdy1 self-assigned this Apr 29, 2024
@kdy1 kdy1 closed this as completed in #8903 May 5, 2024
kdy1 added a commit that referenced this issue May 5, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.5.4 May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants