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

Fix empty return with named results #120

Merged
merged 2 commits into from
Nov 23, 2023
Merged

Fix empty return with named results #120

merged 2 commits into from
Nov 23, 2023

Conversation

pelletier
Copy link
Contributor

When a function has named results and uses empty return, the named result would correctly be renamed and hoisted into the frame struct. In that scenario, using an explicit return (such as "return myvalue") works, because the renaming pass goes over the expression in the return statement and replaces the named result with a selector into the frame struct.

However, when using an empty return (just "return"), the named result is not in the AST, and the renaming pass would not see it.

This patch add and final pass to the renaming procedure to change empty returns in functions with named results into explicit returns using selectors into the frame struct. For example:

func example() (out int) {
    out = 42
    return
}

gets rewritten into:

func example() (_fn0 int) {
    var _f0 *struct {
        IP int
        X0 int
    }
    _f0.X0 = 42
    return _f0.X0
}

(other transformations omitted for this example)

When a function has named results and uses empty return, the named result would
correctly be renamed and hoisted into the frame struct. In that scenario, using
an explicit return (such as "return myvalue") works, because the renaming pass
goes over the expression in the return statement and replaces the named result
with a selector into the frame struct.

However, when using an empty return (just "return"), the named result is not in
the AST, and the renaming pass would not see it.

This patch add and final pass to the renaming procedure to change empty returns
in functions with named results into explicit returns using selectors into the
frame struct. For example:

    func example() (out int) {
        out = 42
        return
    }

gets rewritten into:

    func example() (_fn0 int) {
        var _f0 *struct {
            IP int
            X0 int
        }
        _f0.X0 = 42
        return _f0.X0
    }

(other transformations omitted for this example)
@pelletier pelletier added the bug Something isn't working label Nov 23, 2023
@pelletier pelletier merged commit 0fe43fc into main Nov 23, 2023
2 checks passed
@pelletier pelletier deleted the fix-named-returns branch November 23, 2023 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants