Debian Codegraph @

go/ir/builder_test.go

raw
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //lint:file-ignore SA1019 go/ssa's test suite is built around the deprecated go/loader. We'll leave fixing that to upstream.
6
7 package ir_test
8
9 import (
10 "bytes"
11 "go/ast"
12 "go/importer"
13 "go/parser"
14 "go/token"
15 "go/types"
16 "os"
17 "reflect"
18 "sort"
19 "testing"
20
21 "honnef.co/go/tools/go/ir"
22 "honnef.co/go/tools/go/ir/irutil"
23
24 "golang.org/x/tools/go/loader"
25 )
26
27 func isEmpty(f *ir.Function) bool { return f.Blocks == nil }
28
29 // Tests that programs partially loaded from gc object files contain
30 // functions with no code for the external portions, but are otherwise ok.
31 func TestBuildPackage(t *testing.T) {
32 input := `
33 package main
34
35 import (
36 "bytes"
37 "io"
38 "testing"
39 )
40
41 func main() {
42 var t testing.T
43 t.Parallel() // static call to external declared method
44 t.Fail() // static call to promoted external declared method
45 testing.Short() // static call to external package-level function
46
47 var w io.Writer = new(bytes.Buffer)
48 w.Write(nil) // interface invoke of external declared method
49 }
50 `
51
52 // Parse the file.
53 fset := token.NewFileSet()
54 f, err := parser.ParseFile(fset, "input.go", input, parser.SkipObjectResolution)
55 if err != nil {
56 t.Error(err)
57 return
58 }
59
60 // Build an IR program from the parsed file.
61 // Load its dependencies from gc binary export data.
62 mainPkg, _, err := irutil.BuildPackage(&types.Config{Importer: importer.Default()}, fset,
63 types.NewPackage("main", ""), []*ast.File{f}, ir.SanityCheckFunctions)
64 if err != nil {
65 t.Error(err)
66 return
67 }
68
69 // The main package, its direct and indirect dependencies are loaded.
70 deps := []string{
71 // directly imported dependencies:
72 "bytes", "io", "testing",
73 // indirect dependencies mentioned by
74 // the direct imports' export data
75 "sync", "unicode", "time",
76 }
77
78 prog := mainPkg.Prog
79 all := prog.AllPackages()
80 if len(all) <= len(deps) {
81 t.Errorf("unexpected set of loaded packages: %q", all)
82 }
83 for _, path := range deps {
84 pkg := prog.ImportedPackage(path)
85 if pkg == nil {
86 t.Errorf("package not loaded: %q", path)
87 continue
88 }
89
90 // External packages should have no function bodies (except for wrappers).
91 isExt := pkg != mainPkg
92
93 // init()
94 if isExt && !isEmpty(pkg.Func("init")) {
95 t.Errorf("external package %s has non-empty init", pkg)
96 } else if !isExt && isEmpty(pkg.Func("init")) {
97 t.Errorf("main package %s has empty init", pkg)
98 }
99
100 for _, mem := range pkg.Members {
101 switch mem := mem.(type) {
102 case *ir.Function:
103 // Functions at package level.
104 if isExt && !isEmpty(mem) {
105 t.Errorf("external function %s is non-empty", mem)
106 } else if !isExt && isEmpty(mem) {
107 t.Errorf("function %s is empty", mem)
108 }
109
110 case *ir.Type:
111 // Methods of named types T.
112 // (In this test, all exported methods belong to *T not T.)
113 if !isExt {
114 t.Fatalf("unexpected name type in main package: %s", mem)
115 }
116 mset := prog.MethodSets.MethodSet(types.NewPointer(mem.Type()))
117 for i, n := 0, mset.Len(); i < n; i++ {
118 m := prog.MethodValue(mset.At(i))
119 // For external types, only synthetic wrappers have code.
120 expExt := m.Synthetic != ir.SyntheticWrapper
121 if expExt && !isEmpty(m) {
122 t.Errorf("external method %s is non-empty: %s",
123 m, m.Synthetic)
124 } else if !expExt && isEmpty(m) {
125 t.Errorf("method function %s is empty: %s",
126 m, m.Synthetic)
127 }
128 }
129 }
130 }
131 }
132
133 expectedCallee := []string{
134 "(*testing.T).Parallel",
135 "(*testing.common).Fail",
136 "testing.Short",
137 "N/A",
138 }
139 callNum := 0
140 for _, b := range mainPkg.Func("main").Blocks {
141 for _, instr := range b.Instrs {
142 switch instr := instr.(type) {
143 case ir.CallInstruction:
144 call := instr.Common()
145 if want := expectedCallee[callNum]; want != "N/A" {
146 got := call.StaticCallee().String()
147 if want != got {
148 t.Errorf("call #%d from main.main: got callee %s, want %s",
149 callNum, got, want)
150 }
151 }
152 callNum++
153 }
154 }
155 }
156 if callNum != 4 {
157 t.Errorf("in main.main: got %d calls, want %d", callNum, 4)
158 }
159 }
160
161 // TestRuntimeTypes tests that (*Program).RuntimeTypes() includes all necessary types.
162 func TestRuntimeTypes(t *testing.T) {
163 tests := []struct {
164 input string
165 want []string
166 }{
167 // An exported package-level type is needed.
168 {`package A; type T struct{}; func (T) f() {}`,
169 []string{"*p.T", "p.T"},
170 },
171 // An unexported package-level type is not needed.
172 {`package B; type t struct{}; func (t) f() {}`,
173 nil,
174 },
175 // Subcomponents of type of exported package-level var are needed.
176 {`package C; import "bytes"; var V struct {*bytes.Buffer}`,
177 []string{"*bytes.Buffer", "*struct{*bytes.Buffer}", "struct{*bytes.Buffer}"},
178 },
179 // Subcomponents of type of unexported package-level var are not needed.
180 {`package D; import "bytes"; var v struct {*bytes.Buffer}`,
181 nil,
182 },
183 // Subcomponents of type of exported package-level function are needed.
184 {`package E; import "bytes"; func F(struct {*bytes.Buffer}) {}`,
185 []string{"*bytes.Buffer", "struct{*bytes.Buffer}"},
186 },
187 // Subcomponents of type of unexported package-level function are not needed.
188 {`package F; import "bytes"; func f(struct {*bytes.Buffer}) {}`,
189 nil,
190 },
191 // Subcomponents of type of exported method of uninstantiated unexported type are not needed.
192 {`package G; import "bytes"; type x struct{}; func (x) G(struct {*bytes.Buffer}) {}; var v x`,
193 nil,
194 },
195 // ...unless used by MakeInterface.
196 {`package G2; import "bytes"; type x struct{}; func (x) G(struct {*bytes.Buffer}) {}; var v interface{} = x{}`,
197 []string{"*bytes.Buffer", "*p.x", "p.x", "struct{*bytes.Buffer}"},
198 },
199 // Subcomponents of type of unexported method are not needed.
200 {`package I; import "bytes"; type X struct{}; func (X) G(struct {*bytes.Buffer}) {}`,
201 []string{"*bytes.Buffer", "*p.X", "p.X", "struct{*bytes.Buffer}"},
202 },
203 // Local types aren't needed.
204 {`package J; import "bytes"; func f() { type T struct {*bytes.Buffer}; var t T; _ = t }`,
205 nil,
206 },
207 // ...unless used by MakeInterface.
208 {`package K; import "bytes"; func f() { type T struct {*bytes.Buffer}; _ = interface{}(T{}) }`,
209 []string{"*bytes.Buffer", "*p.T", "p.T"},
210 },
211 // Types used as operand of MakeInterface are needed.
212 {`package L; import "bytes"; func f() { _ = interface{}(struct{*bytes.Buffer}{}) }`,
213 []string{"*bytes.Buffer", "struct{*bytes.Buffer}"},
214 },
215 // MakeInterface is optimized away when storing to a blank.
216 {`package M; import "bytes"; var _ interface{} = struct{*bytes.Buffer}{}`,
217 nil,
218 },
219 }
220 for _, test := range tests {
221 // Parse the file.
222 fset := token.NewFileSet()
223 f, err := parser.ParseFile(fset, "input.go", test.input, 0)
224 if err != nil {
225 t.Errorf("test %q: %s", test.input[:15], err)
226 continue
227 }
228
229 // Create a single-file main package.
230 // Load dependencies from gc binary export data.
231 irpkg, _, err := irutil.BuildPackage(&types.Config{Importer: importer.Default()}, fset,
232 types.NewPackage("p", ""), []*ast.File{f}, ir.SanityCheckFunctions)
233 if err != nil {
234 t.Errorf("test %q: %s", test.input[:15], err)
235 continue
236 }
237
238 var typstrs []string
239 for _, T := range irpkg.Prog.RuntimeTypes() {
240 typstrs = append(typstrs, T.String())
241 }
242 sort.Strings(typstrs)
243
244 if !reflect.DeepEqual(typstrs, test.want) {
245 t.Errorf("test 'package %s': got %q, want %q",
246 f.Name.Name, typstrs, test.want)
247 }
248 }
249 }
250
251 // TestInit tests that synthesized init functions are correctly formed.
252 func TestInit(t *testing.T) {
253 tests := []struct {
254 mode ir.BuilderMode
255 input, want string
256 }{
257 {0, `package A; import _ "errors"; var i int = 42`,
258 `# Name: A.init
259 # Package: A
260 # Synthetic: package initializer
261 func init():
262 b0: # entry
263 t1 = Const <bool> {true}
264 t2 = Const <int> {42}
265 t3 = Load <bool> init$guard
266 If t3 → b1 b2
267
268 b1: ← b0 b2 # exit
269 Return
270
271 b2: ← b0 # init.start
272 Store {bool} init$guard t1
273 t7 = Call <()> errors.init
274 Store {int} i t2
275 Jump → b1
276
277 `},
278 }
279 for _, test := range tests {
280 // Create a single-file main package.
281 var conf loader.Config
282 f, err := conf.ParseFile("<input>", test.input)
283 if err != nil {
284 t.Errorf("test %q: %s", test.input[:15], err)
285 continue
286 }
287 conf.CreateFromFiles(f.Name.Name, f)
288
289 lprog, err := conf.Load()
290 if err != nil {
291 t.Errorf("test 'package %s': Load: %s", f.Name.Name, err)
292 continue
293 }
294 prog := irutil.CreateProgram(lprog, test.mode)
295 mainPkg := prog.Package(lprog.Created[0].Pkg)
296 prog.Build()
297 initFunc := mainPkg.Func("init")
298 if initFunc == nil {
299 t.Errorf("test 'package %s': no init function", f.Name.Name)
300 continue
301 }
302
303 var initbuf bytes.Buffer
304 _, err = initFunc.WriteTo(&initbuf)
305 if err != nil {
306 t.Errorf("test 'package %s': WriteTo: %s", f.Name.Name, err)
307 continue
308 }
309
310 if initbuf.String() != test.want {
311 t.Errorf("test 'package %s': got %s, want %s", f.Name.Name, initbuf.String(), test.want)
312 }
313 }
314 }
315
316 // TestSyntheticFuncs checks that the expected synthetic functions are
317 // created, reachable, and not duplicated.
318 func TestSyntheticFuncs(t *testing.T) {
319 const input = `package P
320 type T int
321 func (T) f() int
322 func (*T) g() int
323 var (
324 // thunks
325 a = T.f
326 b = T.f
327 c = (struct{T}).f
328 d = (struct{T}).f
329 e = (*T).g
330 f = (*T).g
331 g = (struct{*T}).g
332 h = (struct{*T}).g
333
334 // bounds
335 i = T(0).f
336 j = T(0).f
337 k = new(T).g
338 l = new(T).g
339
340 // wrappers
341 m interface{} = struct{T}{}
342 n interface{} = struct{T}{}
343 o interface{} = struct{*T}{}
344 p interface{} = struct{*T}{}
345 q interface{} = new(struct{T})
346 r interface{} = new(struct{T})
347 s interface{} = new(struct{*T})
348 t interface{} = new(struct{*T})
349 )
350 `
351 // Parse
352 var conf loader.Config
353 f, err := conf.ParseFile("<input>", input)
354 if err != nil {
355 t.Fatalf("parse: %v", err)
356 }
357 conf.CreateFromFiles(f.Name.Name, f)
358
359 // Load
360 lprog, err := conf.Load()
361 if err != nil {
362 t.Fatalf("Load: %v", err)
363 }
364
365 // Create and build IR
366 prog := irutil.CreateProgram(lprog, 0)
367 prog.Build()
368
369 // Enumerate reachable synthetic functions
370 want := map[string]ir.Synthetic{
371 "(*P.T).g$bound": ir.SyntheticBound,
372 "(P.T).f$bound": ir.SyntheticBound,
373
374 "(*P.T).g$thunk": ir.SyntheticThunk,
375 "(P.T).f$thunk": ir.SyntheticThunk,
376 "(struct{*P.T}).g$thunk": ir.SyntheticThunk,
377 "(struct{P.T}).f$thunk": ir.SyntheticThunk,
378
379 "(*P.T).f": ir.SyntheticWrapper,
380 "(*struct{*P.T}).f": ir.SyntheticWrapper,
381 "(*struct{*P.T}).g": ir.SyntheticWrapper,
382 "(*struct{P.T}).f": ir.SyntheticWrapper,
383 "(*struct{P.T}).g": ir.SyntheticWrapper,
384 "(struct{*P.T}).f": ir.SyntheticWrapper,
385 "(struct{*P.T}).g": ir.SyntheticWrapper,
386 "(struct{P.T}).f": ir.SyntheticWrapper,
387
388 "P.init": ir.SyntheticPackageInitializer,
389 }
390 var seen []string // may contain dups
391 for fn := range irutil.AllFunctions(prog) {
392 if fn.Synthetic == 0 {
393 continue
394 }
395 name := fn.String()
396 wantDescr, ok := want[name]
397 if !ok {
398 t.Errorf("got unexpected/duplicate func: %q: %q", name, fn.Synthetic)
399 continue
400 }
401 seen = append(seen, name)
402
403 if wantDescr != fn.Synthetic {
404 t.Errorf("(%s).Synthetic = %q, want %q", name, fn.Synthetic, wantDescr)
405 }
406 }
407
408 for _, name := range seen {
409 delete(want, name)
410 }
411 for fn, descr := range want {
412 t.Errorf("want func: %q: %q", fn, descr)
413 }
414 }
415
416 // TestPhiElimination ensures that dead phis, including those that
417 // participate in a cycle, are properly eliminated.
418 func TestPhiElimination(t *testing.T) {
419 const input = `
420 package p
421
422 func f() error
423
424 func g(slice []int) {
425 for {
426 for range slice {
427 // e should not be lifted to a dead φ-node.
428 e := f()
429 h(e)
430 }
431 }
432 }
433
434 func h(error)
435 `
436 // The IR code for this function should look something like this:
437 // 0:
438 // jump 1
439 // 1:
440 // t0 = len(slice)
441 // jump 2
442 // 2:
443 // t1 = phi [1: -1:int, 3: t2]
444 // t2 = t1 + 1:int
445 // t3 = t2 < t0
446 // if t3 goto 3 else 1
447 // 3:
448 // t4 = f()
449 // t5 = h(t4)
450 // jump 2
451 //
452 // But earlier versions of the IR construction algorithm would
453 // additionally generate this cycle of dead phis:
454 //
455 // 1:
456 // t7 = phi [0: nil:error, 2: t8] #e
457 // ...
458 // 2:
459 // t8 = phi [1: t7, 3: t4] #e
460 // ...
461
462 // Parse
463 var conf loader.Config
464 f, err := conf.ParseFile("<input>", input)
465 if err != nil {
466 t.Fatalf("parse: %v", err)
467 }
468 conf.CreateFromFiles("p", f)
469
470 // Load
471 lprog, err := conf.Load()
472 if err != nil {
473 t.Fatalf("Load: %v", err)
474 }
475
476 // Create and build IR
477 prog := irutil.CreateProgram(lprog, 0)
478 p := prog.Package(lprog.Package("p").Pkg)
479 p.Build()
480 g := p.Func("g")
481
482 phis := 0
483 for _, b := range g.Blocks {
484 for _, instr := range b.Instrs {
485 if _, ok := instr.(*ir.Phi); ok {
486 phis++
487 }
488 }
489 }
490 if expected := 4; phis != expected {
491 g.WriteTo(os.Stderr)
492 t.Errorf("expected %d Phi nodes (for the range index, slice length and slice), got %d", expected, phis)
493 }
494 }
495
496 func TestBuildPackageGo120(t *testing.T) {
497 tests := []struct {
498 name string
499 src string
500 importer types.Importer
501 }{
502 {"slice to array", "package p; var s []byte; var _ = ([4]byte)(s)", nil},
503 {"slice to zero length array", "package p; var s []byte; var _ = ([0]byte)(s)", nil},
504 {"slice to zero length array type parameter", "package p; var s []byte; func f[T ~[0]byte]() { tmp := (T)(s); var z T; _ = tmp == z}", nil},
505 {"slice to non-zero length array type parameter", "package p; var s []byte; func h[T ~[1]byte | [4]byte]() { tmp := T(s); var z T; _ = tmp == z}", nil},
506 {"slice to maybe-zero length array type parameter", "package p; var s []byte; func g[T ~[0]byte | [4]byte]() { tmp := T(s); var z T; _ = tmp == z}", nil},
507 {
508 "rune sequence to sequence cast patterns", `
509 package p
510 // Each of fXX functions describes a 1.20 legal cast between sequences of runes
511 // as []rune, pointers to rune arrays, rune arrays, or strings.
512 //
513 // Comments listed given the current emitted instructions [approximately].
514 // If multiple conversions are needed, these are separated by |.
515 // rune was selected as it leads to string casts (byte is similar).
516 // The length 2 is not significant.
517 // Multiple array lengths may occur in a cast in practice (including 0).
518 func f00[S string, D string](s S) { _ = D(s) } // ChangeType
519 func f01[S string, D []rune](s S) { _ = D(s) } // Convert
520 func f02[S string, D []rune | string](s S) { _ = D(s) } // ChangeType | Convert
521 func f03[S [2]rune, D [2]rune](s S) { _ = D(s) } // ChangeType
522 func f04[S *[2]rune, D *[2]rune](s S) { _ = D(s) } // ChangeType
523 func f05[S []rune, D string](s S) { _ = D(s) } // Convert
524 func f06[S []rune, D [2]rune](s S) { _ = D(s) } // SliceToArray
525 func f07[S []rune, D [2]rune | string](s S) { _ = D(s) } // SliceToArray | Convert
526 func f08[S []rune, D *[2]rune](s S) { _ = D(s) } // SliceToArrayPointer
527 func f09[S []rune, D *[2]rune | string](s S) { _ = D(s) } // SliceToArray | Convert
528 func f10[S []rune, D *[2]rune | [2]rune](s S) { _ = D(s) } // SliceToArrayPointer | SliceToArray
529 func f11[S []rune, D *[2]rune | [2]rune | string](s S) { _ = D(s) } // SliceToArrayPointer | SliceToArray | Convert
530 func f12[S []rune, D []rune](s S) { _ = D(s) } // ChangeType
531 func f13[S []rune, D []rune | string](s S) { _ = D(s) } // Convert | ChangeType
532 func f14[S []rune, D []rune | [2]rune](s S) { _ = D(s) } // ChangeType | SliceToArray
533 func f15[S []rune, D []rune | [2]rune | string](s S) { _ = D(s) } // ChangeType | SliceToArray | Convert
534 func f16[S []rune, D []rune | *[2]rune](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer
535 func f17[S []rune, D []rune | *[2]rune | string](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer | Convert
536 func f18[S []rune, D []rune | *[2]rune | [2]rune](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer | SliceToArray
537 func f19[S []rune, D []rune | *[2]rune | [2]rune | string](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer | SliceToArray | Convert
538 func f20[S []rune | string, D string](s S) { _ = D(s) } // Convert | ChangeType
539 func f21[S []rune | string, D []rune](s S) { _ = D(s) } // Convert | ChangeType
540 func f22[S []rune | string, D []rune | string](s S) { _ = D(s) } // ChangeType | Convert | Convert | ChangeType
541 func f23[S []rune | [2]rune, D [2]rune](s S) { _ = D(s) } // SliceToArray | ChangeType
542 func f24[S []rune | *[2]rune, D *[2]rune](s S) { _ = D(s) } // SliceToArrayPointer | ChangeType
543 `, nil,
544 },
545 {
546 "matching named and underlying types", `
547 package p
548 type a string
549 type b string
550 func g0[S []rune | a | b, D []rune | a | b](s S) { _ = D(s) }
551 func g1[S []rune | ~string, D []rune | a | b](s S) { _ = D(s) }
552 func g2[S []rune | a | b, D []rune | ~string](s S) { _ = D(s) }
553 func g3[S []rune | ~string, D []rune |~string](s S) { _ = D(s) }
554 `, nil,
555 },
556 }
557
558 for _, tc := range tests {
559 t.Run(tc.name, func(t *testing.T) {
560 t.Parallel()
561 fset := token.NewFileSet()
562 f, err := parser.ParseFile(fset, "p.go", tc.src, 0)
563 if err != nil {
564 t.Error(err)
565 }
566 files := []*ast.File{f}
567
568 pkg := types.NewPackage("p", "")
569 conf := &types.Config{Importer: tc.importer}
570 _, _, err = irutil.BuildPackage(conf, fset, pkg, files, ir.SanityCheckFunctions)
571 if err != nil {
572 t.Errorf("unexpected error: %v", err)
573 }
574 })
575 }
576 }
577
578 func TestGo117Builtins(t *testing.T) {
579 tests := []struct {
580 name string
581 src string
582 importer types.Importer
583 }{
584 {"slice to array pointer", "package p; var s []byte; var _ = (*[4]byte)(s)", nil},
585 {"unsafe slice", `package p; import "unsafe"; var _ = unsafe.Add(nil, 0)`, importer.Default()},
586 {"unsafe add", `package p; import "unsafe"; var _ = unsafe.Slice((*int)(nil), 0)`, importer.Default()},
587 }
588
589 for _, tc := range tests {
590 t.Run(tc.name, func(t *testing.T) {
591 t.Parallel()
592 fset := token.NewFileSet()
593 f, err := parser.ParseFile(fset, "p.go", tc.src, parser.ParseComments|parser.SkipObjectResolution)
594 if err != nil {
595 t.Error(err)
596 }
597 files := []*ast.File{f}
598
599 pkg := types.NewPackage("p", "")
600 conf := &types.Config{Importer: tc.importer}
601 if _, _, err := irutil.BuildPackage(conf, fset, pkg, files, ir.SanityCheckFunctions); err != nil {
602 t.Errorf("unexpected error: %v", err)
603 }
604 })
605 }
606 }
607
608 // TestLabels just tests that anonymous labels are handled.
609 func TestLabels(t *testing.T) {
610 tests := []string{
611 `package main
612 func main() { _:println(1) }`,
613 `package main
614 func main() { _:println(1); _:println(2)}`,
615 }
616 for _, test := range tests {
617 conf := loader.Config{Fset: token.NewFileSet()}
618 f, err := parser.ParseFile(conf.Fset, "<input>", test, 0)
619 if err != nil {
620 t.Errorf("parse error: %s", err)
621 return
622 }
623 conf.CreateFromFiles("main", f)
624 iprog, err := conf.Load()
625 if err != nil {
626 t.Error(err)
627 continue
628 }
629 prog := irutil.CreateProgram(iprog, ir.BuilderMode(0))
630 pkg := prog.Package(iprog.Created[0].Pkg)
631 pkg.Build()
632 }
633 }
634
635 func TestUnreachableExit(t *testing.T) {
636 tests := []string{
637 `
638 package pkg
639
640 func foo() (err error) {
641 if true {
642 println()
643 }
644
645 println()
646
647 for {
648 err = nil
649 }
650 }`,
651 }
652 for _, test := range tests {
653 conf := loader.Config{Fset: token.NewFileSet()}
654 f, err := parser.ParseFile(conf.Fset, "<input>", test, 0)
655 if err != nil {
656 t.Errorf("parse error: %s", err)
657 return
658 }
659 conf.CreateFromFiles("pkg", f)
660 iprog, err := conf.Load()
661 if err != nil {
662 t.Error(err)
663 continue
664 }
665 prog := irutil.CreateProgram(iprog, ir.BuilderMode(0))
666 pkg := prog.Package(iprog.Created[0].Pkg)
667 pkg.Build()
668 }
669 }