Skip to content

Commit

Permalink
Test generics with slices
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 28, 2024
1 parent 02e002b commit 242f88c
Show file tree
Hide file tree
Showing 3 changed files with 176 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 @@ -300,6 +300,12 @@ func TestCoroutineYield(t *testing.T) {
coro: func() { JSONRoundTrip(3) },
yields: []int{3, 3},
},

{
name: "generics with slices",
coro: func() { GenericSlice(3) },
yields: []int{0, 1, 2, 0, 1, 2},
},
}

// This emulates the installation of function type information by the
Expand Down
29 changes: 29 additions & 0 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,32 @@ func JSONRoundTrip(n int) {
}
coroutine.Yield[int, any](result.N)
}

type Cloner[S ~[]E, E any] struct {
Slice S
}

func (c *Cloner[S, E]) Clone() S {
s2 := make(S, len(c.Slice))
copy(s2, c.Slice)
return s2
}

func GenericSlice(n int) {
ints := make([]int, n)
for i := range ints {
ints[i] = i
}
for _, x := range ints {
coroutine.Yield[int, any](x)
}

cloner := &Cloner[[]int, int]{Slice: ints}
ints2 := cloner.Clone()

clear(ints)

for _, x := range ints2 {
coroutine.Yield[int, any](x)
}
}
141 changes: 141 additions & 0 deletions compiler/testdata/coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4376,6 +4376,145 @@ func JSONRoundTrip(_fn0 int) {
coroutine.Yield[int, any](_f0.X6.N)
}
}

type Cloner[S ~[]E, E any] struct {
Slice S
}

func (c *Cloner[S, E]) Clone() S {
s2 := make(S, len(c.Slice))
copy(s2, c.Slice)
return s2
}

//go:noinline
func GenericSlice(_fn0 int) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
X0 int
X1 []int
X2 []int
X3 int
X4 int
X5 *Cloner[[]int, int]
X6 []int
X7 []int
X8 int
X9 int
} = coroutine.Push[struct {
IP int
X0 int
X1 []int
X2 []int
X3 int
X4 int
X5 *Cloner[[]int, int]
X6 []int
X7 []int
X8 int
X9 int
}](&_c.Stack)
if _f0.IP == 0 {
*_f0 = struct {
IP int
X0 int
X1 []int
X2 []int
X3 int
X4 int
X5 *Cloner[[]int, int]
X6 []int
X7 []int
X8 int
X9 int
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f0.IP < 2:
_f0.X1 = make([]int, _f0.X0)
_f0.IP = 2
fallthrough
case _f0.IP < 3:
for i := range _f0.X1 {
_f0.X1[i] = i
}
_f0.IP = 3
fallthrough
case _f0.IP < 7:
switch {
case _f0.IP < 4:
_f0.X2 = _f0.X1
_f0.IP = 4
fallthrough
case _f0.IP < 7:
switch {
case _f0.IP < 5:
_f0.X3 = 0
_f0.IP = 5
fallthrough
case _f0.IP < 7:
for ; _f0.X3 < len(_f0.X2); _f0.X3, _f0.IP = _f0.X3+1, 5 {
switch {
case _f0.IP < 6:
_f0.X4 = _f0.X2[_f0.X3]
_f0.IP = 6
fallthrough
case _f0.IP < 7:

coroutine.Yield[int, any](_f0.X4)
}
}
}
}
_f0.IP = 7
fallthrough
case _f0.IP < 8:
_f0.X5 = &Cloner[[]int, int]{Slice: _f0.X1}
_f0.IP = 8
fallthrough
case _f0.IP < 9:
_f0.X6 = _f0.X5.Clone()
_f0.IP = 9
fallthrough
case _f0.IP < 10:

clear(_f0.X1)
_f0.IP = 10
fallthrough
case _f0.IP < 14:
switch {
case _f0.IP < 11:
_f0.X7 = _f0.X6
_f0.IP = 11
fallthrough
case _f0.IP < 14:
switch {
case _f0.IP < 12:
_f0.X8 = 0
_f0.IP = 12
fallthrough
case _f0.IP < 14:
for ; _f0.X8 < len(_f0.X7); _f0.X8, _f0.IP = _f0.X8+1, 12 {
switch {
case _f0.IP < 13:
_f0.X9 = _f0.X7[_f0.X8]
_f0.IP = 13
fallthrough
case _f0.IP < 14:

coroutine.Yield[int, any](_f0.X9)
}
}
}
}
}
}
func init() {
_types.RegisterFunc[func(_fn1 int) (_ func(int))]("github.com/dispatchrun/coroutine/compiler/testdata.(*Box).Closure")
_types.RegisterClosure[func(_fn0 int), struct {
Expand All @@ -4387,6 +4526,7 @@ func init() {
}
}]("github.com/dispatchrun/coroutine/compiler/testdata.(*Box).Closure.func1")
_types.RegisterFunc[func()]("github.com/dispatchrun/coroutine/compiler/testdata.(*Box).YieldAndInc")
_types.RegisterFunc[func() (_ []int)]("github.com/dispatchrun/coroutine/compiler/testdata.(*Cloner[go.shape.[]int,go.shape.int]).Clone")
_types.RegisterFunc[func(n int) (_ int)]("github.com/dispatchrun/coroutine/compiler/testdata.(*GenericAdder[go.shape.struct { github.com/dispatchrun/coroutine/compiler/testdata.base int; github.com/dispatchrun/coroutine/compiler/testdata.mul int }]).Add")
_types.RegisterFunc[func(_fn1 int) (_ func(int))]("github.com/dispatchrun/coroutine/compiler/testdata.(*GenericBox[go.shape.int]).Closure")
_types.RegisterClosure[func(_fn0 int), struct {
Expand Down Expand Up @@ -4417,6 +4557,7 @@ func init() {
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.EvenSquareGenerator")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.FizzBuzzIfGenerator")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.FizzBuzzSwitchGenerator")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.GenericSlice")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.GenericStructClosure")
_types.RegisterFunc[func(n int)]("github.com/dispatchrun/coroutine/compiler/testdata.Identity")
_types.RegisterFunc[func(n int)]("github.com/dispatchrun/coroutine/compiler/testdata.IdentityGenericClosureInt")
Expand Down

0 comments on commit 242f88c

Please sign in to comment.