-
Notifications
You must be signed in to change notification settings - Fork 28
/
typeparams_test.go
1192 lines (1037 loc) · 33.1 KB
/
typeparams_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2022 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogen_test
import (
"go/token"
"go/types"
"log"
"runtime"
"testing"
"github.com/goplus/gogen"
"github.com/goplus/gogen/internal/goxdbg"
)
func TestMethodToFunc(t *testing.T) {
const src = `package hello
type Itf interface {
X()
}
type Base struct {
}
func (p Base) F() {}
func (p *Base) PtrF() {}
type Foo struct {
Itf
Base
Val byte
}
func (a Foo) Bar() int {
return 0
}
func (a *Foo) PtrBar() string {
return ""
}
var _ = (Foo).Bar
var _ = (*Foo).PtrBar
var _ = (Foo).F
var _ = (*Foo).PtrF
var _ = (Foo).X
var _ = (*Foo).X
var _ = (Itf).X
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("hello", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("hello")
objFoo := pkgRef.Ref("Foo")
objItf := pkgRef.Ref("Itf")
typ := objFoo.Type()
typItf := objItf.Type()
_, err = pkg.MethodToFunc(typ, "Val")
if err == nil || err.Error() != "-: undefined (type hello.Foo has no method Val)" {
t.Fatal("MethodToFunc failed:", err)
}
checkMethodToFunc(t, pkg, typ, "Bar", "(hello.Foo).Bar")
checkMethodToFunc(t, pkg, types.NewPointer(typ), "PtrBar", "(*hello.Foo).PtrBar")
checkMethodToFunc(t, pkg, typ, "PtrBar", "(*hello.Foo).PtrBar")
checkMethodToFunc(t, pkg, typ, "F", "(hello.Foo).F")
checkMethodToFunc(t, pkg, types.NewPointer(typ), "PtrF", "(*hello.Foo).PtrF")
checkMethodToFunc(t, pkg, typ, "PtrF", "(*hello.Foo).PtrF")
checkMethodToFunc(t, pkg, typItf, "X", "(hello.Itf).X")
checkMethodToFunc(t, pkg, typ, "X", "(hello.Foo).X")
checkMethodToFunc(t, pkg, types.NewPointer(typ), "X", "(*hello.Foo).X")
}
func checkMethodToFunc(t *testing.T, pkg *gogen.Package, typ types.Type, name, code string) {
t.Helper()
ret, err := pkg.MethodToFunc(typ, name)
if err != nil {
t.Fatal("MethodToFunc failed:", err)
}
if _, isPtr := typ.(*types.Pointer); isPtr {
if recv := ret.Type.(*types.Signature).Params().At(0); !types.Identical(recv.Type(), typ) {
t.Fatalf("MethodToFunc: ResultType: %v, Expected: %v\n", recv.Type(), typ)
}
}
if v := goxdbg.Format(pkg.Fset, ret.Val); v != code {
t.Fatalf("MethodToFunc:\nResult:\n%s\nExpected:\n%s\n", v, code)
}
}
func TestTypeAsParamsFunc(t *testing.T) {
const src = `package foo
const GopPackage = true
type basetype interface {
int | string
}
func Gopx_Bar[T basetype](name string) {
}
func Gopx_Row__0[T basetype](name string) {
}
func Gopx_Row__1[Array any](v int) {
}
type Table struct {
}
func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
}
func Gopt_Table_Gopx_Col__1[Array any](p *Table, v int) {
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "test")
foo := pkg.Import("foo")
objTable := foo.Ref("Table")
typ := objTable.Type().(*types.Named)
tyInt := types.Typ[types.Int]
cb := pkg.NewFunc(nil, "Example", nil, nil, false).BodyStart(pkg).
NewVar(types.NewPointer(typ), "tbl")
_, err = cb.VarVal("tbl").Member("col", gogen.MemberFlagMethodAlias)
if err != nil {
t.Fatal("tbl.Member(col):", err)
}
cb.Typ(tyInt).Val("bar").Call(2).EndStmt().
Val(foo.Ref("Bar")).Typ(tyInt).Val("1").Call(2).EndStmt().
Val(foo.Ref("Row")).Typ(tyInt).Val(1, source("1")).Call(2).EndStmt().
End()
domTest(t, pkg, `package test
import "foo"
func Example() {
var tbl *foo.Table
foo.Gopt_Table_Gopx_Col__0[int](tbl, "bar")
foo.Gopx_Bar[int]("1")
foo.Gopx_Row__1[int](1)
}
`)
}
func TestCheckGopPkg(t *testing.T) {
const src = `package foo
import "io"
const GopPackage = "io"
type basetype interface {
int | string
}
func Gopx_Bar[T basetype](name string) {
}
func Gopx_Row__0[T basetype](name string) {
}
func Gopx_Row__1[Array any](v int) {
}
type EmbIntf interface {
io.Reader
Close()
}
type Table struct {
EmbIntf
N int
b string
}
func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
}
func Gopt_Table_Gopx_Col__1[Array any](p *Table, v int) {
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "test")
foo := pkg.Import("foo")
objTable := foo.Ref("Table")
typ := objTable.Type().(*types.Named)
tyInt := types.Typ[types.Int]
typSlice := types.NewSlice(types.NewPointer(typ))
typMap := types.NewMap(types.Typ[types.String], typSlice)
args := types.NewTuple(types.NewParam(0, pkg.Types, "tbls", typMap))
cb := pkg.NewFunc(nil, "Example", args, nil, false).BodyStart(pkg)
_, err = cb.VarVal("tbls").Val("Hi").Index(1, false).Val(0).Index(1, false).Member("col", gogen.MemberFlagMethodAlias)
if err != nil {
t.Fatal("tbl.Member(col):", err)
}
cb.Typ(tyInt).Val("bar").Call(2).EndStmt().
Val(foo.Ref("Bar")).Typ(tyInt).Val("1").Call(2).EndStmt().
Val(foo.Ref("Row")).Typ(tyInt).Val(1, source("1")).Call(2).EndStmt().
End()
typChan := types.NewChan(types.SendRecv, typSlice)
typArray := types.NewArray(typChan, 2)
args = types.NewTuple(types.NewParam(0, pkg.Types, "", typArray))
pkg.NewFunc(nil, "Create", args, nil, false).BodyStart(pkg).End()
domTest(t, pkg, `package test
import "foo"
const GopPackage = "foo"
func Example(tbls map[string][]*foo.Table) {
foo.Gopt_Table_Gopx_Col__0[int](tbls["Hi"][0], "bar")
foo.Gopx_Bar[int]("1")
foo.Gopx_Row__1[int](1)
}
func Create([2]chan []*foo.Table) {
}
`)
}
func TestOverloadNamed(t *testing.T) {
const src = `package foo
const GopPackage = true
type M = map[string]any
type basetype interface {
string | int | bool | float64
}
type Var__0[T basetype] struct {
val T
}
type Var__1[T map[string]any] struct {
val T
}
func Gopx_Var_Cast__0[T basetype]() *Var__0[T] {
return new(Var__0[T])
}
func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] {
return new(Var__1[T])
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
scope := pkgRef.Types.Scope()
log.Println("==> Lookup", scope.Lookup("Var__0"))
objVar := pkgRef.Ref("Var")
typ := objVar.Type()
on, ok := gogen.CheckOverloadNamed(typ)
if !ok {
t.Fatal("TestOverloadNamed: not TyOverloadNamed?")
}
if on.Types[0].TypeParams() == nil {
t.Fatal("TestOverloadNamed: not generic")
}
tyInt := types.Typ[types.Int]
tyM := pkgRef.Ref("M").Type()
ty1 := pkg.Instantiate(typ, []types.Type{tyInt})
ty2 := pkg.Instantiate(typ, []types.Type{tyM})
pkg.NewTypeDefs().NewType("t1").InitType(pkg, ty1)
pkg.NewTypeDefs().NewType("t2").InitType(pkg, ty2)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(objVar).Typ(tyInt).Call(1).EndStmt().
Val(objVar).Typ(tyM).Call(1).EndStmt().
End()
domTest(t, pkg, `package main
import "foo"
type t1 foo.Var__0[int]
type t2 foo.Var__1[map[string]any]
func main() {
foo.Gopx_Var_Cast__0[int]()
foo.Gopx_Var_Cast__1[map[string]any]()
}
`)
func() {
defer func() {
if e := recover(); e == nil {
t.Fatal("TestOverloadNamed failed: no error?")
}
}()
ty3 := pkg.Instantiate(on, []types.Type{gogen.TyByte})
pkg.NewTypeDefs().NewType("t3").InitType(pkg, ty3)
}()
func() {
defer func() {
if e := recover(); e != nil && e.(error).Error() != "-: 1 (type untyped int) is not a type" {
t.Fatal("TestOverloadNamed failed:", e)
}
}()
pkg.NewFunc(nil, "bar", nil, nil, false).BodyStart(pkg).
Val(objVar).Val(1, source("1")).Call(1).EndStmt().
End()
}()
}
func TestInstantiate(t *testing.T) {
const src = `package foo
type Data[T any] struct {
v T
}
type sliceOf[E any] interface {
~[]E
}
type Slice[S sliceOf[T], T any] struct {
Data S
}
func (p *Slice[S, T]) Append(t ...T) S {
p.Data = append(p.Data, t...)
return p.Data
}
type (
DataInt = Data[int]
SliceInt = Slice[[]int,int]
)
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
tyData := pkgRef.Ref("Data").Type()
tyInt := types.Typ[types.Int]
tyInvalid := types.Typ[types.Invalid]
tySlice := pkgRef.Ref("Slice").Type()
if ret := pkg.Instantiate(tyData, []types.Type{tyInt}); ret == tyInvalid {
t.Fatal("TestInstantiate failed: pkg.Instantiate")
}
func() {
defer func() {
if e := recover(); e.(error).Error() != "-: int is not a generic type" {
t.Fatal("TestInstantiate failed:", e)
}
}()
pkg.Instantiate(tyInt, nil)
}()
func() {
defer func() {
if e := recover(); e == nil {
t.Fatal("TestInstantiate failed: no error?")
}
}()
pkg.Instantiate(tySlice, []types.Type{tyInt})
}()
}
func TestTypeParamsType(t *testing.T) {
const src = `package foo
type Data[T any] struct {
v T
}
type sliceOf[E any] interface {
~[]E
}
type Slice[S sliceOf[T], T any] struct {
Data S
}
func (p *Slice[S, T]) Append(t ...T) S {
p.Data = append(p.Data, t...)
return p.Data
}
type (
DataInt = Data[int]
SliceInt = Slice[[]int,int]
)
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
tySlice := pkgRef.Ref("Slice").Type()
tySliceInt := pkgRef.Ref("SliceInt").Type()
tyData := pkgRef.Ref("Data").Type()
tyDataInt := pkgRef.Ref("DataInt").Type()
tyInt := types.Typ[types.Int]
tyIntSlice := types.NewSlice(tyInt)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVarStart(types.NewPointer(tyDataInt), "data").Typ(tyData).Typ(tyInt).Index(1, false).Star().Val(nil).Call(1).EndInit(1).
NewVarStart(types.NewPointer(tySliceInt), "slice").Typ(tySlice).Typ(tyIntSlice).Typ(tyInt).Index(2, false).Star().Val(nil).Call(1).EndInit(1).
End()
domTest(t, pkg, `package main
import "foo"
func main() {
var data *foo.Data[int] = (*foo.Data[int])(nil)
var slice *foo.Slice[[]int, int] = (*foo.Slice[[]int, int])(nil)
}
`)
}
func TestTypeParamsFunc(t *testing.T) {
const src = `package foo
type Number interface {
~int | float64
}
func Sum[T Number](vec []T) T {
var sum T
for _, elt := range vec {
sum = sum + elt
}
return sum
}
func At[T interface{ ~[]E }, E any](x T, i int) E {
return x[i]
}
func Loader[T1 any, T2 any](p1 T1, p2 T2) T1 {
return p1
}
func Add[T1 any, T2 ~int](v1 T1, v2 ...T2) (sum T2) {
println(v1)
for _, v := range v2 {
sum += v
}
return sum
}
type Int []int
var MyInts = Int{1,2,3,4}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnSum := pkgRef.Ref("Sum")
fnAt := pkgRef.Ref("At")
fnLoader := pkgRef.Ref("Loader")
fnAdd := pkgRef.Ref("Add")
myInts := pkgRef.Ref("MyInts")
tyInt := types.Typ[types.Int]
tyString := types.Typ[types.String]
tyIntSlice := types.NewSlice(tyInt)
tyIntPointer := types.NewPointer(tyInt)
var fn1 *types.Var
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
VarRef(nil).Val(fnAt).Typ(tyIntSlice).Index(1, false).Assign(1, 1).
VarRef(nil).Val(fnSum).Typ(tyInt).Index(1, false).Assign(1, 1).
VarRef(nil).Val(fnLoader).Typ(tyInt).Typ(tyInt).Index(2, false).Assign(1, 1).
VarRef(nil).Val(fnAdd).Typ(tyString).Typ(tyInt).Index(2, false).Assign(1, 1).
NewVarStart(tyInt, "s1").Val(fnSum).Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).Call(1).EndInit(1).
NewVarStart(tyInt, "s2").Val(fnSum).Typ(tyInt).Index(1, false).Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).Call(1).EndInit(1).
NewVarStart(tyInt, "v1").Val(fnAt).Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).Val(1).Call(2).EndInit(1).
NewVarStart(tyInt, "v2").Val(fnAt).Typ(tyIntSlice).Index(1, false).Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).Val(1).Call(2).EndInit(1).
NewVarStart(tyInt, "v3").Val(fnAt).Typ(tyIntSlice).Typ(tyInt).Index(2, false).Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).Val(1).Call(2).EndInit(1).
NewVarStart(tyInt, "n1").Val(fnAdd).Val("hello").Val(1).Val(2).Val(3).Call(4).EndInit(1).
NewVarStart(tyInt, "n2").Val(fnAdd).Typ(tyString).Index(1, false).Val("hello").Val(1).Val(2).Val(3).Call(4).EndInit(1).
NewVarStart(tyInt, "n3").Val(fnAdd).Typ(tyString).Typ(tyInt).Index(2, false).Val("hello").Val(1).Val(2).Val(3).Call(4).EndInit(1).
NewVarStart(tyInt, "n4").Val(fnAdd).Val("hello").Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).CallWith(2, gogen.InstrFlagEllipsis).EndInit(1).
NewVarStart(tyInt, "n5").Val(fnAdd).Val("hello").Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).CallWith(2, gogen.InstrFlagEllipsis).EndInit(1).
NewVarStart(tyInt, "n6").Val(fnAdd).Typ(tyString).Index(1, false).Val("hello").Val(myInts).CallWith(2, gogen.InstrFlagEllipsis).EndInit(1).
NewVarStart(tyInt, "n7").Val(fnAdd).Typ(tyString).Typ(tyInt).Index(2, false).Val("hello").Val(1).Val(2).Val(3).SliceLit(tyIntSlice, 3).CallWith(2, gogen.InstrFlagEllipsis).EndInit(1).
NewVarStart(tyIntPointer, "p1").Val(fnLoader).Typ(tyIntPointer).Index(1, false).Val(nil).Val(1).Call(2).EndInit(1).
NewVarStart(tyIntPointer, "p2").Val(fnLoader).Typ(tyIntPointer).Typ(tyInt).Index(2, false).Val(nil).Val(1).Call(2).EndInit(1).
NewAutoVar(0, "fn1", &fn1).VarRef(fn1).Val(fnLoader).Typ(tyIntPointer).Typ(tyInt).Index(2, false).Assign(1, 1).EndStmt().
Val(fn1).Val(nil).Val(1).Call(2).EndStmt().
End()
domTest(t, pkg, `package main
import "foo"
func main() {
_ = foo.At[[]int]
_ = foo.Sum[int]
_ = foo.Loader[int, int]
_ = foo.Add[string, int]
var s1 int = foo.Sum([]int{1, 2, 3})
var s2 int = foo.Sum[int]([]int{1, 2, 3})
var v1 int = foo.At([]int{1, 2, 3}, 1)
var v2 int = foo.At[[]int]([]int{1, 2, 3}, 1)
var v3 int = foo.At[[]int, int]([]int{1, 2, 3}, 1)
var n1 int = foo.Add("hello", 1, 2, 3)
var n2 int = foo.Add[string]("hello", 1, 2, 3)
var n3 int = foo.Add[string, int]("hello", 1, 2, 3)
var n4 int = foo.Add("hello", []int{1, 2, 3}...)
var n5 int = foo.Add("hello", []int{1, 2, 3}...)
var n6 int = foo.Add[string]("hello", foo.MyInts...)
var n7 int = foo.Add[string, int]("hello", []int{1, 2, 3}...)
var p1 *int = foo.Loader[*int](nil, 1)
var p2 *int = foo.Loader[*int, int](nil, 1)
var fn1 func(p1 *int, p2 int) *int
fn1 = foo.Loader[*int, int]
fn1(nil, 1)
}
`)
}
func TestTypeParamsErrorInstantiate(t *testing.T) {
const src = `package foo
type Number interface {
~int | float64
}
func Sum[T Number](vec []T) T {
var sum T
for _, elt := range vec {
sum = sum + elt
}
return sum
}
var SumInt = Sum[int]
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnSum := pkgRef.Ref("Sum")
tyUint := types.Typ[types.Uint]
var msg string
switch runtime.Version()[:6] {
case "go1.18":
msg = `./foo.gop:5:40: uint does not implement foo.Number`
case "go1.19":
msg = `./foo.gop:5:40: uint does not implement foo.Number (uint missing in ~int | float64)`
default:
msg = `./foo.gop:5:40: uint does not satisfy foo.Number (uint missing in ~int | float64)`
}
codeErrorTestEx(t, pkg, msg, func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "sum").Val(fnSum).Typ(tyUint).Index(1, false, source(`foo.Sum[uint]`, 5, 40)).EndInit(1).
End()
})
}
func TestTypeParamsErrorMatch(t *testing.T) {
const src = `package foo
func At[T interface{ ~[]E }, E any](x T, i int) E {
return x[i]
}
var AtInt = At[[]int]
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnAt := pkgRef.Ref("At")
tyAtInt := pkgRef.Ref("AtInt").Type()
tyInt := types.Typ[types.Int]
var msg string
switch runtime.Version()[:6] {
case "go1.18", "go1.19":
msg = `./foo.gop:5:40: T does not match ~[]E`
case "go1.20":
msg = `./foo.gop:5:40: int does not match ~[]E`
default:
msg = `./foo.gop:5:40: T (type int) does not satisfy interface{~[]E}`
}
codeErrorTestEx(t, pkg, msg, func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVarStart(tyAtInt, "at").Val(fnAt).Typ(tyInt).Index(1, false, source(`foo.At[int]`, 5, 40)).EndInit(1).
End()
})
}
func TestTypeParamsErrInferFunc(t *testing.T) {
const src = `package foo
func Loader[T1 any, T2 any](p1 T1, p2 T2) T1 {
return p1
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnLoader := pkgRef.Ref("Loader")
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: cannot infer T2 (foo.go:3:21)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "v1").Val(fnLoader).Typ(tyInt).Index(1, false, source(`v1 := foo.Loader[int]`, 5, 40)).EndInit(1).
End()
})
}
func TestTypeParamsErrArgumentsParameters1(t *testing.T) {
const src = `package foo
type Data[T1 any, T2 any] struct {
v1 T1
v2 T2
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
tyData := pkgRef.Ref("Data").Type()
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: got 1 type arguments but foo.Data[T1, T2 any] has 2 type parameters`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "v1").Typ(tyData).Typ(tyInt).Index(1, false, source(`foo.Data[int]`, 5, 40)).Star().Val(nil).Call(1).EndInit(1).
End()
})
}
func TestTypeParamsErrArgumentsParameters2(t *testing.T) {
const src = `package foo
type Data[T1 any, T2 any] struct {
v1 T1
v2 T2
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
tyData := pkgRef.Ref("Data").Type()
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: got 3 type arguments but foo.Data[T1, T2 any] has 2 type parameters`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "v1").Typ(tyData).Typ(tyInt).Typ(tyInt).Typ(tyInt).Index(3, false, source(`foo.Data[int,int,int]`, 5, 40)).Star().Val(nil).Call(1).EndInit(1).
End()
})
}
func TestTypeParamsErrArgumentsParameters3(t *testing.T) {
const src = `package foo
func Test[T1 any, T2 any](t1 T1, t2 T2) {
println(t1,t2)
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnTest := pkgRef.Ref("Test")
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: got 3 type arguments but func[T1, T2 any](t1 T1, t2 T2) has 2 type parameters`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnTest).Typ(tyInt).Typ(tyInt).Typ(tyInt).Index(3, false, source(`foo.Test[int,int,int]`, 5, 40)).Val(1).Val(1).Call(2).EndStmt().
End()
})
}
func TestTypeParamsErrCallArguments1(t *testing.T) {
const src = `package foo
func Test[T1 any, T2 any](t1 T1, t2 T2) {
println(t1,t2)
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnTest := pkgRef.Ref("Test")
codeErrorTestEx(t, pkg, `./foo.gop:5:40: not enough arguments in call to foo.Test
have (untyped int)
want (T1, T2)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnTest).Val(1).CallWith(1, 0, source("foo.Test(1)", 5, 40)).EndStmt().
End()
})
}
func TestTypeParamsErrCallArguments2(t *testing.T) {
const src = `package foo
func Test[T1 any, T2 any](t1 T1, t2 T2) {
println(t1,t2)
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnTest := pkgRef.Ref("Test")
codeErrorTestEx(t, pkg, `./foo.gop:5:40: too many arguments in call to foo.Test
have (untyped int, untyped int, untyped int)
want (T1, T2)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnTest).Val(1).Val(2).Val(3).CallWith(3, 0, source("foo.Test(1,2,3)", 5, 40)).EndStmt().
End()
})
}
func TestTypeParamsErrCallArguments3(t *testing.T) {
const src = `package foo
func Test[T1 any, T2 any]() {
var t1 T1
var t2 T2
println(t1,t2)
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnTest := pkgRef.Ref("Test")
codeErrorTestEx(t, pkg, `./foo.gop:5:40: too many arguments in call to foo.Test
have (untyped int, untyped int)
want ()`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnTest).Val(1).Val(2).CallWith(2, 0, source("foo.Test(1,2)", 5, 40)).EndStmt().
End()
})
}
func TestTypeParamsErrCallVariadicArguments1(t *testing.T) {
const src = `package foo
func Add[T1 any, T2 ~int](v1 T1, v2 ...T2) (sum T2) {
println(v1)
for _, v := range v2 {
sum += v
}
return sum
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnAdd := pkgRef.Ref("Add")
codeErrorTestEx(t, pkg, `./foo.gop:5:40: not enough arguments in call to foo.Add
have ()
want (T1, ...T2)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnAdd).CallWith(0, 0, source("foo.Add()", 5, 40)).EndStmt().
End()
})
}
func TestTypeParamsErrCallVariadicArguments2(t *testing.T) {
const src = `package foo
func Add[T1 any, T2 ~int](v1 T1, v2 ...T2) (sum T2) {
println(v1)
for _, v := range v2 {
sum += v
}
return sum
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnAdd := pkgRef.Ref("Add")
// not pass source to foo.Add
codeErrorTestEx(t, pkg, `./foo.gop:5:40: cannot infer T2 (-)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnAdd).Val(1).CallWith(1, 0, source("foo.Add(1)", 5, 40)).EndStmt().
End()
})
}
func TestTypeParamsErrorCall(t *testing.T) {
const src = `package foo
type Number interface {
~int | float64
}
func Sum[T Number](vec []T) T {
var sum T
for _, elt := range vec {
sum = sum + elt
}
return sum
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnSum := pkgRef.Ref("Sum")
tyUint := types.Typ[types.Uint]
tyUintSlice := types.NewSlice(tyUint)
var msg string
switch runtime.Version()[:6] {
case "go1.18":
msg = `./foo.gop:5:40: uint does not implement foo.Number`
case "go1.19":
msg = `./foo.gop:5:40: uint does not implement foo.Number (uint missing in ~int | float64)`
default:
msg = `./foo.gop:5:40: uint does not satisfy foo.Number (uint missing in ~int | float64)`
}
codeErrorTestEx(t, pkg, msg, func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnSum).Val(1).Val(2).Val(3).SliceLit(tyUintSlice, 3).CallWith(1, 0, source(`foo.Sum([]uint{1,2,3})`, 5, 40)).EndInit(1).
End()
})
}
func TestTypeParamsErrorInferCall(t *testing.T) {
const src = `package foo
func Loader[T1 any, T2 any](p1 T1, p2 T2) T1 {
return p1
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnLoader := pkgRef.Ref("Loader")
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: cannot infer T2 (foo.go:3:21)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fnLoader).Typ(tyInt).Index(1, false, source(`foo.Loader[int]`, 5, 40)).Val(10).Val(nil).CallWith(2, 0, source(`foo.Loader[int]`, 5, 40)).EndStmt().
End()
})
}
func TestTypeParamErrGenericType(t *testing.T) {
const src = `package foo
type Data struct {
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
tyData := pkgRef.Ref("Data").Type()
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: foo.Data is not a generic type`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "v1").Typ(tyData).Typ(tyInt).Index(1, false, source(`foo.Data[int]`, 5, 40)).Star().Val(nil).Call(1).EndInit(1).
End()
})
}
func TestTypeParamErrGenericType2(t *testing.T) {
gt := newGoxTest()
pkg := gt.NewPackage("", "main")
tyInt := types.Typ[types.Int]
tyString := types.Typ[types.String]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: string is not a generic type`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "v1").Typ(tyString).Typ(tyInt).Index(1, false, source(`string[int]`, 5, 40)).Star().Val(nil).Call(1).EndInit(1).
End()
})
}
func TestTypeParamErrGenericFunc(t *testing.T) {
const src = `package foo
func Loader(n int) string {
return ""
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
fnLoader := pkgRef.Ref("Loader")
tyInt := types.Typ[types.Int]
codeErrorTestEx(t, pkg, `./foo.gop:5:40: invalid operation: cannot index foo.Loader (value of type func(n int) string)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "v1").Val(fnLoader).Typ(tyInt).Index(1, false, source(`v1 := foo.Loader[int]`, 5, 40)).EndInit(1).
End()
})
}
func TestGenTypeParamsFunc(t *testing.T) {