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 tail calls in to be closed context #65

Merged
merged 2 commits into from
Jan 17, 2022

Conversation

arnodel
Copy link
Owner

@arnodel arnodel commented Jan 13, 2022

Although it is not documented, the Lua 5.4 test suite checks that when returning from a function at a point when there are pending to-be-closed variables, tail-calls are disabled so that the closing action can be performed after the call.

e.g.

function f()
    local x <close> = mkclose()
    return g() -- This would normally be a tail-call
end

This PR implements this restriction.

@arnodel arnodel added the bug label Jan 13, 2022
@arnodel arnodel added this to the Lua 5.4 milestone Jan 13, 2022
@cderici
Copy link

cderici commented Jan 14, 2022

Yeah this makes sense. I initially thought that the close functions were in tail position with respect to the returning function, but I guess the close functions can close over the variables (that are passed by reference types like tables/fucntions) from the caller like

function f()
    t = {1,2}
    local x <close> = mkclose(t)
    local y <close> = mkclose(t)
    return g() -- This would normally be a tail-call
end

so f's frame in the stack needs to stay where it is, meaning they can't guarantee proper tail calls when there's to-be-closed vars, especially when the close functions are just arbitrary code (i.e. can overflow).

@arnodel arnodel merged commit b686ecb into lua5.4 Jan 17, 2022
@arnodel arnodel deleted the fix-tail-calls-in-to-be-closed-context branch January 17, 2022 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants