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

expose pointer confusion #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/coroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func TestCoroutineYield(t *testing.T) {
yields: []int{0, 3, 6, 9},
},

{
name: "range over yield and assign to pointer",
coro: func() { RangeYieldAndAssign(4) },
yields: []int{0, 1, 2, 3},
},

{
name: "range over closure capturing values",
coro: Range10ClosureCapturingValues,
Expand Down
29 changes: 29 additions & 0 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,35 @@ func RangeTripleFuncValue(n int) {
Range(n, f)
}

func yieldAndAssign(assign *int, yield, value int) {
// The pointer assignment here gets confused because on resume, the variable
// that it refers to is recreated during the call to RangeYieldAndAssign,
// resulting in assigning the value to a different value than the local `i`
// variable of the parent function.
f := func() { *assign = value }

// TODO: remove this function; it's here to ensure that both assign and
// value are seen as potentially being written to, so the compiler doesn't
// dereference their values in the closure type of f.
g := func() { assign = &value }

coroutine.Yield[int, any](yield)
// If we make the call to f before yielding, the coroutine behaves as
// expected because the assign pointer is pointing to the `i` variable of
/// the parent function, but after resuming the pointers aren't the same
// anymore.
f()

// TODO: remove
g()
}

func RangeYieldAndAssign(n int) {
for i := 0; i < n; {
yieldAndAssign(&i, i, i+1)
}
}

func Range10ClosureCapturingValues() {
i := 0
n := 10
Expand Down
115 changes: 115 additions & 0 deletions compiler/testdata/coroutine_durable.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions compiler/testdata/coroutine_functypes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading