]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/issues_test.go
[dev.fuzz] all: merge master (d137b74) into dev.fuzz
[gostls13.git] / src / cmd / compile / internal / types2 / issues_test.go
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 // This file implements tests for various issues.
6
7 package types2_test
8
9 import (
10         "bytes"
11         "cmd/compile/internal/syntax"
12         "fmt"
13         "internal/testenv"
14         "sort"
15         "strings"
16         "testing"
17
18         . "cmd/compile/internal/types2"
19 )
20
21 func mustParse(t *testing.T, src string) *syntax.File {
22         f, err := parseSrc("", src)
23         if err != nil {
24                 t.Fatal(err)
25         }
26         return f
27 }
28 func TestIssue5770(t *testing.T) {
29         f := mustParse(t, `package p; type S struct{T}`)
30         var conf Config
31         _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, nil) // do not crash
32         want := "undeclared name: T"
33         if err == nil || !strings.Contains(err.Error(), want) {
34                 t.Errorf("got: %v; want: %s", err, want)
35         }
36 }
37
38 func TestIssue5849(t *testing.T) {
39         src := `
40 package p
41 var (
42         s uint
43         _ = uint8(8)
44         _ = uint16(16) << s
45         _ = uint32(32 << s)
46         _ = uint64(64 << s + s)
47         _ = (interface{})("foo")
48         _ = (interface{})(nil)
49 )`
50         f := mustParse(t, src)
51
52         var conf Config
53         types := make(map[syntax.Expr]TypeAndValue)
54         _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, &Info{Types: types})
55         if err != nil {
56                 t.Fatal(err)
57         }
58
59         for x, tv := range types {
60                 var want Type
61                 switch x := x.(type) {
62                 case *syntax.BasicLit:
63                         switch x.Value {
64                         case `8`:
65                                 want = Typ[Uint8]
66                         case `16`:
67                                 want = Typ[Uint16]
68                         case `32`:
69                                 want = Typ[Uint32]
70                         case `64`:
71                                 want = Typ[Uint] // because of "+ s", s is of type uint
72                         case `"foo"`:
73                                 want = Typ[String]
74                         }
75                 case *syntax.Name:
76                         if x.Value == "nil" {
77                                 want = NewInterfaceType(nil, nil) // interface{} (for now, go/types types this as "untyped nil")
78                         }
79                 }
80                 if want != nil && !Identical(tv.Type, want) {
81                         t.Errorf("got %s; want %s", tv.Type, want)
82                 }
83         }
84 }
85
86 func TestIssue6413(t *testing.T) {
87         src := `
88 package p
89 func f() int {
90         defer f()
91         go f()
92         return 0
93 }
94 `
95         f := mustParse(t, src)
96
97         var conf Config
98         types := make(map[syntax.Expr]TypeAndValue)
99         _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, &Info{Types: types})
100         if err != nil {
101                 t.Fatal(err)
102         }
103
104         want := Typ[Int]
105         n := 0
106         for x, tv := range types {
107                 if _, ok := x.(*syntax.CallExpr); ok {
108                         if tv.Type != want {
109                                 t.Errorf("%s: got %s; want %s", x.Pos(), tv.Type, want)
110                         }
111                         n++
112                 }
113         }
114
115         if n != 2 {
116                 t.Errorf("got %d CallExprs; want 2", n)
117         }
118 }
119
120 func TestIssue7245(t *testing.T) {
121         src := `
122 package p
123 func (T) m() (res bool) { return }
124 type T struct{} // receiver type after method declaration
125 `
126         f := mustParse(t, src)
127
128         var conf Config
129         defs := make(map[*syntax.Name]Object)
130         _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, &Info{Defs: defs})
131         if err != nil {
132                 t.Fatal(err)
133         }
134
135         m := f.DeclList[0].(*syntax.FuncDecl)
136         res1 := defs[m.Name].(*Func).Type().(*Signature).Results().At(0)
137         res2 := defs[m.Type.ResultList[0].Name].(*Var)
138
139         if res1 != res2 {
140                 t.Errorf("got %s (%p) != %s (%p)", res1, res2, res1, res2)
141         }
142 }
143
144 // This tests that uses of existing vars on the LHS of an assignment
145 // are Uses, not Defs; and also that the (illegal) use of a non-var on
146 // the LHS of an assignment is a Use nonetheless.
147 func TestIssue7827(t *testing.T) {
148         const src = `
149 package p
150 func _() {
151         const w = 1        // defs w
152         x, y := 2, 3       // defs x, y
153         w, x, z := 4, 5, 6 // uses w, x, defs z; error: cannot assign to w
154         _, _, _ = x, y, z  // uses x, y, z
155 }
156 `
157         f := mustParse(t, src)
158
159         const want = `L3 defs func p._()
160 L4 defs const w untyped int
161 L5 defs var x int
162 L5 defs var y int
163 L6 defs var z int
164 L6 uses const w untyped int
165 L6 uses var x int
166 L7 uses var x int
167 L7 uses var y int
168 L7 uses var z int`
169
170         // don't abort at the first error
171         conf := Config{Error: func(err error) { t.Log(err) }}
172         defs := make(map[*syntax.Name]Object)
173         uses := make(map[*syntax.Name]Object)
174         _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, &Info{Defs: defs, Uses: uses})
175         if s := fmt.Sprint(err); !strings.HasSuffix(s, "cannot assign to w") {
176                 t.Errorf("Check: unexpected error: %s", s)
177         }
178
179         var facts []string
180         for id, obj := range defs {
181                 if obj != nil {
182                         fact := fmt.Sprintf("L%d defs %s", id.Pos().Line(), obj)
183                         facts = append(facts, fact)
184                 }
185         }
186         for id, obj := range uses {
187                 fact := fmt.Sprintf("L%d uses %s", id.Pos().Line(), obj)
188                 facts = append(facts, fact)
189         }
190         sort.Strings(facts)
191
192         got := strings.Join(facts, "\n")
193         if got != want {
194                 t.Errorf("Unexpected defs/uses\ngot:\n%s\nwant:\n%s", got, want)
195         }
196 }
197
198 // This tests that the package associated with the types2.Object.Pkg method
199 // is the type's package independent of the order in which the imports are
200 // listed in the sources src1, src2 below.
201 // The actual issue is in go/internal/gcimporter which has a corresponding
202 // test; we leave this test here to verify correct behavior at the go/types
203 // level.
204 func TestIssue13898(t *testing.T) {
205         testenv.MustHaveGoBuild(t)
206
207         const src0 = `
208 package main
209
210 import "go/types"
211
212 func main() {
213         var info types.Info
214         for _, obj := range info.Uses {
215                 _ = obj.Pkg()
216         }
217 }
218 `
219         // like src0, but also imports go/importer
220         const src1 = `
221 package main
222
223 import (
224         "go/types"
225         _ "go/importer"
226 )
227
228 func main() {
229         var info types.Info
230         for _, obj := range info.Uses {
231                 _ = obj.Pkg()
232         }
233 }
234 `
235         // like src1 but with different import order
236         // (used to fail with this issue)
237         const src2 = `
238 package main
239
240 import (
241         _ "go/importer"
242         "go/types"
243 )
244
245 func main() {
246         var info types.Info
247         for _, obj := range info.Uses {
248                 _ = obj.Pkg()
249         }
250 }
251 `
252         f := func(test, src string) {
253                 f := mustParse(t, src)
254                 conf := Config{Importer: defaultImporter()}
255                 info := Info{Uses: make(map[*syntax.Name]Object)}
256                 _, err := conf.Check("main", []*syntax.File{f}, &info)
257                 if err != nil {
258                         t.Fatal(err)
259                 }
260
261                 var pkg *Package
262                 count := 0
263                 for id, obj := range info.Uses {
264                         if id.Value == "Pkg" {
265                                 pkg = obj.Pkg()
266                                 count++
267                         }
268                 }
269                 if count != 1 {
270                         t.Fatalf("%s: got %d entries named Pkg; want 1", test, count)
271                 }
272                 if pkg.Name() != "types" {
273                         t.Fatalf("%s: got %v; want package types2", test, pkg)
274                 }
275         }
276
277         f("src0", src0)
278         f("src1", src1)
279         f("src2", src2)
280 }
281
282 func TestIssue22525(t *testing.T) {
283         f := mustParse(t, `package p; func f() { var a, b, c, d, e int }`)
284
285         got := "\n"
286         conf := Config{Error: func(err error) { got += err.Error() + "\n" }}
287         conf.Check(f.PkgName.Value, []*syntax.File{f}, nil) // do not crash
288         want := `
289 :1:27: a declared but not used
290 :1:30: b declared but not used
291 :1:33: c declared but not used
292 :1:36: d declared but not used
293 :1:39: e declared but not used
294 `
295         if got != want {
296                 t.Errorf("got: %swant: %s", got, want)
297         }
298 }
299
300 func TestIssue25627(t *testing.T) {
301         const prefix = `package p; import "unsafe"; type P *struct{}; type I interface{}; type T `
302         // The src strings (without prefix) are constructed such that the number of semicolons
303         // plus one corresponds to the number of fields expected in the respective struct.
304         for _, src := range []string{
305                 `struct { x Missing }`,
306                 `struct { Missing }`,
307                 `struct { *Missing }`,
308                 `struct { unsafe.Pointer }`,
309                 `struct { P }`,
310                 `struct { *I }`,
311                 `struct { a int; b Missing; *Missing }`,
312         } {
313                 f := mustParse(t, prefix+src)
314
315                 conf := Config{Importer: defaultImporter(), Error: func(err error) {}}
316                 info := &Info{Types: make(map[syntax.Expr]TypeAndValue)}
317                 _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, info)
318                 if err != nil {
319                         if _, ok := err.(Error); !ok {
320                                 t.Fatal(err)
321                         }
322                 }
323
324                 syntax.Walk(f, func(n syntax.Node) bool {
325                         if decl, _ := n.(*syntax.TypeDecl); decl != nil {
326                                 if tv, ok := info.Types[decl.Type]; ok && decl.Name.Value == "T" {
327                                         want := strings.Count(src, ";") + 1
328                                         if got := tv.Type.(*Struct).NumFields(); got != want {
329                                                 t.Errorf("%s: got %d fields; want %d", src, got, want)
330                                         }
331                                 }
332                         }
333                         return false
334                 })
335         }
336 }
337
338 func TestIssue28005(t *testing.T) {
339         // method names must match defining interface name for this test
340         // (see last comment in this function)
341         sources := [...]string{
342                 "package p; type A interface{ A() }",
343                 "package p; type B interface{ B() }",
344                 "package p; type X interface{ A; B }",
345         }
346
347         // compute original file ASTs
348         var orig [len(sources)]*syntax.File
349         for i, src := range sources {
350                 orig[i] = mustParse(t, src)
351         }
352
353         // run the test for all order permutations of the incoming files
354         for _, perm := range [][len(sources)]int{
355                 {0, 1, 2},
356                 {0, 2, 1},
357                 {1, 0, 2},
358                 {1, 2, 0},
359                 {2, 0, 1},
360                 {2, 1, 0},
361         } {
362                 // create file order permutation
363                 files := make([]*syntax.File, len(sources))
364                 for i := range perm {
365                         files[i] = orig[perm[i]]
366                 }
367
368                 // type-check package with given file order permutation
369                 var conf Config
370                 info := &Info{Defs: make(map[*syntax.Name]Object)}
371                 _, err := conf.Check("", files, info)
372                 if err != nil {
373                         t.Fatal(err)
374                 }
375
376                 // look for interface object X
377                 var obj Object
378                 for name, def := range info.Defs {
379                         if name.Value == "X" {
380                                 obj = def
381                                 break
382                         }
383                 }
384                 if obj == nil {
385                         t.Fatal("object X not found")
386                 }
387                 iface := obj.Type().Underlying().(*Interface) // object X must be an interface
388
389                 // Each iface method m is embedded; and m's receiver base type name
390                 // must match the method's name per the choice in the source file.
391                 for i := 0; i < iface.NumMethods(); i++ {
392                         m := iface.Method(i)
393                         recvName := m.Type().(*Signature).Recv().Type().(*Named).Obj().Name()
394                         if recvName != m.Name() {
395                                 t.Errorf("perm %v: got recv %s; want %s", perm, recvName, m.Name())
396                         }
397                 }
398         }
399 }
400
401 func TestIssue28282(t *testing.T) {
402         // create type interface { error }
403         et := Universe.Lookup("error").Type()
404         it := NewInterfaceType(nil, []Type{et})
405         it.Complete()
406         // verify that after completing the interface, the embedded method remains unchanged
407         want := et.Underlying().(*Interface).Method(0)
408         got := it.Method(0)
409         if got != want {
410                 t.Fatalf("%s.Method(0): got %q (%p); want %q (%p)", it, got, got, want, want)
411         }
412         // verify that lookup finds the same method in both interfaces (redundant check)
413         obj, _, _ := LookupFieldOrMethod(et, false, nil, "Error")
414         if obj != want {
415                 t.Fatalf("%s.Lookup: got %q (%p); want %q (%p)", et, obj, obj, want, want)
416         }
417         obj, _, _ = LookupFieldOrMethod(it, false, nil, "Error")
418         if obj != want {
419                 t.Fatalf("%s.Lookup: got %q (%p); want %q (%p)", it, obj, obj, want, want)
420         }
421 }
422
423 func TestIssue29029(t *testing.T) {
424         f1 := mustParse(t, `package p; type A interface { M() }`)
425         f2 := mustParse(t, `package p; var B interface { A }`)
426
427         // printInfo prints the *Func definitions recorded in info, one *Func per line.
428         printInfo := func(info *Info) string {
429                 var buf bytes.Buffer
430                 for _, obj := range info.Defs {
431                         if fn, ok := obj.(*Func); ok {
432                                 fmt.Fprintln(&buf, fn)
433                         }
434                 }
435                 return buf.String()
436         }
437
438         // The *Func (method) definitions for package p must be the same
439         // independent on whether f1 and f2 are type-checked together, or
440         // incrementally.
441
442         // type-check together
443         var conf Config
444         info := &Info{Defs: make(map[*syntax.Name]Object)}
445         check := NewChecker(&conf, NewPackage("", "p"), info)
446         if err := check.Files([]*syntax.File{f1, f2}); err != nil {
447                 t.Fatal(err)
448         }
449         want := printInfo(info)
450
451         // type-check incrementally
452         info = &Info{Defs: make(map[*syntax.Name]Object)}
453         check = NewChecker(&conf, NewPackage("", "p"), info)
454         if err := check.Files([]*syntax.File{f1}); err != nil {
455                 t.Fatal(err)
456         }
457         if err := check.Files([]*syntax.File{f2}); err != nil {
458                 t.Fatal(err)
459         }
460         got := printInfo(info)
461
462         if got != want {
463                 t.Errorf("\ngot : %swant: %s", got, want)
464         }
465 }
466
467 func TestIssue34151(t *testing.T) {
468         const asrc = `package a; type I interface{ M() }; type T struct { F interface { I } }`
469         const bsrc = `package b; import "a"; type T struct { F interface { a.I } }; var _ = a.T(T{})`
470
471         a, err := pkgFor("a", asrc, nil)
472         if err != nil {
473                 t.Fatalf("package %s failed to typecheck: %v", a.Name(), err)
474         }
475
476         bast := mustParse(t, bsrc)
477         conf := Config{Importer: importHelper{pkg: a}}
478         b, err := conf.Check(bast.PkgName.Value, []*syntax.File{bast}, nil)
479         if err != nil {
480                 t.Errorf("package %s failed to typecheck: %v", b.Name(), err)
481         }
482 }
483
484 type importHelper struct {
485         pkg      *Package
486         fallback Importer
487 }
488
489 func (h importHelper) Import(path string) (*Package, error) {
490         if path == h.pkg.Path() {
491                 return h.pkg, nil
492         }
493         if h.fallback == nil {
494                 return nil, fmt.Errorf("got package path %q; want %q", path, h.pkg.Path())
495         }
496         return h.fallback.Import(path)
497 }
498
499 // TestIssue34921 verifies that we don't update an imported type's underlying
500 // type when resolving an underlying type. Specifically, when determining the
501 // underlying type of b.T (which is the underlying type of a.T, which is int)
502 // we must not set the underlying type of a.T again since that would lead to
503 // a race condition if package b is imported elsewhere, in a package that is
504 // concurrently type-checked.
505 func TestIssue34921(t *testing.T) {
506         defer func() {
507                 if r := recover(); r != nil {
508                         t.Error(r)
509                 }
510         }()
511
512         var sources = []string{
513                 `package a; type T int`,
514                 `package b; import "a"; type T a.T`,
515         }
516
517         var pkg *Package
518         for _, src := range sources {
519                 f := mustParse(t, src)
520                 conf := Config{Importer: importHelper{pkg: pkg}}
521                 res, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, nil)
522                 if err != nil {
523                         t.Errorf("%q failed to typecheck: %v", src, err)
524                 }
525                 pkg = res // res is imported by the next package in this test
526         }
527 }
528
529 func TestIssue43088(t *testing.T) {
530         // type T1 struct {
531         //         _ T2
532         // }
533         //
534         // type T2 struct {
535         //         _ struct {
536         //                 _ T2
537         //         }
538         // }
539         n1 := NewTypeName(syntax.Pos{}, nil, "T1", nil)
540         T1 := NewNamed(n1, nil, nil)
541         n2 := NewTypeName(syntax.Pos{}, nil, "T2", nil)
542         T2 := NewNamed(n2, nil, nil)
543         s1 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
544         T1.SetUnderlying(s1)
545         s2 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
546         s3 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", s2, false)}, nil)
547         T2.SetUnderlying(s3)
548
549         // These calls must terminate (no endless recursion).
550         Comparable(T1)
551         Comparable(T2)
552 }
553
554 func TestIssue44515(t *testing.T) {
555         typ := Unsafe.Scope().Lookup("Pointer").Type()
556
557         got := TypeString(typ, nil)
558         want := "unsafe.Pointer"
559         if got != want {
560                 t.Errorf("got %q; want %q", got, want)
561         }
562
563         qf := func(pkg *Package) string {
564                 if pkg == Unsafe {
565                         return "foo"
566                 }
567                 return ""
568         }
569         got = TypeString(typ, qf)
570         want = "foo.Pointer"
571         if got != want {
572                 t.Errorf("got %q; want %q", got, want)
573         }
574 }
575
576 func TestIssue43124(t *testing.T) {
577         // All involved packages have the same name (template). Error messages should
578         // disambiguate between text/template and html/template by printing the full
579         // path.
580         const (
581                 asrc = `package a; import "text/template"; func F(template.Template) {}; func G(int) {}`
582                 bsrc = `package b; import ("a"; "html/template"); func _() { a.F(template.Template{}) }`
583                 csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }`
584         )
585
586         a, err := pkgFor("a", asrc, nil)
587         if err != nil {
588                 t.Fatalf("package a failed to typecheck: %v", err)
589         }
590         conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}}
591
592         // Packages should be fully qualified when there is ambiguity within the
593         // error string itself.
594         bast := mustParse(t, bsrc)
595         _, err = conf.Check(bast.PkgName.Value, []*syntax.File{bast}, nil)
596         if err == nil {
597                 t.Fatal("package b had no errors")
598         }
599         if !strings.Contains(err.Error(), "text/template") || !strings.Contains(err.Error(), "html/template") {
600                 t.Errorf("type checking error for b does not disambiguate package template: %q", err)
601         }
602
603         // ...and also when there is any ambiguity in reachable packages.
604         cast := mustParse(t, csrc)
605         _, err = conf.Check(cast.PkgName.Value, []*syntax.File{cast}, nil)
606         if err == nil {
607                 t.Fatal("package c had no errors")
608         }
609         if !strings.Contains(err.Error(), "html/template") {
610                 t.Errorf("type checking error for c does not disambiguate package template: %q", err)
611         }
612 }