-
Notifications
You must be signed in to change notification settings - Fork 29
/
error_msg_test.go
1443 lines (1385 loc) · 51.3 KB
/
error_msg_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 2021 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 (
"bytes"
"fmt"
"go/ast"
"go/token"
"go/types"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"github.com/goplus/gogen"
xtoken "github.com/goplus/gogen/token"
)
type txtNode struct {
Msg string
pos token.Pos
}
func (p *txtNode) Pos() token.Pos {
return p.pos
}
func (p *txtNode) End() token.Pos {
return p.pos + token.Pos(len(p.Msg))
}
var (
mutex sync.Mutex
pos2Positions = map[token.Pos]token.Position{}
)
// text, line, column
func source(text string, args ...interface{}) ast.Node {
if len(args) < 2 {
return &txtNode{Msg: text}
}
pos := position(args[0].(int), args[1].(int))
return &txtNode{Msg: text, pos: pos}
}
const (
posBits = 16
posMask = (1 << posBits) - 1
)
func position(line, column int) token.Pos {
mutex.Lock()
defer mutex.Unlock()
pos := token.Pos(len(pos2Positions)+1) << posBits
pos2Positions[pos] = token.Position{Filename: "./foo.gop", Line: line, Column: column}
return pos
}
type nodeInterp struct{}
func (p nodeInterp) Position(pos token.Pos) (ret token.Position) {
mutex.Lock()
defer mutex.Unlock()
ret = pos2Positions[(pos &^ posMask)]
ret.Column += int(pos & posMask)
return
}
func (p nodeInterp) Caller(node ast.Node) string {
t := node.(*txtNode)
idx := strings.Index(t.Msg, "(")
if idx > 0 {
return t.Msg[:idx]
}
return t.Msg
}
func (p nodeInterp) LoadExpr(node ast.Node) string {
t := node.(*txtNode)
return t.Msg
}
func codeErrorTest(t *testing.T, msg string, source func(pkg *gogen.Package), disableRecover ...bool) {
t.Run(msg, func(t *testing.T) {
pkg := newMainPackage()
codeErrorTestDo(t, pkg, msg, source, disableRecover...)
})
}
func codeErrorTestEx(t *testing.T, pkg *gogen.Package, msg string, source func(pkg *gogen.Package), disableRecover ...bool) {
t.Run(msg, func(t *testing.T) {
codeErrorTestDo(t, pkg, msg, source, disableRecover...)
})
}
func codeErrorTestDo(t *testing.T, pkg *gogen.Package, msg string, source func(pkg *gogen.Package), disableRecover ...bool) {
pos2Positions = map[token.Pos]token.Position{}
if !(disableRecover != nil && disableRecover[0]) {
defer func() {
if e := recover(); e != nil {
switch err := e.(type) {
case *gogen.CodeError, *gogen.MatchError:
defer recover()
pkg.CB().ResetStmt()
if ret := err.(error).Error(); ret != msg {
t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
}
case *gogen.ImportError:
if ret := err.Error(); ret != msg {
t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
}
case error:
if ret := err.Error(); ret != msg {
t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
}
default:
t.Fatalf("Unexpected error: %v (%T)\n", e, e)
}
} else {
t.Fatal("no error?")
}
}()
}
source(pkg)
var b bytes.Buffer
gogen.WriteTo(&b, pkg, "")
}
func newFunc(
pkg *gogen.Package, line, column int, rline, rcolumn int,
recv *gogen.Param, name string, params, results *types.Tuple, variadic bool) *gogen.Func {
pos := position(line, column)
fn, err := pkg.NewFuncWith(
pos, name, types.NewSignatureType(recv, nil, nil, params, results, variadic), func() token.Pos {
return position(rline, rcolumn)
})
if err != nil {
panic(err)
}
return fn
}
func TestErrIf(t *testing.T) {
codeErrorTest(t, "./foo.gop:1:3: non-boolean condition in if statement",
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
If().Val(1, source("1", 1, 3)).Then(source("1", 1, 3)). // if 1
End()
})
}
func TestErrSwitch(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:5: cannot use 1 (type untyped int) as type string",
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Switch().Val(`"x"`, source("x", 1, 3)).Then(). // switch "x"
Case().Val(1, source("1", 2, 5)).Val(2).Then(). // case 1, 2:
/**/ End().
End()
})
codeErrorTest(t, "./foo.gop:2:5: cannot use 1 (type untyped int) as type bool",
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Switch().None().Then(). // switch
Case().Val(1, source("1", 2, 5)).Val(2).Then(). // case 1, 2:
/**/ End().
End()
})
}
func TestErrTypeRedefined(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:5: foo redeclared in this block\n\tprevious declaration at ./foo.gop:1:5", func(pkg *gogen.Package) {
typ := pkg.NewType("foo", source("foo", 1, 5))
if typ.Inited() {
t.Fatal("NewType failed: inited?")
}
pkg.NewType("foo", source("foo", 2, 5))
})
}
func TestErrTypeSwitch(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: impossible type switch case: v (type interface{Bar()}) cannot have dynamic type int (missing Bar method)",
func(pkg *gogen.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
v := pkg.NewParam(token.NoPos, "v", tyInterf)
pkg.NewFunc(nil, "foo", types.NewTuple(v), nil, false).BodyStart(pkg).
/**/ TypeSwitch("t").Val(v, source("v", 1, 5)).TypeAssertThen().
/**/ TypeCase().Typ(types.Typ[types.Int], source("int", 2, 9)).Then().
/**/ End().
End()
})
codeErrorTest(t, "./foo.gop:2:9: 1 (type untyped int) is not a type",
func(pkg *gogen.Package) {
v := pkg.NewParam(token.NoPos, "v", gogen.TyEmptyInterface)
pkg.NewFunc(nil, "foo", types.NewTuple(v), nil, false).BodyStart(pkg).
/**/ TypeSwitch("t").Val(v).TypeAssertThen().
/**/ TypeCase().Val(1, source("1", 2, 9)).Then().
/**/ End().
End()
})
}
func TestErrAssignOp(t *testing.T) {
codeErrorTest(t, `boundType untyped int => string failed`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.String], "a").
VarRef(ctxRef(pkg, "a"), source("a")).Val(10).AssignOp(token.ADD_ASSIGN).
End()
})
}
func TestErrBinaryOp(t *testing.T) {
codeErrorTest(t, `-: invalid operation: * (mismatched types int and float64)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
NewVar(types.Typ[types.Float64], "b").
VarVal("a").VarVal("b").BinaryOp(token.MUL).EndStmt().
End()
})
codeErrorTest(t, `./foo.gop:2:9: invalid operation: a * b (mismatched types int and float64)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
NewVar(types.Typ[types.Float64], "b").
VarVal("a").VarVal("b").BinaryOp(token.MUL, source(`a * b`, 2, 9)).EndStmt().
End()
})
codeErrorTest(t, `./foo.gop:2:9: invalid operation: v != 3 (mismatched types interface{Bar()} and untyped int)`,
func(pkg *gogen.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
params := types.NewTuple(pkg.NewParam(token.NoPos, "v", tyInterf))
pkg.NewFunc(nil, "foo", params, nil, false).BodyStart(pkg).
/**/ If().VarVal("v").Val(3).BinaryOp(token.NEQ, source(`v != 3`, 2, 9)).Then().
/**/ End().
End()
})
codeErrorTest(t, `./foo.gop:2:9: invalid operation: sl == v (mismatched types []int and interface{Bar()})`,
func(pkg *gogen.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
params := types.NewTuple(pkg.NewParam(token.NoPos, "v", tyInterf))
pkg.NewFunc(nil, "foo", params, nil, false).BodyStart(pkg).
NewVar(types.NewSlice(types.Typ[types.Int]), "sl").
/**/ If().Val(ctxRef(pkg, "sl")).VarVal("v").BinaryOp(token.EQL, source(`sl == v`, 2, 9)).Then().
/**/ End().
End()
})
codeErrorTest(t, `./foo.gop:2:9: invalid operation: 3 == "Hi" (mismatched types untyped int and untyped string)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
/**/ If().Val(3).Val("Hi").BinaryOp(token.EQL, source(`3 == "Hi"`, 2, 9)).Then().
/**/ End().
End()
})
}
func TestErrBinaryOp2(t *testing.T) {
codeErrorTest(t, `-: invalid operation: operator <> not defined on a (int)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
NewVar(types.Typ[types.Int], "b").
VarVal("a", source("a")).VarVal("b").BinaryOp(xtoken.BIDIARROW).EndStmt().
End()
})
}
func TestErrTypeAssert(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: impossible type assertion:\n\tstring does not implement bar (missing Bar method)",
func(pkg *gogen.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
bar := pkg.NewType("bar").InitType(pkg, tyInterf)
params := types.NewTuple(pkg.NewParam(token.NoPos, "v", bar))
pkg.NewFunc(nil, "foo", params, nil, false).BodyStart(pkg).
DefineVarStart(0, "x").VarVal("v").
TypeAssert(types.Typ[types.String], false, source("v.(string)", 2, 9)).EndInit(1).
End()
})
codeErrorTest(t, "./foo.gop:2:9: invalid type assertion: v.(string) (non-interface type int on left)",
func(pkg *gogen.Package) {
params := types.NewTuple(pkg.NewParam(token.NoPos, "v", types.Typ[types.Int]))
pkg.NewFunc(nil, "foo", params, nil, false).BodyStart(pkg).
DefineVarStart(0, "x").VarVal("v").
TypeAssert(types.Typ[types.String], false, source("v.(string)", 2, 9)).EndInit(1).
End()
})
}
func TestErrConst(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: cannot use 1 (type untyped int) as type string in assignment",
func(pkg *gogen.Package) {
pkg.NewConstStart(pkg.Types.Scope(), position(2, 7), types.Typ[types.String], "a").Val(1, source("1", 2, 9)).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: missing value in const declaration",
func(pkg *gogen.Package) {
pkg.NewConstStart(pkg.Types.Scope(), position(2, 7), nil, "a", "b").Val(1).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: extra expression in const declaration",
func(pkg *gogen.Package) {
pkg.NewConstStart(pkg.Types.Scope(), position(2, 7), nil, "a").Val(1).Val(2).EndInit(2)
})
codeErrorTest(t, "./foo.gop:2:7: a redeclared in this block\n\tprevious declaration at ./foo.gop:1:5",
func(pkg *gogen.Package) {
pkg.NewVarStart(position(1, 5), nil, "a").Val(1).EndInit(1)
pkg.NewConstStart(pkg.Types.Scope(), position(2, 7), nil, "a").Val(2).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: a redeclared in this block\n\tprevious declaration at ./foo.gop:1:5",
func(pkg *gogen.Package) {
scope := pkg.Types.Scope()
pkg.NewConstStart(scope, position(1, 5), nil, "a").Val(2).EndInit(1)
pkg.NewVarDefs(scope).New(position(2, 7), types.Typ[types.Int], "a").InitStart(pkg).Val(1).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: const initializer len(a) is not a constant",
func(pkg *gogen.Package) {
pkg.NewVar(position(1, 5), types.NewSlice(types.Typ[types.Int]), "a")
pkg.NewConstStart(pkg.Types.Scope(), position(2, 7), nil, "b").
Val(ctxRef(pkg, "len")).VarVal("a").CallWith(1, 0, source("len(a)", 2, 10)).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:9: a redeclared in this block\n\tprevious declaration at ./foo.gop:1:5",
func(pkg *gogen.Package) {
pkg.NewVarStart(position(1, 5), nil, "a").Val(1).EndInit(1)
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gogen.CodeBuilder) int {
cb.Val(2)
return 1
}, 0, position(2, 7), nil, "_").
Next(1, position(2, 9), "a")
})
codeErrorTest(t, "./foo.gop:2:9: extra expression in const declaration",
func(pkg *gogen.Package) {
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gogen.CodeBuilder) int {
cb.Val(2)
cb.Val(ctxRef(pkg, "iota"))
return 2
}, 0, position(2, 7), nil, "a", "b").
Next(1, position(2, 9), "c")
})
codeErrorTest(t, "./foo.gop:2:9: missing value in const declaration",
func(pkg *gogen.Package) {
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gogen.CodeBuilder) int {
cb.Val(2)
cb.Val(ctxRef(pkg, "iota"))
return 2
}, 0, position(2, 7), nil, "a", "b").
Next(1, position(2, 9), "c", "d", "e")
})
}
func TestErrNewVar(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:6: foo redeclared in this block\n\tprevious declaration at ./foo.gop:1:5",
func(pkg *gogen.Package) {
var x *types.Var
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewAutoVar(position(1, 5), "foo", &x).
NewAutoVar(position(2, 6), "foo", &x).
End()
})
codeErrorTest(t, "./foo.gop:2:9: cannot use 1 (type untyped int) as type string in assignment",
func(pkg *gogen.Package) {
pkg.NewVarStart(position(2, 7), types.Typ[types.String], "a").Val(1, source("1", 2, 9)).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: assignment mismatch: 1 variables but fmt.Println returns 2 values",
func(pkg *gogen.Package) {
fmt := pkg.Import("fmt")
pkg.NewVarStart(position(2, 7), nil, "a").
Val(fmt.Ref("Println")).Val(2).CallWith(1, 0, source("fmt.Println(2)", 2, 11)).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: assignment mismatch: 1 variables but 2 values",
func(pkg *gogen.Package) {
pkg.NewVarStart(position(2, 7), nil, "a").Val(1).Val(2).EndInit(2)
})
codeErrorTest(t, "./foo.gop:2:7: assignment mismatch: 2 variables but 1 values",
func(pkg *gogen.Package) {
pkg.NewVarStart(position(2, 7), nil, "a", "b").Val(2).EndInit(1)
})
codeErrorTest(t, "./foo.gop:2:7: a redeclared in this block\n\tprevious declaration at ./foo.gop:1:5",
func(pkg *gogen.Package) {
pkg.NewVarStart(position(1, 5), nil, "a").Val(1).EndInit(1)
pkg.NewVarStart(position(2, 7), nil, "a").Val(2).EndInit(1)
})
}
func TestErrDefineVar(t *testing.T) {
handleErr = func(err error) {
if err.Error() != "./foo.gop:2:1: no new variables on left side of :=" {
t.Fatal("TestErrDefineVar:", err)
}
}
codeErrorTest(t, `./foo.gop:2:6: cannot use "Hi" (type untyped string) as type int in assignment`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "foo").Val(1).EndInit(1).
DefineVarStart(position(2, 1), "foo").Val("Hi", source(`"Hi"`, 2, 6)).EndInit(1).
End()
})
}
func TestErrForRange(t *testing.T) {
codeErrorTest(t, `./foo.gop:1:17: can't use return/continue/break/goto in for range of udt.Gop_Enum(callback)`,
func(pkg *gogen.Package) {
foo := pkg.Import("github.com/goplus/gogen/internal/foo")
bar := foo.Ref("Foo2").Type()
v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
pkg.NewFunc(nil, "foo", types.NewTuple(v), nil, false).BodyStart(pkg).
ForRange("a", "b").
Val(v, source("v", 1, 9)).
RangeAssignThen(position(1, 17)).
Return(0).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:17: cannot range over v (type *github.com/goplus/gogen/internal/foo.Foo4)`,
func(pkg *gogen.Package) {
foo := pkg.Import("github.com/goplus/gogen/internal/foo")
bar := foo.Ref("Foo4").Type()
v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
pkg.NewFunc(nil, "foo", types.NewTuple(v), nil, false).BodyStart(pkg).
ForRange().
Val(v, source("v", 1, 9)).
RangeAssignThen(position(1, 17)).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:17: cannot range over v (type *github.com/goplus/gogen/internal/foo.Foo3)`,
func(pkg *gogen.Package) {
foo := pkg.Import("github.com/goplus/gogen/internal/foo")
bar := foo.Ref("Foo3").Type()
v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar))
pkg.NewFunc(nil, "foo", types.NewTuple(v), nil, false).BodyStart(pkg).
ForRange("a", "b").
Val(v, source("v", 1, 9)).
RangeAssignThen(position(1, 17)).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:17: cannot range over 13 (type untyped int)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
ForRange("a", "b").
Val(13, source("13", 1, 9)).
RangeAssignThen(position(1, 17)).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:17: cannot range over 13 (type untyped int)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
ForRange().
VarRef(ctxRef(pkg, "a")).
Val(13, source("13", 1, 9)).
RangeAssignThen(position(1, 17)).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:17: too many variables in range`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a", "b", "c").
ForRange().
VarRef(ctxRef(pkg, "a")).
VarRef(ctxRef(pkg, "b")).
VarRef(ctxRef(pkg, "c")).
Val("Hello").
RangeAssignThen(position(1, 17)).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:17: too many variables in range`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
ForRange("a", "b", "c").
Val("Hello").
RangeAssignThen(position(1, 17)).
End().
End()
})
codeErrorTest(t, `./foo.gop:1:5: cannot assign type string to a (type int) in range`,
func(pkg *gogen.Package) {
tySlice := types.NewSlice(types.Typ[types.String])
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "a").Val(1).EndInit(1).
NewVar(tySlice, "b").
ForRange().
VarRef(nil).
VarRef(ctxRef(pkg, "a"), source("a", 1, 5)).
Val(ctxRef(pkg, "b"), source("b", 1, 9)).
RangeAssignThen(token.NoPos).
End().
End()
})
}
func TestErrAssign(t *testing.T) {
codeErrorTest(t, "./foo.gop:1:3: assignment mismatch: 1 variables but bar returns 2 values",
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(1, 15), "", gogen.TyError)
newFunc(pkg, 3, 5, 3, 7, nil, "bar", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).End()
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "x").
VarRef(ctxRef(pkg, "x")).
Val(ctxRef(pkg, "bar")).
CallWith(0, 0, source("bar()", 1, 5)).
AssignWith(1, 1, source("x = bar()", 1, 3)).
End()
})
codeErrorTest(t, "./foo.gop:1:3: assignment mismatch: 1 variables but 2 values",
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "x").
VarRef(ctxRef(pkg, "x")).
Val(1).Val(2).
AssignWith(1, 2, source("x = 1, 2", 1, 3)).
End()
})
}
func TestErrFunc(t *testing.T) {
codeErrorTest(t, `./foo.gop:5:1: main redeclared in this block
./foo.gop:1:10: other declaration of main`,
func(pkg *gogen.Package) {
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
pkg.NewFuncDecl(position(1, 10), "main", sig).BodyStart(pkg).End()
pkg.NewFuncDecl(position(5, 1), "main", sig).BodyStart(pkg).End()
})
}
func TestErrFuncCall(t *testing.T) {
codeErrorTest(t, `./foo.gop:2:10: cannot call non-function a() (type int)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
VarVal("a").CallWith(0, 0, source("a()", 2, 10)).
End()
})
codeErrorTest(t, `./foo.gop:2:10: cannot use ... in call to non-variadic foo`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "foo", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
Val(ctxRef(pkg, "foo"), source("foo", 2, 2)).VarVal("a").CallWith(1, 1, source("foo(a...)", 2, 10)).
End()
})
codeErrorTest(t, `./foo.gop:3:5: cannot use a (type bool) as type int in argument to foo(a)`,
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
newFunc(pkg, 1, 5, 1, 7, nil, "foo", types.NewTuple(retInt), nil, false).BodyStart(pkg).
End()
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Debug(func(cb *gogen.CodeBuilder) {
pkg.NewVar(position(2, 9), types.Typ[types.Bool], "a")
}).
Val(ctxRef(pkg, "foo")).Val(ctxRef(pkg, "a"), source("a", 3, 5)).
CallWith(1, 0, source("foo(a)", 3, 10)).
End()
})
codeErrorTest(t, `./foo.gop:2:10: not enough arguments in call to foo
have (int)
want (int, int)`,
func(pkg *gogen.Package) {
argInt1 := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
argInt2 := pkg.NewParam(position(1, 15), "", types.Typ[types.Int])
pkg.NewFunc(nil, "foo", types.NewTuple(argInt1, argInt2), nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
Val(ctxRef(pkg, "foo"), source("foo", 2, 2)).VarVal("a").CallWith(1, 0, source("foo(a)", 2, 10)).
End()
})
codeErrorTest(t, `./foo.gop:2:10: too many arguments in call to foo
have (int, untyped int, untyped int)
want (int, int)`,
func(pkg *gogen.Package) {
argInt1 := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
argInt2 := pkg.NewParam(position(1, 15), "", types.Typ[types.Int])
pkg.NewFunc(nil, "foo", types.NewTuple(argInt1, argInt2), nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
Val(ctxRef(pkg, "foo"), source("foo", 2, 2)).VarVal("a").Val(1).Val(2).CallWith(3, 0, source("foo(a, 1, 2)", 2, 10)).
End()
})
codeErrorTest(t, `./foo.gop:2:10: not enough arguments in call to foo
have (int)
want (int, int, []int)`,
func(pkg *gogen.Package) {
argInt1 := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
argInt2 := pkg.NewParam(position(1, 15), "", types.Typ[types.Int])
argIntSlice3 := pkg.NewParam(position(1, 20), "", types.NewSlice(types.Typ[types.Int]))
pkg.NewFunc(nil, "foo", types.NewTuple(argInt1, argInt2, argIntSlice3), nil, true).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
Val(ctxRef(pkg, "foo"), source("foo", 2, 2)).VarVal("a").CallWith(1, 0, source("foo(a)", 2, 10)).
End()
})
}
func TestErrReturn(t *testing.T) {
codeErrorTest(t, `./foo.gop:2:9: cannot use "Hi" (type untyped string) as type error in return argument`,
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(1, 15), "", gogen.TyError)
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).
Val(1, source("1", 2, 7)).
Val("Hi", source(`"Hi"`, 2, 9)).
Return(2, source(`return 1, "Hi"`, 2, 5)).
End()
})
codeErrorTest(t, "./foo.gop:2:5: cannot use byte value as type error in return argument",
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(1, 15), "", gogen.TyError)
retInt2 := pkg.NewParam(position(3, 10), "", types.Typ[types.Int])
retByte := pkg.NewParam(position(3, 15), "", gogen.TyByte)
newFunc(pkg, 3, 5, 3, 7, nil, "bar", nil, types.NewTuple(retInt2, retByte), false).BodyStart(pkg).End()
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).
Val(ctxRef(pkg, "bar")).
CallWith(0, 0, source("bar()", 2, 9)).
Return(1, source("return bar()", 2, 5)).
End()
})
codeErrorTest(t, "./foo.gop:2:5: too many arguments to return\n\thave (untyped int, untyped int, untyped int)\n\twant (int, error)",
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(1, 15), "", gogen.TyError)
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).
Val(1, source("1", 2, 7)).
Val(2, source("2", 2, 9)).
Val(3, source("3", 2, 11)).
Return(3, source("return 1, 2, 3", 2, 5)).
End()
})
codeErrorTest(t, "./foo.gop:2:5: too few arguments to return\n\thave (untyped int)\n\twant (int, error)",
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(1, 15), "", gogen.TyError)
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).
Val(1, source("1", 2, 7)).
Return(1, source("return 1", 2, 5)).
End()
})
codeErrorTest(t, "./foo.gop:2:5: too few arguments to return\n\thave (byte)\n\twant (int, error)",
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(1, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(1, 15), "", gogen.TyError)
ret := pkg.NewParam(position(3, 10), "", gogen.TyByte)
newFunc(pkg, 3, 5, 3, 7, nil, "bar", nil, types.NewTuple(ret), false).BodyStart(pkg).End()
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).
Val(ctxRef(pkg, "bar")).
CallWith(0, 0, source("bar()", 2, 9)).
Return(1, source("return bar()", 2, 5)).
End()
})
codeErrorTest(t, "./foo.gop:2:5: too many arguments to return\n\thave (int, error)\n\twant (byte)",
func(pkg *gogen.Package) {
retInt := pkg.NewParam(position(3, 10), "", types.Typ[types.Int])
retErr := pkg.NewParam(position(3, 15), "", gogen.TyError)
newFunc(pkg, 3, 5, 3, 7, nil, "bar", nil, types.NewTuple(retInt, retErr), false).BodyStart(pkg).End()
ret := pkg.NewParam(position(1, 10), "", gogen.TyByte)
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(ret), false).BodyStart(pkg).
Val(ctxRef(pkg, "bar")).
CallWith(0, 0, source("bar()", 2, 9)).
Return(1, source("return bar()", 2, 5)).
End()
})
codeErrorTest(t, "./foo.gop:2:5: not enough arguments to return\n\thave ()\n\twant (byte)",
func(pkg *gogen.Package) {
ret := pkg.NewParam(position(1, 10), "", gogen.TyByte)
newFunc(pkg, 1, 5, 1, 7, nil, "foo", nil, types.NewTuple(ret), false).BodyStart(pkg).
Return(0, source("return", 2, 5)).
End()
})
}
func TestErrInitFunc(t *testing.T) {
codeErrorTest(t, "./foo.gop:1:5: func init must have no arguments and no return values", func(pkg *gogen.Package) {
v := pkg.NewParam(token.NoPos, "v", gogen.TyByte)
newFunc(pkg, 1, 5, 1, 7, nil, "init", types.NewTuple(v), nil, false).BodyStart(pkg).End()
})
}
func TestErrRecv(t *testing.T) {
tySlice := types.NewSlice(gogen.TyByte)
codeErrorTest(t, "./foo.gop:1:9: invalid receiver type []byte ([]byte is not a defined type)", func(pkg *gogen.Package) {
recv := pkg.NewParam(position(1, 7), "p", tySlice)
newFunc(pkg, 1, 5, 1, 9, recv, "foo", nil, nil, false).BodyStart(pkg).End()
})
codeErrorTest(t, "./foo.gop:2:9: invalid receiver type []byte ([]byte is not a defined type)", func(pkg *gogen.Package) {
recv := pkg.NewParam(position(2, 7), "p", types.NewPointer(tySlice))
newFunc(pkg, 2, 6, 2, 9, recv, "foo", nil, nil, false).BodyStart(pkg).End()
})
codeErrorTest(t, "./foo.gop:3:10: invalid receiver type error (error is an interface type)", func(pkg *gogen.Package) {
recv := pkg.NewParam(position(3, 9), "p", gogen.TyError)
newFunc(pkg, 3, 7, 3, 10, recv, "foo", nil, nil, false).BodyStart(pkg).End()
})
codeErrorTest(t, "./foo.gop:3:10: invalid receiver type recv (recv is a pointer type)", func(pkg *gogen.Package) {
t := pkg.NewType("recv").InitType(pkg, types.NewPointer(gogen.TyByte))
recv := pkg.NewParam(position(3, 9), "p", t)
newFunc(pkg, 3, 7, 3, 10, recv, "foo", nil, nil, false).BodyStart(pkg).End()
})
}
func TestErrLabel(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:1: label foo already defined at ./foo.gop:1:1", func(pkg *gogen.Package) {
cb := pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg)
l := cb.NewLabel(position(1, 1), "foo")
cb.NewLabel(position(2, 1), "foo")
cb.Goto(l)
cb.End()
})
codeErrorTest(t, "./foo.gop:1:1: label foo defined and not used", func(pkg *gogen.Package) {
cb := pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg)
cb.NewLabel(position(1, 1), "foo")
cb.End()
})
codeErrorTest(t, "./foo.gop:1:1: syntax error: non-declaration statement outside function body", func(pkg *gogen.Package) {
pkg.CB().NewLabel(position(1, 1), "foo")
})
/* codeErrorTest(t, "./foo.gop:1:1: label foo is not defined", func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Goto("foo", source("goto foo", 1, 1)).
End()
})
*/
}
func TestErrStructLit(t *testing.T) {
codeErrorTest(t, `./foo.gop:1:7: too many values in struct{x int; y string}{...}`,
func(pkg *gogen.Package) {
fields := []*types.Var{
types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false),
types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.String], false),
}
tyStruc := types.NewStruct(fields, nil)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(1, source(`1`, 1, 1)).
Val("1", source(`"1"`, 1, 5)).
Val(1, source(`1`, 1, 7)).
StructLit(tyStruc, 3, false).
EndStmt().
End()
})
codeErrorTest(t, `./foo.gop:1:1: too few values in struct{x int; y string}{...}`,
func(pkg *gogen.Package) {
fields := []*types.Var{
types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false),
types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.String], false),
}
tyStruc := types.NewStruct(fields, nil)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(1, source(`1`, 1, 1)).
StructLit(tyStruc, 1, false).
EndStmt().
End()
})
codeErrorTest(t, `./foo.gop:1:5: cannot use 1 (type untyped int) as type string in value of field y`,
func(pkg *gogen.Package) {
fields := []*types.Var{
types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false),
types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.String], false),
}
tyStruc := types.NewStruct(fields, nil)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(1).
Val(1, source(`1`, 1, 5)).
StructLit(tyStruc, 2, true).
EndStmt().
End()
})
codeErrorTest(t, `./foo.gop:1:1: cannot use "1" (type untyped string) as type int in value of field x`,
func(pkg *gogen.Package) {
fields := []*types.Var{
types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false),
types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.String], false),
}
tyStruc := types.NewStruct(fields, nil)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("1", source(`"1"`, 1, 1)).
Val(1, source(`1`, 1, 5)).
StructLit(tyStruc, 2, false).
EndStmt().
End()
})
}
func TestErrMapLit(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:6: cannot use 1+2 (type untyped int) as type string in map key",
func(pkg *gogen.Package) {
tyMap := types.NewMap(types.Typ[types.String], types.Typ[types.Int])
cb := pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
DefineVarStart(0, "x")
cb.ResetInit()
cb.Val(1, source("1")).
Val(2, source("2")).
BinaryOp(token.ADD, source("1+2", 2, 6)).
Val(3).
MapLit(tyMap, 2).
End()
})
codeErrorTest(t, `./foo.gop:1:5: cannot use "Hi" + "!" (type untyped string) as type int in map value`,
func(pkg *gogen.Package) {
tyMap := types.NewMap(types.Typ[types.String], types.Typ[types.Int])
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("1").
Val("Hi", source(`"Hi"`)).
Val("!", source(`"!"`)).
BinaryOp(token.ADD, source(`"Hi" + "!"`, 1, 5)).
MapLit(tyMap, 2).
EndStmt().
End()
})
}
func TestErrMapLit2(t *testing.T) {
codeErrorTest(t, "-: MapLit: invalid arity, can't be odd - 1",
func(pkg *gogen.Package) {
tyMap := types.NewMap(types.Typ[types.String], types.Typ[types.Int])
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("1").
MapLit(tyMap, 1).
EndStmt().
End()
})
codeErrorTest(t, "-: type string isn't a map",
func(pkg *gogen.Package) {
tyMap := types.Typ[types.String]
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("1").
Val("Hi", source(`"Hi"`)).
Val("!", source(`"!"`)).
BinaryOp(token.ADD, source(`"Hi" + "!"`, 1, 5)).
MapLit(tyMap, 2).
EndStmt().
End()
})
}
func TestErrArrayLit(t *testing.T) {
codeErrorTest(t, "./foo.gop:1:5: cannot use 32 (type untyped int) as type string in array literal",
func(pkg *gogen.Package) {
tyArray := types.NewArray(types.Typ[types.String], 10)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(1, source("1")).
Val(32, source("32", 1, 5)).
ArrayLit(tyArray, 2, true).
EndStmt().
End()
})
codeErrorTest(t, "./foo.gop:1:5: cannot use 1+2 (type untyped int) as type string in array literal",
func(pkg *gogen.Package) {
tyArray := types.NewArray(types.Typ[types.String], 10)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(1, source("1")).
Val(2, source("2")).
BinaryOp(token.ADD, source("1+2", 1, 5)).
ArrayLit(tyArray, 1).
EndStmt().
End()
})
codeErrorTest(t,
`./foo.gop:2:10: array index 1 out of bounds [0:1]`,
func(pkg *gogen.Package) {
tyArray := types.NewArray(types.Typ[types.String], 1)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("Hi", source(`"Hi"`, 1, 5)).
Val("!", source(`"!"`, 2, 10)).
ArrayLit(tyArray, 2, false).
EndStmt().
End()
})
codeErrorTest(t,
`./foo.gop:1:5: array index 12 (value 12) out of bounds [0:10]`,
func(pkg *gogen.Package) {
tyArray := types.NewArray(types.Typ[types.String], 10)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(12, source(`12`, 1, 5)).
Val("!", source(`"!"`)).
ArrayLit(tyArray, 2, true).
EndStmt().
End()
})
codeErrorTest(t,
`./foo.gop:2:10: array index 10 out of bounds [0:10]`,
func(pkg *gogen.Package) {
tyArray := types.NewArray(types.Typ[types.String], 10)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(9, source(`9`, 1, 5)).
Val("!", source(`"!"`)).
None().
Val("!!", source(`"!!"`, 2, 10)).
ArrayLit(tyArray, 4, true).
EndStmt().
End()
})
codeErrorTest(t,
`./foo.gop:1:5: cannot use "Hi" + "!" as index which must be non-negative integer constant`,
func(pkg *gogen.Package) {
tyArray := types.NewArray(types.Typ[types.String], 100)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("Hi", source(`"Hi"`)).
Val("!", source(`"!"`)).
BinaryOp(token.ADD, source(`"Hi" + "!"`, 1, 5)).
Val("Hi", source(`"Hi"`)).
ArrayLit(tyArray, 2, true).
EndStmt().
End()
})
}
func TestErrSliceLit(t *testing.T) {
codeErrorTest(t,
`./foo.gop:1:5: cannot use "10" as index which must be non-negative integer constant`,
func(pkg *gogen.Package) {
tySlice := types.NewSlice(types.Typ[types.String])
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val("10", source(`"10"`, 1, 5)).
Val("Hi", source(`"Hi"`)).
SliceLit(tySlice, 2, true).
EndStmt().
End()
})
codeErrorTest(t, "./foo.gop:1:5: cannot use 32 (type untyped int) as type string in slice literal",
func(pkg *gogen.Package) {
tySlice := types.NewSlice(types.Typ[types.String])
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(10, source("10")).
Val(32, source("32", 1, 5)).
SliceLit(tySlice, 2, true).
EndStmt().
End()
})
codeErrorTest(t, "./foo.gop:1:5: cannot use 1+2 (type untyped int) as type string in slice literal",
func(pkg *gogen.Package) {
tySlice := types.NewSlice(types.Typ[types.String])
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(1, source("1")).
Val(2, source("2")).
BinaryOp(token.ADD, source("1+2", 1, 5)).
SliceLit(tySlice, 1).
EndStmt().
End()
})
}
func TestErrSlice(t *testing.T) {
codeErrorTest(t,
`./foo.gop:1:5: cannot slice true (type untyped bool)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(types.Universe.Lookup("true"), source("true", 1, 5)).
Val(1).
Val(3).
Val(5).
Slice(true, source("true[1:3:5]", 1, 5)).
EndStmt().
End()
})
codeErrorTest(t,
`./foo.gop:1:1: cannot slice x (type *byte)`,
func(pkg *gogen.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.NewPointer(gogen.TyByte), "x").
Val(ctxRef(pkg, "x"), source("x", 1, 1)).
Val(1).
Val(3).
Val(5).
Slice(true, source("x[1:3:5]", 1, 5)).
EndStmt().
End()
})
codeErrorTest(t,
`./foo.gop:1:5: invalid operation x[1:3:5] (3-index slice of string)`,
func(pkg *gogen.Package) {