-
Notifications
You must be signed in to change notification settings - Fork 546
/
compile_test.go
4023 lines (3348 loc) · 54.7 KB
/
compile_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 (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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 cl_test
import (
"bytes"
"os"
"strings"
"sync"
"testing"
"github.com/goplus/gop"
"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/scanner"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
"github.com/goplus/mod/env"
)
const (
gopRootDir = ".."
)
var (
gblFset *token.FileSet
gblConf *cl.Config
)
func init() {
gox.SetDebug(gox.DbgFlagAll)
cl.SetDebug(cl.DbgFlagAll)
gblFset = token.NewFileSet()
imp := gop.NewImporter(nil, &env.Gop{Root: gopRootDir, Version: "1.0"}, gblFset)
gblConf = &cl.Config{
Fset: gblFset,
Importer: imp,
LookupClass: lookupClass,
LookupPub: lookupPub,
C2goBase: "github.com/goplus/gop/cl/internal",
NoFileLine: true,
NoAutoGenMain: true,
}
}
func gopClNamedTest(t *testing.T, name string, gopcode, expected string) {
t.Run(name, func(t *testing.T) {
gopClTest(t, gopcode, expected)
})
}
func gopClTest(t *testing.T, gopcode, expected string) {
gopClTestEx(t, gblConf, "main", gopcode, expected)
}
func gopClTestEx(t *testing.T, conf *cl.Config, pkgname, gopcode, expected string) {
fs := parsertest.NewSingleFileFS("/foo", "bar.gop", gopcode)
gopClTestFS(t, conf, fs, pkgname, expected)
}
func gopMixedClTest(t *testing.T, pkgname, gocode, gopcode, expected string) {
fs := parsertest.NewTwoFilesFS("/foo", "a.go", gocode, "b.gop", gopcode)
gopClTestFS(t, gblConf, fs, pkgname, expected)
}
func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, expected string) {
cl.SetDisableRecover(true)
defer cl.SetDisableRecover(false)
pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", parser.Config{Mode: parser.ParseComments})
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatal("ParseFSDir:", err)
}
bar := pkgs[pkgname]
pkg, err := cl.NewPackage("github.com/goplus/gop/cl", bar, conf)
if err != nil {
t.Fatal("NewPackage:", err)
}
var b bytes.Buffer
err = pkg.WriteTo(&b)
if err != nil {
t.Fatal("gox.WriteTo failed:", err)
}
result := b.String()
if result != expected {
t.Fatalf("\nResult:\n%s\nExpected:\n%s\n", result, expected)
}
}
func TestMixedGo(t *testing.T) {
gopMixedClTest(t, "main", `package main
import "strconv"
const n = 10
func f(v int) string {
return strconv.Itoa(v)
}
type foo struct {
v int
}
func (a foo) _() {
}
func (a foo) Str() string {
return f(a.v)
}
func (a *foo) Bar() int {
return 0
}
type foo2 = foo
type foo3 foo2
`, `
var a [n]int
var b string = f(n)
var c foo2
var d int = c.v
var e = foo3{}
var x string = c.str
`, `package main
var a [10]int
var b string = f(n)
var c foo
var d int = c.v
var e = foo3{}
var x string = c.Str()
`)
}
func Test_RangeExpressionIf_Issue1243(t *testing.T) {
gopClTest(t, `
for i <- :10, i%3 == 0 {
println i
}`, `package main
import fmt "fmt"
func main() {
for i := 0; i < 10; i += 1 {
if i%3 == 0 {
fmt.Println(i)
}
}
}
`)
}
func Test_CastSlice_Issue1240(t *testing.T) {
gopClTest(t, `
type fvec []float64
type foo float64
a := []float64([1, 2])
b := fvec([1, 2])
c := foo([1, 2])
println a, b, c
`, `package main
import fmt "fmt"
type fvec []float64
type foo float64
func main() {
a := []float64{1, 2}
b := fvec{1, 2}
c := foo([]int{1, 2})
fmt.Println(a, b, c)
}
`)
}
func TestUnderscoreRedeclared_Issue1197(t *testing.T) {
gopClTest(t, `
func() (_ [2]int) { type _ int; return }()
`, `package main
func main() {
func() (_ [2]int) {
return
}()
}
`)
}
func TestInterfaceBugNilUnderlying_Issue1198(t *testing.T) {
gopClTest(t, `
import "runtime"
type Outer interface{ Inner }
type impl struct{}
func New() Outer { return &impl{} }
type Inner interface {
DoStuff() error
}
func (a *impl) DoStuff() error {
return nil
}
func main() {
var outer Outer = New()
}
`, `package main
type Inner interface {
DoStuff() error
}
type Outer interface {
Inner
}
type impl struct {
}
func (a *impl) DoStuff() error {
return nil
}
func New() Outer {
return &impl{}
}
func main() {
var outer Outer = New()
}
`)
}
func TestInterfaceBugNilUnderlying_Issue1196(t *testing.T) {
gopClTest(t, `
func main() {
i := I(A{})
b := make(chan I, 1)
b <- B{}
var ok bool
i, ok = <-b
}
type I interface{ M() int }
type T int
func (T) M() int { return 0 }
type A struct{ T }
type B struct{ T }
`, `package main
func main() {
i := I(A{})
b := make(chan I, 1)
b <- B{}
var ok bool
i, ok = <-b
}
type T int
func (T) M() int {
return 0
}
type A struct {
T
}
type I interface {
M() int
}
type B struct {
T
}
`)
}
func TestMyIntInc_Issue1195(t *testing.T) {
gopClTest(t, `
type MyInt int
var c MyInt
c++
`, `package main
type MyInt int
var c MyInt
func main() {
c++
}
`)
}
func TestAutoPropMixedName_Issue1194(t *testing.T) {
gopClTest(t, `
type Point struct {
Min, Max int
}
type Obj struct {
bbox Point
}
func (o *Obj) Bbox() Point {
return o.bbox
}
func (o *Obj) Points() [2]int{
return [2]int{o.bbox.Min, o.bbox.Max}
}
`, `package main
type Point struct {
Min int
Max int
}
type Obj struct {
bbox Point
}
func (o *Obj) Bbox() Point {
return o.bbox
}
func (o *Obj) Points() [2]int {
return [2]int{o.bbox.Min, o.bbox.Max}
}
`)
}
func TestShiftUntypedInt_Issue1193(t *testing.T) {
gopClTest(t, `
func GetValue(shift uint) uint {
return 1 << shift
}`, `package main
func GetValue(shift uint) uint {
return 1 << shift
}
`)
}
func TestInitFunc(t *testing.T) {
gopClTest(t, `
func init() {}
func init() {}
`, `package main
func init() {
}
func init() {
}
`)
}
func TestSlogan(t *testing.T) {
gopClTest(t, `
fields := ["engineering", "STEM education", "data science"]
println "The Go+ Language for", fields.join(", ")
`, `package main
import (
fmt "fmt"
strings "strings"
)
func main() {
fields := []string{"engineering", "STEM education", "data science"}
fmt.Println("The Go+ Language for", strings.Join(fields, ", "))
}
`)
}
func TestAssignPrintln(t *testing.T) {
gopClTest(t, `
p := println
p "Hello world"
`, `package main
import fmt "fmt"
func main() {
p := fmt.Println
p("Hello world")
}
`)
}
func TestRedefineBuiltin(t *testing.T) {
gopClTest(t, `
func main() {
const a = append + len
}
const (
append = iota
len
)
`, `package main
const (
append = iota
len
)
func main() {
const a = append + len
}
`)
}
func TestTypeConvIssue804(t *testing.T) {
gopClTest(t, `
c := make(chan int)
d := (chan<- int)(c)
e := (<-chan int)(c)
f := (*int)(nil)
a := c == d
b := c == e
`, `package main
func main() {
c := make(chan int)
d := (chan<- int)(c)
e := (<-chan int)(c)
f := (*int)(nil)
a := c == d
b := c == e
}
`)
}
func TestUntypedFloatIssue798(t *testing.T) {
gopClTest(t, `
func isPow10(x uint64) bool {
switch x {
case 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19:
return true
}
return false
}
`, `package main
func isPow10(x uint64) bool {
switch x {
case 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19:
return true
}
return false
}
`)
}
func TestInterfaceIssue795(t *testing.T) {
gopClTest(t, `
type I interface {
a(s string) I
b(s string) string
}
type T1 int
func (t T1) a(s string) I {
return t
}
func (T1) b(s string) string {
return s
}
`, `package main
type I interface {
a(s string) I
b(s string) string
}
type T1 int
func (t T1) a(s string) I {
return t
}
func (T1) b(s string) string {
return s
}
`)
}
func TestChanRecvIssue789(t *testing.T) {
gopClTest(t, `
func foo(ch chan int) (int, bool) {
x, ok := (<-ch)
return x, ok
}
`, `package main
func foo(ch chan int) (int, bool) {
x, ok := <-ch
return x, ok
}
`)
}
func TestNamedChanCloseIssue790(t *testing.T) {
gopClTest(t, `
type XChan chan int
func foo(ch XChan) {
close(ch)
}
`, `package main
type XChan chan int
func foo(ch XChan) {
close(ch)
}
`)
}
func TestUntypedFloatIssue793(t *testing.T) {
gopClTest(t, `
var a [1e1]int
`, `package main
var a [10]int
`)
}
func TestUntypedFloatIssue788(t *testing.T) {
gopClTest(t, `
func foo(v int) bool {
return v > 1.1e5
}
`, `package main
func foo(v int) bool {
return v > 1.1e5
}
`)
}
func TestSwitchCompositeLitIssue801(t *testing.T) {
gopClTest(t, `
type T struct {
X int
}
switch (T{}) {
case T{1}:
panic("bad")
}
`, `package main
type T struct {
X int
}
func main() {
switch (T{}) {
case T{1}:
panic("bad")
}
}
`)
}
func TestConstIssue800(t *testing.T) {
gopClTest(t, `
const (
h0_0, h0_1 = 1.0 / (iota + 1), 1.0 / (iota + 2)
h1_0, h1_1
)
`, `package main
const (
h0_0, h0_1 = 1.0 / (iota + 1), 1.0 / (iota + 2)
h1_0, h1_1
)
`)
}
func TestConstIssue805(t *testing.T) {
gopClTest(t, `
const (
n1 = +5
d1 = +3
q1 = +1
r1 = +2
)
const (
ret1 = n1/d1 != q1
ret2 = n1%d1 != r1
ret3 = n1/d1 != q1 || n1%d1 != r1
)
`, `package main
const (
n1 = +5
d1 = +3
q1 = +1
r1 = +2
)
const (
ret1 = n1/d1 != q1
ret2 = n1%d1 != r1
ret3 = false
)
`)
}
func TestUntypedNilIssue806(t *testing.T) {
gopClTest(t, `
switch f := func() {}; f {
case nil:
}
`, `package main
func main() {
switch f := func() {
}; f {
case nil:
}
}
`)
}
func TestSwitchIssue807(t *testing.T) {
gopClTest(t, `
switch {
case interface{}(true):
}
`, `package main
func main() {
switch {
case interface {
}(true):
}
}
`)
}
func TestUntypedComplexIssue799(t *testing.T) {
gopClTest(t, `
const ulp1 = imag(1i + 2i / 3 - 5i / 3)
const ulp2 = imag(1i + complex(0, 2) / 3 - 5i / 3)
func main() {
const a = (ulp1 == ulp2)
}
`, `package main
const ulp1 = imag(1i + 2i/3 - 5i/3)
const ulp2 = imag(1i + complex(0, 2)/3 - 5i/3)
func main() {
const a = ulp1 == ulp2
}
`)
}
func TestUnderscoreConstAndVar(t *testing.T) {
gopClTest(t, `
const (
c0 = 1 << iota
_
_
_
c4
)
func i() int {
return 23
}
var (
_ = i()
_ = i()
)
`, `package main
const (
c0 = 1 << iota
_
_
_
c4
)
func i() int {
return 23
}
var _ = i()
var _ = i()
`)
}
func TestUnderscoreFuncAndMethod(t *testing.T) {
gopClTest(t, `
func _() {
}
type T struct {
_, _, _ int
}
func (T) _() {
}
func (T) _() {
}
`, `package main
type T struct {
_ int
_ int
_ int
}
func (T) _() {
}
func (T) _() {
}
func _() {
}
`)
}
func TestErrWrapIssue772(t *testing.T) {
gopClTest(t, `
package main
func t() (int,int,error){
return 0, 0, nil
}
func main() {
a, b := t()!
println(a, b)
}`, `package main
import fmt "fmt"
func t() (int, int, error) {
return 0, 0, nil
}
func main() {
a, b := func() (_gop_ret int, _gop_ret2 int) {
var _gop_err error
_gop_ret, _gop_ret2, _gop_err = t()
if _gop_err != nil {
panic(_gop_err)
}
return
}()
fmt.Println(a, b)
}
`)
}
func TestErrWrapIssue778(t *testing.T) {
gopClTest(t, `
package main
func t() error {
return nil
}
func main() {
t()!
}`, `package main
func t() error {
return nil
}
func main() {
func() {
var _gop_err error
_gop_err = t()
if _gop_err != nil {
panic(_gop_err)
}
return
}()
}
`)
}
func TestIssue774(t *testing.T) {
gopClNamedTest(t, "InterfaceTypeAssert", `
package main
import "fmt"
func main() {
var a AA = &A{str: "hello"}
fmt.Println(a.(*A))
}
type AA interface {
String() string
}
type A struct {
str string
}
func (a *A) String() string {
return a.str
}
`, `package main
import fmt "fmt"
func main() {
var a AA = &A{str: "hello"}
fmt.Println(a.(*A))
}
type A struct {
str string
}
func (a *A) String() string {
return a.str
}
type AA interface {
String() string
}
`)
gopClNamedTest(t, "getInterface", `
package main
import "fmt"
func main() {
a := get()
fmt.Println(a.(*A))
}
func get() AA {
var a AA
return a
}
type AA interface {
String() string
}
type A struct {
str string
}
func (a *A) String() string {
return a.str
}
`, `package main
import fmt "fmt"
func main() {
a := get()
fmt.Println(a.(*A))
}
func get() AA {
var a AA
return a
}
type AA interface {
String() string
}
type A struct {
str string
}
func (a *A) String() string {
return a.str
}
`)
}
func TestBlockStmt(t *testing.T) {
gopClTest(t, `
package main
func main() {
{
type T int
t := T(100)
println(t)
}
{
type T string
t := "hello"
println(t)
}
}
`, `package main
import fmt "fmt"
func main() {
{
type T int
t := T(100)
fmt.Println(t)
}
{
type T string
t := "hello"
fmt.Println(t)
}
}
`)
}
func TestConstTypeConvIssue792(t *testing.T) {
gopClTest(t, `
const dots = ". . . " + ". . . . . "
const n = uint(len(dots))
`, `package main
const dots = ". . . " + ". . . . . "
const n = uint(len(dots))
`)
}
func TestVarInitTwoValueIssue791(t *testing.T) {
gopClTest(t, `
var (
m = map[string]string{"a": "A"}
a, ok = m["a"]
)
`, `package main
var m = map[string]string{"a": "A"}
var a, ok = m["a"]
`)
}
func TestVarAfterMain(t *testing.T) {
gopClTest(t, `
package main
func main() {
println(i)
}
var i int
`, `package main
import fmt "fmt"
func main() {
fmt.Println(i)
}
var i int
`)
gopClTest(t, `
package main
func f(v float64) float64 {
return v
}
func main() {
sink = f(100)
}
var sink float64
`, `package main
func f(v float64) float64 {