1
2
3
4
5 package types2_test
6
7 import (
8 "cmd/compile/internal/syntax"
9 "fmt"
10 "testing"
11
12 . "cmd/compile/internal/types2"
13 )
14
15 var builtinCalls = []struct {
16 name, src, sig string
17 }{
18 {"append", `var s []int; _ = append(s)`, `func([]int, ...int) []int`},
19 {"append", `var s []int; _ = append(s, 0)`, `func([]int, ...int) []int`},
20 {"append", `var s []int; _ = (append)(s, 0)`, `func([]int, ...int) []int`},
21 {"append", `var s []byte; _ = ((append))(s, 0)`, `func([]byte, ...byte) []byte`},
22 {"append", `var s []byte; _ = append(s, "foo"...)`, `func([]byte, string...) []byte`},
23 {"append", `type T []byte; var s T; var str string; _ = append(s, str...)`, `func(p.T, string...) p.T`},
24 {"append", `type T []byte; type U string; var s T; var str U; _ = append(s, str...)`, `func(p.T, p.U...) p.T`},
25
26 {"cap", `var s [10]int; _ = cap(s)`, `invalid type`},
27 {"cap", `var s [10]int; _ = cap(&s)`, `invalid type`},
28 {"cap", `var s []int64; _ = cap(s)`, `func([]int64) int`},
29 {"cap", `var c chan<-bool; _ = cap(c)`, `func(chan<- bool) int`},
30 {"cap", `type S []byte; var s S; _ = cap(s)`, `func(p.S) int`},
31 {"cap", `var s P; _ = cap(s)`, `func(P) int`},
32
33 {"len", `_ = len("foo")`, `invalid type`},
34 {"len", `var s string; _ = len(s)`, `func(string) int`},
35 {"len", `var s [10]int; _ = len(s)`, `invalid type`},
36 {"len", `var s [10]int; _ = len(&s)`, `invalid type`},
37 {"len", `var s []int64; _ = len(s)`, `func([]int64) int`},
38 {"len", `var c chan<-bool; _ = len(c)`, `func(chan<- bool) int`},
39 {"len", `var m map[string]float32; _ = len(m)`, `func(map[string]float32) int`},
40 {"len", `type S []byte; var s S; _ = len(s)`, `func(p.S) int`},
41 {"len", `var s P; _ = len(s)`, `func(P) int`},
42
43 {"clear", `var m map[float64]int; clear(m)`, `func(map[float64]int)`},
44 {"clear", `var s []byte; clear(s)`, `func([]byte)`},
45
46 {"close", `var c chan int; close(c)`, `func(chan int)`},
47 {"close", `var c chan<- chan string; close(c)`, `func(chan<- chan string)`},
48
49 {"complex", `_ = complex(1, 0)`, `invalid type`},
50 {"complex", `var re float32; _ = complex(re, 1.0)`, `func(float32, float32) complex64`},
51 {"complex", `var im float64; _ = complex(1, im)`, `func(float64, float64) complex128`},
52 {"complex", `type F32 float32; var re, im F32; _ = complex(re, im)`, `func(p.F32, p.F32) complex64`},
53 {"complex", `type F64 float64; var re, im F64; _ = complex(re, im)`, `func(p.F64, p.F64) complex128`},
54
55 {"copy", `var src, dst []byte; copy(dst, src)`, `func([]byte, []byte) int`},
56 {"copy", `type T [][]int; var src, dst T; _ = copy(dst, src)`, `func(p.T, p.T) int`},
57 {"copy", `var src string; var dst []byte; copy(dst, src)`, `func([]byte, string) int`},
58 {"copy", `type T string; type U []byte; var src T; var dst U; copy(dst, src)`, `func(p.U, p.T) int`},
59 {"copy", `var dst []byte; copy(dst, "hello")`, `func([]byte, string) int`},
60
61 {"delete", `var m map[string]bool; delete(m, "foo")`, `func(map[string]bool, string)`},
62 {"delete", `type (K string; V int); var m map[K]V; delete(m, "foo")`, `func(map[p.K]p.V, p.K)`},
63
64 {"imag", `_ = imag(1i)`, `invalid type`},
65 {"imag", `var c complex64; _ = imag(c)`, `func(complex64) float32`},
66 {"imag", `var c complex128; _ = imag(c)`, `func(complex128) float64`},
67 {"imag", `type C64 complex64; var c C64; _ = imag(c)`, `func(p.C64) float32`},
68 {"imag", `type C128 complex128; var c C128; _ = imag(c)`, `func(p.C128) float64`},
69
70 {"real", `_ = real(1i)`, `invalid type`},
71 {"real", `var c complex64; _ = real(c)`, `func(complex64) float32`},
72 {"real", `var c complex128; _ = real(c)`, `func(complex128) float64`},
73 {"real", `type C64 complex64; var c C64; _ = real(c)`, `func(p.C64) float32`},
74 {"real", `type C128 complex128; var c C128; _ = real(c)`, `func(p.C128) float64`},
75
76 {"make", `_ = make([]int, 10)`, `func([]int, int) []int`},
77 {"make", `type T []byte; _ = make(T, 10, 20)`, `func(p.T, int, int) p.T`},
78
79
80 {"make", ` _ = make([]int, 0 )`, `func([]int, int) []int`},
81 {"make", `var l int; _ = make([]int, l )`, `func([]int, int) []int`},
82 {"make", ` _ = make([]int, 0, 0)`, `func([]int, int, int) []int`},
83 {"make", `var l int; _ = make([]int, l, 0)`, `func([]int, int, int) []int`},
84 {"make", `var c int; _ = make([]int, 0, c)`, `func([]int, int, int) []int`},
85 {"make", `var l, c int; _ = make([]int, l, c)`, `func([]int, int, int) []int`},
86
87
88 {"make", ` _ = make([]int , 0 )`, `func([]int, int) []int`},
89 {"make", `var l byte ; _ = make([]int8 , l )`, `func([]int8, byte) []int8`},
90 {"make", ` _ = make([]int16 , 0, 0)`, `func([]int16, int, int) []int16`},
91 {"make", `var l int16; _ = make([]string , l, 0)`, `func([]string, int16, int) []string`},
92 {"make", `var c int32; _ = make([]float64 , 0, c)`, `func([]float64, int, int32) []float64`},
93 {"make", `var l, c uint ; _ = make([]complex128, l, c)`, `func([]complex128, uint, uint) []complex128`},
94
95
96 {"make", `const l uint = 1; _ = make([]int, l)`, `func([]int, uint) []int`},
97
98 {"max", ` _ = max(0 )`, `invalid type`},
99 {"max", `var x int ; _ = max(x )`, `func(int) int`},
100 {"max", `var x int ; _ = max(0, x )`, `func(int, int) int`},
101 {"max", `var x string ; _ = max("a", x )`, `func(string, string) string`},
102 {"max", `var x float32; _ = max(0, 1.0, x)`, `func(float32, float32, float32) float32`},
103
104 {"min", ` _ = min(0 )`, `invalid type`},
105 {"min", `var x int ; _ = min(x )`, `func(int) int`},
106 {"min", `var x int ; _ = min(0, x )`, `func(int, int) int`},
107 {"min", `var x string ; _ = min("a", x )`, `func(string, string) string`},
108 {"min", `var x float32; _ = min(0, 1.0, x)`, `func(float32, float32, float32) float32`},
109
110 {"new", `_ = new(int)`, `func(int) *int`},
111 {"new", `type T struct{}; _ = new(T)`, `func(p.T) *p.T`},
112
113 {"panic", `panic(0)`, `func(interface{})`},
114 {"panic", `panic("foo")`, `func(interface{})`},
115
116 {"print", `print()`, `func()`},
117 {"print", `print(0)`, `func(int)`},
118 {"print", `print(1, 2.0, "foo", true)`, `func(int, float64, string, bool)`},
119
120 {"println", `println()`, `func()`},
121 {"println", `println(0)`, `func(int)`},
122 {"println", `println(1, 2.0, "foo", true)`, `func(int, float64, string, bool)`},
123
124 {"recover", `recover()`, `func() interface{}`},
125 {"recover", `_ = recover()`, `func() interface{}`},
126
127 {"Add", `var p unsafe.Pointer; _ = unsafe.Add(p, -1.0)`, `func(unsafe.Pointer, int) unsafe.Pointer`},
128 {"Add", `var p unsafe.Pointer; var n uintptr; _ = unsafe.Add(p, n)`, `func(unsafe.Pointer, uintptr) unsafe.Pointer`},
129 {"Add", `_ = unsafe.Add(nil, 0)`, `func(unsafe.Pointer, int) unsafe.Pointer`},
130
131 {"Alignof", `_ = unsafe.Alignof(0)`, `invalid type`},
132 {"Alignof", `var x struct{}; _ = unsafe.Alignof(x)`, `invalid type`},
133 {"Alignof", `var x P; _ = unsafe.Alignof(x)`, `func(P) uintptr`},
134
135 {"Offsetof", `var x struct{f bool}; _ = unsafe.Offsetof(x.f)`, `invalid type`},
136 {"Offsetof", `var x struct{_ int; f bool}; _ = unsafe.Offsetof((&x).f)`, `invalid type`},
137 {"Offsetof", `var x struct{_ int; f P}; _ = unsafe.Offsetof((&x).f)`, `func(P) uintptr`},
138
139 {"Sizeof", `_ = unsafe.Sizeof(0)`, `invalid type`},
140 {"Sizeof", `var x struct{}; _ = unsafe.Sizeof(x)`, `invalid type`},
141 {"Sizeof", `var x P; _ = unsafe.Sizeof(x)`, `func(P) uintptr`},
142
143 {"Slice", `var p *int; _ = unsafe.Slice(p, 1)`, `func(*int, int) []int`},
144 {"Slice", `var p *byte; var n uintptr; _ = unsafe.Slice(p, n)`, `func(*byte, uintptr) []byte`},
145 {"Slice", `type B *byte; var b B; _ = unsafe.Slice(b, 0)`, `func(*byte, int) []byte`},
146
147 {"SliceData", "var s []int; _ = unsafe.SliceData(s)", `func([]int) *int`},
148 {"SliceData", "type S []int; var s S; _ = unsafe.SliceData(s)", `func([]int) *int`},
149
150 {"String", `var p *byte; _ = unsafe.String(p, 1)`, `func(*byte, int) string`},
151 {"String", `type B *byte; var b B; _ = unsafe.String(b, 0)`, `func(*byte, int) string`},
152
153 {"StringData", `var s string; _ = unsafe.StringData(s)`, `func(string) *byte`},
154 {"StringData", `_ = unsafe.StringData("foo")`, `func(string) *byte`},
155
156 {"assert", `assert(true)`, `invalid type`},
157 {"assert", `type B bool; const pred B = 1 < 2; assert(pred)`, `invalid type`},
158
159
160 }
161
162 func TestBuiltinSignatures(t *testing.T) {
163 DefPredeclaredTestFuncs()
164
165 seen := map[string]bool{"trace": true}
166 for _, call := range builtinCalls {
167 testBuiltinSignature(t, call.name, call.src, call.sig)
168 seen[call.name] = true
169 }
170
171
172 for _, name := range Universe.Names() {
173 if _, ok := Universe.Lookup(name).(*Builtin); ok && !seen[name] {
174 t.Errorf("missing test for %s", name)
175 }
176 }
177 for _, name := range Unsafe.Scope().Names() {
178 if _, ok := Unsafe.Scope().Lookup(name).(*Builtin); ok && !seen[name] {
179 t.Errorf("missing test for unsafe.%s", name)
180 }
181 }
182 }
183
184 func testBuiltinSignature(t *testing.T, name, src0, want string) {
185 src := fmt.Sprintf(`package p; import "unsafe"; type _ unsafe.Pointer /* use unsafe */; func _[P ~[]byte]() { %s }`, src0)
186
187 uses := make(map[*syntax.Name]Object)
188 types := make(map[syntax.Expr]TypeAndValue)
189 mustTypecheck(src, nil, &Info{Uses: uses, Types: types})
190
191
192 n := 0
193 var fun syntax.Expr
194 for x := range types {
195 if call, _ := x.(*syntax.CallExpr); call != nil {
196 fun = call.Fun
197 n++
198 }
199 }
200 if n != 1 {
201 t.Errorf("%s: got %d CallExprs; want 1", src0, n)
202 return
203 }
204
205
206 for {
207
208 typ := types[fun].Type
209 if typ == nil {
210 t.Errorf("%s: no type recorded for %s", src0, ExprString(fun))
211 return
212 }
213 if got := typ.String(); got != want {
214 t.Errorf("%s: got type %s; want %s", src0, got, want)
215 return
216 }
217
218
219
220 switch p := fun.(type) {
221 case *syntax.Name:
222 obj := uses[p]
223 if obj == nil {
224 t.Errorf("%s: no object found for %s", src0, p.Value)
225 return
226 }
227 bin, _ := obj.(*Builtin)
228 if bin == nil {
229 t.Errorf("%s: %s does not denote a built-in", src0, p.Value)
230 return
231 }
232 if bin.Name() != name {
233 t.Errorf("%s: got built-in %s; want %s", src0, bin.Name(), name)
234 return
235 }
236 return
237
238 case *syntax.ParenExpr:
239 fun = p.X
240
241 case *syntax.SelectorExpr:
242
243 return
244
245 default:
246 t.Errorf("%s: invalid function call", src0)
247 return
248 }
249 }
250 }
251
View as plain text