Skip to content

Commit

Permalink
expose pointer confusion
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Sep 21, 2023
1 parent 32a5667 commit b664258
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
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.

0 comments on commit b664258

Please sign in to comment.