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 yield and try-finally behaviour #5913

Closed
tomas-c opened this issue Sep 20, 2022 · 2 comments · Fixed by #7215
Closed

Incorrect yield and try-finally behaviour #5913

tomas-c opened this issue Sep 20, 2022 · 2 comments · Fixed by #7215
Assignees
Milestone

Comments

@tomas-c
Copy link

tomas-c commented Sep 20, 2022

Describe the bug

I'm using Next.js with SWC and redux-saga and noticed that updating Next.js from 12.2.1 to 12.3.1 broke redux-saga for me.

Turns out wrapping a yield statement in a try-finally block doesn't behave as it should with SWC.

Input code

(() => {
  function* generator() {
    try {
      while (true) {
        yield "foo";
      }
    } finally {
      yield "bar";
    }
  }

  function test() {
    const gen = generator();
    console.log(gen.next().value);
    console.log(gen.next().value);
    console.log(gen.next().value);
    console.log(gen.next().value);
  }

  test();
})();

Config

Copied from playground defaults:

{
  "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=H4sIAAAAAAAAA72PwQ2DMAxF70zxxSn0wAIR3SWlhkayYimYtqjK7gQaUSboxbb8v%2B1nYxp0V3wqYJhDr17CBSMFik4lZnFTAI1LqYDXwzPBaJypOZrA4onvqAeR2pZm2nPC4INj%2Fm0o1puLxboZU3VigNKkx%2FVewqQbFbozmz1EYWpZRpPFNtA7T7ZPx5nvr5b9gy%2B4rVKT4wq0x%2FTnXgEAAA%3D%3D&config=H4sIAAAAAAAAA0WNSwrDMAxE76J1ts0id%2BghjKsEF%2F%2FQKBBjfPfawSU7aebpqdIXlrZK2QhYxoQS1Vy0kZbMsOKy0kKKHu3Gg1tfjBysHWG8eudTAs92oeCi28sw2RSyMPBUJh7%2BT7YuCulzjqDez27hSu1xzDuH9wRVTm4%2FOkwQUrUAAAA%3D

Expected behavior

Running the input code from the playground outputs

foo
foo
foo
foo

Actual behavior

Running the output code from the playground outputs

foo
bar
foo
bar

Version

1.3.2

Additional context

No response

@tomas-c tomas-c added the C-bug label Sep 20, 2022
@kdy1 kdy1 added this to the Planned milestone Sep 21, 2022
@kdy1 kdy1 self-assigned this Sep 21, 2022
@kdy1 kdy1 removed this from the Planned milestone Sep 21, 2022
@kdy1 kdy1 removed their assignment Sep 21, 2022
@kdy1 kdy1 added this to the Planned milestone Sep 21, 2022
@kdy1 kdy1 self-assigned this Sep 21, 2022
@kdy1 kdy1 added the P-high label Oct 5, 2022
@kdy1
Copy link
Member

kdy1 commented Apr 6, 2023

Expected output:

"use strict";

function generator() {
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                _a.trys.push([0, , 4, 6]);
                _a.label = 1;
            case 1:
                if (!true) return [3 /*break*/, 3];
                return [4 /*yield*/, "foo"];
            case 2:
                _a.sent();
                return [3 /*break*/, 1];
            case 3: return [3 /*break*/, 6];
            case 4: return [4 /*yield*/, "bar"];
            case 5:
                _a.sent();
                return [7 /*endfinally*/];
            case 6: return [2 /*return*/];
        }
    });
}

Output of swc:

function generator() {
    return __generator(this, function(_state) {
        switch(_state.label){
            case 0:
                _state.trys.push([
                    0,
                    ,
                    3,
                    5
                ]);
                if (!true) return [
                    3,
                    2
                ];
                return [
                    4,
                    "foo"
                ];
            case 1:
                _state.sent();
                return [
                    3,
                    0
                ];
            case 2:
                return [
                    3,
                    5
                ];
            case 3:
                return [
                    4,
                    "bar"
                ];
            case 4:
                _state.sent();
                return [
                    7
                ];
            case 5:
                return [
                    2
                ];
        }
    });
}

@kdy1 kdy1 closed this as completed in #7215 Apr 6, 2023
kdy1 added a commit that referenced this issue Apr 6, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.47 Apr 10, 2023
@swc-bot
Copy link
Collaborator

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