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

[BUG] VMify "Unresolved Upvalue" error during special repeat until case #171

Open
9382 opened this issue Jul 15, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@9382
Copy link
Contributor

9382 commented Jul 15, 2024

VMify will fail if there's a repeat until statement that references a local in the until condition that was first defined in the repeat's body

Code to reproduce:

repeat
	local x = 5
until x == 5 -- The condition here should see the `local x = 5` above, as they are actually contained in the same scope

The actual code that parses the repeat until (src/prometheus/parser.lua#L225-L233) is parsing the condition with the return's scope, which should be good, but for some reason something later is breaking because of this, and I have no idea what

@9382 9382 added the bug Something isn't working label Jul 15, 2024
@Zaenalos
Copy link

VMIFY Scoping problem.

@Zaenalos
Copy link

Zaenalos commented Jul 16, 2024

that

There is a possible fix for this. 

Possible fix #1

Add a boolean reference in Repeat Statement AST named "hasUpvalue", set it to false if one of the condition variables only refers inside the Repeat loop or true by default; all of this can be done during parsing, and then add a check like this in the compiler.

if statement.hasUpvalue then

    -- Handle the condition first, which means no local variable refers from body to condition, which is safe.

else

   -- Handle the loop body first (this is where the fix should be applied).

end

before the code:

local conditionReg = self:compileExpression(statement.condition, funcDepth, 1)[1];

Possible fix #2:

Add a function in the compiler that safely traverses expressions, so that you can check if there is a local variable inside the Repeat loop that is being used in the condition. 

@levno-710
Copy link
Member

If anyone is willing to fix this issue, I am willing to accept a PR, but I currently do not have time, to fix this myself due to personal reasons.

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

No branches or pull requests

3 participants