Skip to content

Commit

Permalink
Reproduce another issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Nov 8, 2023
1 parent 0e63087 commit c42e86c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 31 deletions.
54 changes: 38 additions & 16 deletions compiler/coroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ func init() {

func TestCoroutineYield(t *testing.T) {
tests := []struct {
name string
coro func()
yields []int
skip bool
name string
coro func()
generateCoro func() func()
yields []int
skip bool
}{
{
name: "identity",
Expand Down Expand Up @@ -200,19 +201,25 @@ func TestCoroutineYield(t *testing.T) {
},

{
name: "closure yield",
coro: func() { YieldFromClosure(3) },
yields: []int{3},
name: "closure yield 1",
coro: func() { YieldFromClosure1(1) },
yields: []int{1},
},
}

// This emulates the installation of function type information by the
// compiler because we are not doing codegen for the test files in this
// package.
for _, test := range tests {
a := types.FuncAddr(test.coro)
f := types.FuncByAddr(a)
types.RegisterFunc[func()](f.Name)
{
name: "closure yield 2",
coro: func() { YieldFromClosure2(YieldingClosure(2)) },
yields: []int{2},
},

{
name: "closure yield 3",
generateCoro: func() func() {
fn := YieldingClosure(3)
return func() { YieldFromClosure2(fn) }
},
yields: []int{3},
},
}

for _, test := range tests {
Expand All @@ -221,7 +228,22 @@ func TestCoroutineYield(t *testing.T) {
t.Skip("test is disabled")
}

g := coroutine.New[int, any](test.coro)
fn := test.coro
if fn == nil && test.generateCoro != nil {
fn = test.generateCoro()
}

// This emulates the installation of function type information by the
// compiler because we are not doing codegen for the test files in this
// package.
a := types.FuncAddr(fn)
f := types.FuncByAddr(a)
if f == nil {
panic("not found")
}
types.RegisterFunc[func()](f.Name)

g := coroutine.New[int, any](fn)

var yield int
for g.Next() {
Expand Down
10 changes: 7 additions & 3 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,15 @@ func varArgs(args ...int) {
}
}

func YieldFromClosure(n int) {
closure(n)()
func YieldFromClosure1(n int) {
YieldingClosure(n)()
}

func closure(arg int) func() {
func YieldFromClosure2(fn func()) {
fn()
}

func YieldingClosure(arg int) func() {
return func() {
coroutine.Yield[int, any](arg)
}
Expand Down
29 changes: 17 additions & 12 deletions compiler/testdata/coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,7 @@ func varArgs(_fn0 ...int) {
}

//go:noinline
func YieldFromClosure(_fn0 int) {
func YieldFromClosure1(_fn0 int) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
Expand All @@ -3201,16 +3201,20 @@ func YieldFromClosure(_fn0 int) {
}()
switch {
case _f0.IP < 2:
_f0.X1 = closure(_f0.X0)
_f0.X1 = YieldingClosure(_f0.X0)
_f0.IP = 2
fallthrough
case _f0.IP < 3:
_f0.X1()
}
}

func YieldFromClosure2(fn func()) {
fn()
}

//go:noinline
func closure(_fn0 int) (_ func()) {
func YieldingClosure(_fn0 int) (_ func()) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
Expand Down Expand Up @@ -3347,7 +3351,16 @@ func init() {
X3 []func()
}
}]("github.com/stealthrocket/coroutine/compiler/testdata.YieldAndDeferAssign.func2")
_types.RegisterFunc[func(_fn0 int)]("github.com/stealthrocket/coroutine/compiler/testdata.YieldFromClosure")
_types.RegisterFunc[func(_fn0 int)]("github.com/stealthrocket/coroutine/compiler/testdata.YieldFromClosure1")
_types.RegisterFunc[func(fn func())]("github.com/stealthrocket/coroutine/compiler/testdata.YieldFromClosure2")
_types.RegisterFunc[func(_fn0 int) (_ func())]("github.com/stealthrocket/coroutine/compiler/testdata.YieldingClosure")
_types.RegisterClosure[func(), struct {
F uintptr
X0 *struct {
IP int
X0 int
}
}]("github.com/stealthrocket/coroutine/compiler/testdata.YieldingClosure.func2")
_types.RegisterFunc[func()]("github.com/stealthrocket/coroutine/compiler/testdata.YieldingDurations")
_types.RegisterClosure[func(), struct {
F uintptr
Expand All @@ -3362,13 +3375,5 @@ func init() {
_types.RegisterFunc[func()]("github.com/stealthrocket/coroutine/compiler/testdata.YieldingExpressionDesugaring")
_types.RegisterFunc[func(_fn0 int) (_ int)]("github.com/stealthrocket/coroutine/compiler/testdata.a")
_types.RegisterFunc[func(_fn0 int) (_ int)]("github.com/stealthrocket/coroutine/compiler/testdata.b")
_types.RegisterFunc[func(_fn0 int) (_ func())]("github.com/stealthrocket/coroutine/compiler/testdata.closure")
_types.RegisterClosure[func(), struct {
F uintptr
X0 *struct {
IP int
X0 int
}
}]("github.com/stealthrocket/coroutine/compiler/testdata.closure.func2")
_types.RegisterFunc[func(_fn0 ...int)]("github.com/stealthrocket/coroutine/compiler/testdata.varArgs")
}

0 comments on commit c42e86c

Please sign in to comment.