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

Incorrect handling of finally block in async generator #6370

Closed
nicknotfun opened this issue Nov 8, 2022 · 3 comments
Closed

Incorrect handling of finally block in async generator #6370

nicknotfun opened this issue Nov 8, 2022 · 3 comments
Labels

Comments

@nicknotfun
Copy link

nicknotfun commented Nov 8, 2022

Describe the bug

The generator finally block is called on every yield, not just on termination.

Input code

(() => {
  async function* generator() {
    try {
      while (true) {
        yield "value";
      }
    } finally {
      console.log("exit generator");
    }
  }

  async function test() {
    let output = 0;
    for await (const val of generator()) {
      console.log(val);
      output++;
      if (output === 2) {
        break;
      }
    }
  }

  test();
})();

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.2&code=H4sIAAAAAAAAA2WPwQ7CIBBEz%2FYrJj2BJsZ4beq%2FYF2USMDQxdqY%2FrvUim30AuyyM%2FtGCIn6gGcBqLZ3DXR0DRvv1jiTo6DYhzQy%2FgMc%2Bs8L6C7GEgSHSPLbBHpD9oTyrmyksvq0h%2Fc9QBunrJ09Gu9ab2lr%2FVmU9DA87yzlJB6lQ%2FFHB6aWv1yWGD7yLTJq7Cah9gGqU8lTjGsYCQleL1Nl%2BWrJkcZk5p48N5tcGw2R99Q19svgx0Dq%2BhM4s0%2BwVTHIdL4AD7K37nIBAAA%3D&config=H4sIAAAAAAAAA0WNSwrDMAxE76J1ts0id%2BghjKsEF%2F%2FQKBBjfPfawSU7aebpqdIXlrZK2QhYxoQS1Vy0kZbMsOKy0kKKHu3Gg1tfjBysHWG8eudTAs92oeCi28sw2RSyMPBUJh7%2BT7YuCulzjqDez27hSu1xzDuH9wRVTm4%2FOkwQUrUAAAA%3D

Expected behavior

value
value
exit generator

Actual behavior

value
exit generator
value
exit generator

Version

1.3.2

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Nov 8, 2022

Closing as duplicate. Please search before filing an issue
#5913

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2022
@nicknotfun
Copy link
Author

@kdy1 I did search apologies; I wasn't aware that the behaviour here was the same root cause [in this case there is no yield in the finally block but it still is incorrectly executed]

@swc-bot
Copy link
Collaborator

swc-bot commented Dec 8, 2022

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 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants