-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 #13502 a and b
now short-circuits semcheck in VM
#13541
Closed
timotheecour
wants to merge
1
commit into
nim-lang:devel
from
timotheecour:pr_fix_13502_vm_and_shortcircuit
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,13 @@ proc `and`*(x, y: bool): bool {.magic: "And", noSideEffect.} | |
## are true). | ||
## | ||
## Evaluation is lazy: if ``x`` is false, ``y`` will not even be evaluated. | ||
## Semantic check is lazy in VM, to allow `when declared(Foo) and T is Foo` | ||
|
||
template nimInternalAndVM*(x: bool, y: untyped): bool = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new way of "fixing" things by adding implementation details to system.nim is not acceptable. |
||
## Internal lowering used by `and` in VM to enable shortcuiting semcheck | ||
when x: y | ||
else: false | ||
|
||
proc `or`*(x, y: bool): bool {.magic: "Or", noSideEffect.} | ||
## Boolean ``or``; returns true if ``not (not x and not y)`` (if any of | ||
## the arguments is true). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too hacky. I understand why you need to do it this way but I wonder if there is a better way...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to fix this line instead:
Or to add logic to
semmagic.magicsAfterOverloadResolution
? Sem'checking both operands forand
andor
does make sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point,
or
should also short-circuit, I will update PR after the design forand
is accepted (should be straightforward to adapt)But for the remaining points:
How could that work? if you semcheck the 2nd operand for
and
,const a = false and nonexistant
would not compile; nor wouldwhen declared(Foo) and T is Foo: discard
Error: undeclared identifier
would be issued before code reaches semmagic.magicsAfterOverloadResolution or semfold.evalOp.semcheck, which is why I'm doing it instead insemMagic
If you
nim c -r main
with:and you add
and
you'll see:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. As it should be. I'm sorry, I seem to have misunderstood this point previously. Short cut evaluation refers to evaluation, not to the semantic checking of
and
andor
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you seemed to agree here #13502 (comment) it should be legal
feature or bug is a matter of perspective, here's a motivating example from https://github.com/nim-lang/Nim/pull/13498/files#diff-7eb4d1654e7ba69a150250a34148c9c9
or, without ctAnd you'd need to duplicate the else branch (worst option) or write boilerplate:
with this PR you'd simply write:
which is arguably simpler. Are you implying #13502 should be closed as wontfix? I don't see downsides and only upsides to making
when declared(Foo) and T is Foo
work. If it can't be accepted, then we should addctAnd
,ctOr
to stdlibThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it's simpler to use but a weird special case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that I ensured it only affects static/when/const blocks, not general VM code, so that:
seems reasonable feature, if feature is accepted I can make sure doc is clear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I'm really happier without this special case, regardless of "only const/when/static blocks" are affected or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:-( would you accept a modified PR that adds instead
sugar.lazyAnd
,sugar.lazyOr
?and analog for lazyOr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, do not circumvent the RFC process.
lazyAnd
is a good name, but consider that your very own implementation is a single when statement, why not write it as this when the need arises: