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

Compiler: fix bug related to if branch truthiness #7792

Merged
merged 1 commit into from
May 19, 2019

Conversation

asterite
Copy link
Member

Fixes #7222

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic labels May 17, 2019
case node
when .and?
@last_is_truthy = cond_is_truthy && then_is_truthy
@last_is_falsey = cond_is_falsey || then_is_falsey
when .or?
@last_is_truthy = (cond_is_truthy && then_is_truthy) || (cond_is_falsey && else_is_truthy)
@last_is_truthy = cond_is_truthy || else_is_truthy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug was this line, which computes whether an or expression like a || b is always truthy or falsey.

As documented, something like:

a || b

is turned into:

if temp = a # cond
  temp # then
else
  b # else
end

So seeing the above we can say that a || b is truthy when either a or b are truthy. But since the || is turned into the if above, it's when the cond part is truthy or when the else part is truthy (for the cond part the compiler will see through an assignment to check whether the assignment value is truthy).

Also note the symmetry of the resulting code with and.

The bug was that the old condition was nonsense: I didn't fully understand it at that time and just experimented with some combinations... shame on me. Or actually, I thought the version in this PR was correct but it didn't work for some reason. And the reason was that the variables @last_is_truthy and @last_is_falsey weren't reset before reaching this line, so their value was wrong.

So this PR fixes those things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and all of this is computed in order to know whether we can ignore some parts of an if: if the condition is truthy then there's no need to codegen the else part, we can always codegen the then part. Same but reversed when the condition is falsey.

@asterite
Copy link
Member Author

@bcardiff @straight-shoota I left an explanation of what this PR fixes, let me know if you have questions :-)

@asterite asterite added this to the 0.29.0 milestone May 19, 2019
@asterite asterite merged commit 98bf9e5 into crystal-lang:master May 19, 2019
@asterite asterite deleted the bug/if-no-return branch May 19, 2019 16:12
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:semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: trying to downcast NoReturn <- Nil
2 participants