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

Wrong codegen with while and && #4243

Closed
asterite opened this issue Apr 5, 2017 · 1 comment
Closed

Wrong codegen with while and && #4243

asterite opened this issue Apr 5, 2017 · 1 comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Milestone

Comments

@asterite
Copy link
Member

asterite commented Apr 5, 2017

Given:

x = nil
y = nil

while !(x && y)
  pp x
  x = 1
end

The above code will print x # => nil forever, which is wrong because x is clearly assigned the value 1 inside the loop.

The same happens with until:

x = nil
y = nil

until x && y
  pp x
  x = 1
end

(but until exp is currently re-written to while !exp so the above is logical to happen)

Extracted from #4239

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Apr 5, 2017
@asterite asterite modified the milestone: Next Apr 5, 2017
@zatherz
Copy link
Contributor

zatherz commented Apr 6, 2017

Copying over from the other issue:
Using while !(a && b): https://carc.in/#/r/1t8j (unexpected behavior)
Using while !a || !b: https://carc.in/#/r/1t8n (expected behavior)
Using while !(Glob.a && b) (Glob is a class): https://carc.in/#/r/1t8p (expected behavior)
Using while !Glob.a || !b (same result): https://carc.in/#/r/1t8q (expected behavior)
Using while !(a && b), assigning a to Glob.a: https://carc.in/#/r/1t8r (unexpected behavior with a, expected behavior with Glob.a)
Using while !(a && b), assigning a to Glob.a: https://carc.in/#/r/1t8s (expected behavior with both a and Glob.a)

To sum it up, it seems like setting the value of a in the scope of a while !(a && b) actually creates a new local variable instead of modifying the one in the outer scope, while while !a || !b has the expected result of modifying the local variable in the outer scope.

EDIT:
First test, but printing the value of a after the loop: https://carc.in/#/r/1t8u
Actually, it does change the variable of the outer scope, but the value of a at the beginning of the scope of each loop is set back to the initial value before the loop?

EDIT 2:
Expanding the while condition of the first test from !(a && b) to !((a != nil) && b) produces expected behavior: https://carc.in/#/r/1t90
But, expanding the condition from !(a && b) to !(!a.nil? && b) retains the unexpected effect: https://carc.in/#/r/1t92

@asterite asterite added this to the Next milestone Apr 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

No branches or pull requests

2 participants