Skip to content

Commit 0a62925

Browse files
authored
Fixes (part 3) (#152)
Following on from #148 and #149, this PR contains various minor fixes: * slices can now be used with generics * struct tags are now supported * lint warnings have been fixed
2 parents f730716 + c9119d6 commit 0a62925

File tree

6 files changed

+358
-27
lines changed

6 files changed

+358
-27
lines changed

compiler/coroutine_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ func TestCoroutineYield(t *testing.T) {
294294
coro: func() { GenericStructClosure(3) },
295295
yields: []int{3, 5, 7},
296296
},
297+
298+
{
299+
name: "JSON roundtrip",
300+
coro: func() { JSONRoundTrip(3) },
301+
yields: []int{3, 3},
302+
},
303+
304+
{
305+
name: "generics with slices",
306+
coro: func() { GenericSlice(3) },
307+
yields: []int{0, 1, 2, 0, 1, 2},
308+
},
297309
}
298310

299311
// This emulates the installation of function type information by the

compiler/function.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ func (g *genericInstance) gcshapePath() string {
558558
func writeGoShape(b *strings.Builder, tt types.Type) {
559559
b.WriteString("go.shape.")
560560

561+
writeGoShapeType(b, tt)
562+
}
563+
564+
func writeGoShapeType(b *strings.Builder, tt types.Type) {
561565
switch t := tt.Underlying().(type) {
562566
case *types.Basic:
563567
b.WriteString(t.Name())
@@ -588,6 +592,9 @@ func writeGoShape(b *strings.Builder, tt types.Type) {
588592
b.WriteString(f.Type().String())
589593
}
590594
b.WriteString(" }")
595+
case *types.Slice:
596+
b.WriteString("[]")
597+
writeGoShapeType(b, t.Elem())
591598
default:
592599
panic(fmt.Sprintf("not implemented: %#v (%T)", tt, t))
593600
}

compiler/testdata/coroutine.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package testdata
44

55
import (
6+
"encoding/json"
7+
"fmt"
68
"math"
79
"reflect"
810
"time"
@@ -777,3 +779,54 @@ type GenericAdder[A adder] struct{ adder A }
777779
func (b *GenericAdder[A]) Add(n int) int {
778780
return b.adder.Add(n)
779781
}
782+
783+
func JSONRoundTrip(n int) {
784+
b, err := json.Marshal(struct {
785+
N int `json:"n"`
786+
}{n})
787+
if err != nil {
788+
panic(err)
789+
}
790+
if string(b) != fmt.Sprintf(`{"n":%d}`, n) {
791+
panic(fmt.Errorf("unexpected JSON: %v", b))
792+
}
793+
794+
coroutine.Yield[int, any](n)
795+
796+
var result struct {
797+
N int `json:"n"`
798+
}
799+
if err := json.Unmarshal(b, &result); err != nil {
800+
panic(err)
801+
}
802+
coroutine.Yield[int, any](result.N)
803+
}
804+
805+
type Cloner[S ~[]E, E any] struct {
806+
Slice S
807+
}
808+
809+
func (c *Cloner[S, E]) Clone() S {
810+
s2 := make(S, len(c.Slice))
811+
copy(s2, c.Slice)
812+
return s2
813+
}
814+
815+
func GenericSlice(n int) {
816+
ints := make([]int, n)
817+
for i := range ints {
818+
ints[i] = i
819+
}
820+
for _, x := range ints {
821+
coroutine.Yield[int, any](x)
822+
}
823+
824+
cloner := &Cloner[[]int, int]{Slice: ints}
825+
ints2 := cloner.Clone()
826+
827+
clear(ints)
828+
829+
for _, x := range ints2 {
830+
coroutine.Yield[int, any](x)
831+
}
832+
}

0 commit comments

Comments
 (0)