-
Notifications
You must be signed in to change notification settings - Fork 43
for loops over .passive iterators: three fixes, no new protocol for now #2570
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
Closed
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # `for` loops over `.passive` iterators. | ||
| # | ||
| # Three things that used to be wrong: | ||
| # 1. a loop in a `.passive` routine did not compile at all — the iterator call | ||
| # was taken for a suspension point of the routine, which put a state proc | ||
| # inside the loop; and the loop variable was not redirected into the frame; | ||
| # 2. a passive call inside the iterator returns into the iterator's frame, and | ||
| # the trampoline's frame-identity test read that as a yield, running the | ||
| # body again with the previous value; | ||
| # 3. a `{.cast(...)}:` block anywhere in a coroutine crashed hexer. | ||
| # | ||
| # The loop is a trampoline inside ONE state proc, so its body cannot suspend: | ||
| # no passive call, no `yield`, no park in the iterator. Each of those is | ||
| # refused — the first two at compile time, the park at run time. | ||
|
|
||
| import std / syncio | ||
|
|
||
| proc step() {.passive.} = discard | ||
|
|
||
| iterator count(n: int): int {.passive.} = | ||
| var i = 1 | ||
| while i <= n: | ||
| yield i | ||
| {.cast(noSideEffect).}: | ||
| step() # a passive call BETWEEN two yields | ||
| inc i | ||
|
|
||
| proc fromRegular() = | ||
| for x in count(3): | ||
| echo "regular ", x | ||
|
|
||
| proc fromPassive() {.passive.} = | ||
| var sum = 0 | ||
| for x in count(3): # `x` lives in this coroutine's frame | ||
| sum += x | ||
| echo "passive ", x | ||
| echo "passive sum ", sum | ||
|
|
||
| proc breaks() {.passive.} = | ||
| for x in count(10): | ||
| if x == 3: break | ||
| echo "break ", x | ||
|
|
||
| proc returns(): int {.passive.} = | ||
| for x in count(10): | ||
| if x == 4: return x | ||
| result = -1 | ||
|
|
||
| proc nested() {.passive.} = | ||
| for a in count(2): | ||
| for b in count(2): | ||
| echo "nested ", a, " ", b | ||
|
|
||
| proc pragmaBlock() {.passive.} = | ||
| echo "before" | ||
| {.cast(noSideEffect).}: | ||
| step() | ||
| echo "after" | ||
|
|
||
| fromRegular() | ||
| fromPassive() | ||
| breaks() | ||
| echo "returns ", returns() | ||
| nested() | ||
| pragmaBlock() | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| regular 1 | ||
| regular 2 | ||
| regular 3 | ||
| passive 1 | ||
| passive 2 | ||
| passive 3 | ||
| passive sum 6 | ||
| break 1 | ||
| break 2 | ||
| returns 4 | ||
| nested 1 1 | ||
| nested 1 2 | ||
| nested 2 1 | ||
| nested 2 2 | ||
| before | ||
| after |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| tests/nimony/errmsgs/tcoroforsuspend.nim(14, 7) Error: a `for` loop over a `.passive` iterator cannot suspend in its body |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # A `for` loop over a `.passive` iterator is a trampoline inside ONE state | ||
| # proc, so its body cannot suspend: there is nowhere for the state boundary to | ||
| # go. Refused rather than mislowered. | ||
|
|
||
| proc step() {.passive.} = discard | ||
|
|
||
| iterator count(n: int): int {.passive.} = | ||
| var i = 1 | ||
| while i <= n: | ||
| yield i | ||
| inc i | ||
|
|
||
| proc consume() {.passive.} = | ||
| for x in count(3): | ||
| step() |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
if it cannot suspend what is difference between simple iterators and passive iterators?