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.
5 // This file implements tests for various issues.
10 "cmd/compile/internal/syntax"
18 . "cmd/compile/internal/types2"
21 func TestIssue5770(t *testing.T) {
22 f := mustParse("", `package p; type S struct{T}`)
24 _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, nil) // do not crash
25 const want = "undefined: T"
26 if err == nil || !strings.Contains(err.Error(), want) {
27 t.Errorf("got: %v; want: %s", err, want)
31 func TestIssue5849(t *testing.T) {
39 _ = uint64(64 << s + s)
40 _ = (interface{})("foo")
41 _ = (interface{})(nil)
43 types := make(map[syntax.Expr]TypeAndValue)
44 mustTypecheck("p", src, &Info{Types: types})
46 for x, tv := range types {
48 switch x := x.(type) {
49 case *syntax.BasicLit:
58 want = Typ[Uint] // because of "+ s", s is of type uint
64 want = NewInterfaceType(nil, nil) // interface{} (for now, go/types types this as "untyped nil")
67 if want != nil && !Identical(tv.Type, want) {
68 t.Errorf("got %s; want %s", tv.Type, want)
73 func TestIssue6413(t *testing.T) {
82 types := make(map[syntax.Expr]TypeAndValue)
83 mustTypecheck("p", src, &Info{Types: types})
87 for x, tv := range types {
88 if _, ok := x.(*syntax.CallExpr); ok {
90 t.Errorf("%s: got %s; want %s", x.Pos(), tv.Type, want)
97 t.Errorf("got %d CallExprs; want 2", n)
101 func TestIssue7245(t *testing.T) {
104 func (T) m() (res bool) { return }
105 type T struct{} // receiver type after method declaration
107 f := mustParse("", src)
110 defs := make(map[*syntax.Name]Object)
111 _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, &Info{Defs: defs})
116 m := f.DeclList[0].(*syntax.FuncDecl)
117 res1 := defs[m.Name].(*Func).Type().(*Signature).Results().At(0)
118 res2 := defs[m.Type.ResultList[0].Name].(*Var)
121 t.Errorf("got %s (%p) != %s (%p)", res1, res2, res1, res2)
125 // This tests that uses of existing vars on the LHS of an assignment
126 // are Uses, not Defs; and also that the (illegal) use of a non-var on
127 // the LHS of an assignment is a Use nonetheless.
128 func TestIssue7827(t *testing.T) {
132 const w = 1 // defs w
133 x, y := 2, 3 // defs x, y
134 w, x, z := 4, 5, 6 // uses w, x, defs z; error: cannot assign to w
135 _, _, _ = x, y, z // uses x, y, z
138 f := mustParse("", src)
140 const want = `L3 defs func p._()
141 L4 defs const w untyped int
145 L6 uses const w untyped int
151 // don't abort at the first error
152 conf := Config{Error: func(err error) { t.Log(err) }}
153 defs := make(map[*syntax.Name]Object)
154 uses := make(map[*syntax.Name]Object)
155 _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, &Info{Defs: defs, Uses: uses})
156 if s := fmt.Sprint(err); !strings.HasSuffix(s, "cannot assign to w") {
157 t.Errorf("Check: unexpected error: %s", s)
161 for id, obj := range defs {
163 fact := fmt.Sprintf("L%d defs %s", id.Pos().Line(), obj)
164 facts = append(facts, fact)
167 for id, obj := range uses {
168 fact := fmt.Sprintf("L%d uses %s", id.Pos().Line(), obj)
169 facts = append(facts, fact)
173 got := strings.Join(facts, "\n")
175 t.Errorf("Unexpected defs/uses\ngot:\n%s\nwant:\n%s", got, want)
179 // This tests that the package associated with the types2.Object.Pkg method
180 // is the type's package independent of the order in which the imports are
181 // listed in the sources src1, src2 below.
182 // The actual issue is in go/internal/gcimporter which has a corresponding
183 // test; we leave this test here to verify correct behavior at the go/types
185 func TestIssue13898(t *testing.T) {
186 testenv.MustHaveGoBuild(t)
195 for _, obj := range info.Uses {
200 // like src0, but also imports go/importer
211 for _, obj := range info.Uses {
216 // like src1 but with different import order
217 // (used to fail with this issue)
228 for _, obj := range info.Uses {
233 f := func(test, src string) {
234 f := mustParse("", src)
235 conf := Config{Importer: defaultImporter()}
236 info := Info{Uses: make(map[*syntax.Name]Object)}
237 _, err := conf.Check("main", []*syntax.File{f}, &info)
244 for id, obj := range info.Uses {
245 if id.Value == "Pkg" {
251 t.Fatalf("%s: got %d entries named Pkg; want 1", test, count)
253 if pkg.Name() != "types" {
254 t.Fatalf("%s: got %v; want package types2", test, pkg)
263 func TestIssue22525(t *testing.T) {
264 f := mustParse("", `package p; func f() { var a, b, c, d, e int }`)
267 conf := Config{Error: func(err error) { got += err.Error() + "\n" }}
268 conf.Check(f.PkgName.Value, []*syntax.File{f}, nil) // do not crash
270 :1:27: a declared and not used
271 :1:30: b declared and not used
272 :1:33: c declared and not used
273 :1:36: d declared and not used
274 :1:39: e declared and not used
277 t.Errorf("got: %swant: %s", got, want)
281 func TestIssue25627(t *testing.T) {
282 const prefix = `package p; import "unsafe"; type P *struct{}; type I interface{}; type T `
283 // The src strings (without prefix) are constructed such that the number of semicolons
284 // plus one corresponds to the number of fields expected in the respective struct.
285 for _, src := range []string{
286 `struct { x Missing }`,
287 `struct { Missing }`,
288 `struct { *Missing }`,
289 `struct { unsafe.Pointer }`,
292 `struct { a int; b Missing; *Missing }`,
294 f := mustParse("", prefix+src)
296 conf := Config{Importer: defaultImporter(), Error: func(err error) {}}
297 info := &Info{Types: make(map[syntax.Expr]TypeAndValue)}
298 _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, info)
300 if _, ok := err.(Error); !ok {
305 syntax.Crawl(f, func(n syntax.Node) bool {
306 if decl, _ := n.(*syntax.TypeDecl); decl != nil {
307 if tv, ok := info.Types[decl.Type]; ok && decl.Name.Value == "T" {
308 want := strings.Count(src, ";") + 1
309 if got := tv.Type.(*Struct).NumFields(); got != want {
310 t.Errorf("%s: got %d fields; want %d", src, got, want)
319 func TestIssue28005(t *testing.T) {
320 // method names must match defining interface name for this test
321 // (see last comment in this function)
322 sources := [...]string{
323 "package p; type A interface{ A() }",
324 "package p; type B interface{ B() }",
325 "package p; type X interface{ A; B }",
328 // compute original file ASTs
329 var orig [len(sources)]*syntax.File
330 for i, src := range sources {
331 orig[i] = mustParse("", src)
334 // run the test for all order permutations of the incoming files
335 for _, perm := range [][len(sources)]int{
343 // create file order permutation
344 files := make([]*syntax.File, len(sources))
345 for i := range perm {
346 files[i] = orig[perm[i]]
349 // type-check package with given file order permutation
351 info := &Info{Defs: make(map[*syntax.Name]Object)}
352 _, err := conf.Check("", files, info)
357 // look for interface object X
359 for name, def := range info.Defs {
360 if name.Value == "X" {
366 t.Fatal("object X not found")
368 iface := obj.Type().Underlying().(*Interface) // object X must be an interface
370 // Each iface method m is embedded; and m's receiver base type name
371 // must match the method's name per the choice in the source file.
372 for i := 0; i < iface.NumMethods(); i++ {
374 recvName := m.Type().(*Signature).Recv().Type().(*Named).Obj().Name()
375 if recvName != m.Name() {
376 t.Errorf("perm %v: got recv %s; want %s", perm, recvName, m.Name())
382 func TestIssue28282(t *testing.T) {
383 // create type interface { error }
384 et := Universe.Lookup("error").Type()
385 it := NewInterfaceType(nil, []Type{et})
386 // verify that after completing the interface, the embedded method remains unchanged
387 // (interfaces are "completed" lazily now, so the completion happens implicitly when
388 // accessing Method(0))
389 want := et.Underlying().(*Interface).Method(0)
392 t.Fatalf("%s.Method(0): got %q (%p); want %q (%p)", it, got, got, want, want)
394 // verify that lookup finds the same method in both interfaces (redundant check)
395 obj, _, _ := LookupFieldOrMethod(et, false, nil, "Error")
397 t.Fatalf("%s.Lookup: got %q (%p); want %q (%p)", et, obj, obj, want, want)
399 obj, _, _ = LookupFieldOrMethod(it, false, nil, "Error")
401 t.Fatalf("%s.Lookup: got %q (%p); want %q (%p)", it, obj, obj, want, want)
405 func TestIssue29029(t *testing.T) {
406 f1 := mustParse("", `package p; type A interface { M() }`)
407 f2 := mustParse("", `package p; var B interface { A }`)
409 // printInfo prints the *Func definitions recorded in info, one *Func per line.
410 printInfo := func(info *Info) string {
411 var buf strings.Builder
412 for _, obj := range info.Defs {
413 if fn, ok := obj.(*Func); ok {
414 fmt.Fprintln(&buf, fn)
420 // The *Func (method) definitions for package p must be the same
421 // independent on whether f1 and f2 are type-checked together, or
424 // type-check together
426 info := &Info{Defs: make(map[*syntax.Name]Object)}
427 check := NewChecker(&conf, NewPackage("", "p"), info)
428 if err := check.Files([]*syntax.File{f1, f2}); err != nil {
431 want := printInfo(info)
433 // type-check incrementally
434 info = &Info{Defs: make(map[*syntax.Name]Object)}
435 check = NewChecker(&conf, NewPackage("", "p"), info)
436 if err := check.Files([]*syntax.File{f1}); err != nil {
439 if err := check.Files([]*syntax.File{f2}); err != nil {
442 got := printInfo(info)
445 t.Errorf("\ngot : %swant: %s", got, want)
449 func TestIssue34151(t *testing.T) {
450 const asrc = `package a; type I interface{ M() }; type T struct { F interface { I } }`
451 const bsrc = `package b; import "a"; type T struct { F interface { a.I } }; var _ = a.T(T{})`
453 a := mustTypecheck("a", asrc, nil)
455 bast := mustParse("", bsrc)
456 conf := Config{Importer: importHelper{pkg: a}}
457 b, err := conf.Check(bast.PkgName.Value, []*syntax.File{bast}, nil)
459 t.Errorf("package %s failed to typecheck: %v", b.Name(), err)
463 type importHelper struct {
468 func (h importHelper) Import(path string) (*Package, error) {
469 if path == h.pkg.Path() {
472 if h.fallback == nil {
473 return nil, fmt.Errorf("got package path %q; want %q", path, h.pkg.Path())
475 return h.fallback.Import(path)
478 // TestIssue34921 verifies that we don't update an imported type's underlying
479 // type when resolving an underlying type. Specifically, when determining the
480 // underlying type of b.T (which is the underlying type of a.T, which is int)
481 // we must not set the underlying type of a.T again since that would lead to
482 // a race condition if package b is imported elsewhere, in a package that is
483 // concurrently type-checked.
484 func TestIssue34921(t *testing.T) {
486 if r := recover(); r != nil {
491 var sources = []string{
492 `package a; type T int`,
493 `package b; import "a"; type T a.T`,
497 for _, src := range sources {
498 f := mustParse("", src)
499 conf := Config{Importer: importHelper{pkg: pkg}}
500 res, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, nil)
502 t.Errorf("%q failed to typecheck: %v", src, err)
504 pkg = res // res is imported by the next package in this test
508 func TestIssue43088(t *testing.T) {
518 n1 := NewTypeName(syntax.Pos{}, nil, "T1", nil)
519 T1 := NewNamed(n1, nil, nil)
520 n2 := NewTypeName(syntax.Pos{}, nil, "T2", nil)
521 T2 := NewNamed(n2, nil, nil)
522 s1 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
524 s2 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
525 s3 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", s2, false)}, nil)
528 // These calls must terminate (no endless recursion).
533 func TestIssue44515(t *testing.T) {
534 typ := Unsafe.Scope().Lookup("Pointer").Type()
536 got := TypeString(typ, nil)
537 want := "unsafe.Pointer"
539 t.Errorf("got %q; want %q", got, want)
542 qf := func(pkg *Package) string {
548 got = TypeString(typ, qf)
551 t.Errorf("got %q; want %q", got, want)
555 func TestIssue43124(t *testing.T) {
556 testenv.MustHaveGoBuild(t)
558 // All involved packages have the same name (template). Error messages should
559 // disambiguate between text/template and html/template by printing the full
562 asrc = `package a; import "text/template"; func F(template.Template) {}; func G(int) {}`
563 bsrc = `package b; import ("a"; "html/template"); func _() { a.F(template.Template{}) }`
564 csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }`
567 a := mustTypecheck("a", asrc, nil)
568 conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}}
570 // Packages should be fully qualified when there is ambiguity within the
571 // error string itself.
572 bast := mustParse("", bsrc)
573 _, err := conf.Check(bast.PkgName.Value, []*syntax.File{bast}, nil)
575 t.Fatal("package b had no errors")
577 if !strings.Contains(err.Error(), "text/template") || !strings.Contains(err.Error(), "html/template") {
578 t.Errorf("type checking error for b does not disambiguate package template: %q", err)
581 // ...and also when there is any ambiguity in reachable packages.
582 cast := mustParse("", csrc)
583 _, err = conf.Check(cast.PkgName.Value, []*syntax.File{cast}, nil)
585 t.Fatal("package c had no errors")
587 if !strings.Contains(err.Error(), "html/template") {
588 t.Errorf("type checking error for c does not disambiguate package template: %q", err)
592 func TestIssue50646(t *testing.T) {
593 anyType := Universe.Lookup("any").Type()
594 comparableType := Universe.Lookup("comparable").Type()
596 if !Comparable(anyType) {
597 t.Error("any is not a comparable type")
599 if !Comparable(comparableType) {
600 t.Error("comparable is not a comparable type")
603 if Implements(anyType, comparableType.Underlying().(*Interface)) {
604 t.Error("any implements comparable")
606 if !Implements(comparableType, anyType.(*Interface)) {
607 t.Error("comparable does not implement any")
610 if AssignableTo(anyType, comparableType) {
611 t.Error("any assignable to comparable")
613 if !AssignableTo(comparableType, anyType) {
614 t.Error("comparable not assignable to any")
618 func TestIssue55030(t *testing.T) {
619 // makeSig makes the signature func(typ...)
620 makeSig := func(typ Type) {
621 par := NewVar(nopos, nil, "", typ)
622 params := NewTuple(par)
623 NewSignatureType(nil, nil, nil, params, nil, true)
626 // makeSig must not panic for the following (example) types:
628 makeSig(NewSlice(Typ[Int]))
633 // P where P's core type is string
635 P := NewTypeName(nopos, nil, "P", nil) // [P string]
636 makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{Typ[String]})))
639 // P where P's core type is an (unnamed) slice
641 P := NewTypeName(nopos, nil, "P", nil) // [P []int]
642 makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewSlice(Typ[Int])})))
645 // P where P's core type is bytestring (i.e., string or []byte)
647 t1 := NewTerm(true, Typ[String]) // ~string
648 t2 := NewTerm(false, NewSlice(Typ[Byte])) // []byte
649 u := NewUnion([]*Term{t1, t2}) // ~string | []byte
650 P := NewTypeName(nopos, nil, "P", nil) // [P ~string | []byte]
651 makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{u})))
655 func TestIssue51093(t *testing.T) {
656 // Each test stands for a conversion of the form P(val)
657 // where P is a type parameter with typ as constraint.
658 // The test ensures that P(val) has the correct type P
659 // and is not a constant.
660 var tests = []struct {
669 {"complex64", "1.25"},
670 {"string", "\"foo\""},
672 // some more complex constraints
674 {"~int | ~float64 | complex128", "1"},
675 {"~uint64 | ~rune", "'X'"},
678 for _, test := range tests {
679 src := fmt.Sprintf("package p; func _[P %s]() { _ = P(%s) }", test.typ, test.val)
680 types := make(map[syntax.Expr]TypeAndValue)
681 mustTypecheck("p", src, &Info{Types: types})
684 for x, tv := range types {
685 if x, _ := x.(*syntax.CallExpr); x != nil {
686 // there must be exactly one CallExpr which is the P(val) conversion
688 tpar, _ := tv.Type.(*TypeParam)
690 t.Fatalf("%s: got type %s, want type parameter", syntax.String(x), tv.Type)
692 if name := tpar.Obj().Name(); name != "P" {
693 t.Fatalf("%s: got type parameter name %s, want P", syntax.String(x), name)
695 // P(val) must not be constant
697 t.Errorf("%s: got constant value %s (%s), want no constant", syntax.String(x), tv.Value, tv.Value.String())
703 t.Fatalf("%s: got %d CallExpr nodes; want 1", src, 1)
708 func TestIssue54258(t *testing.T) {
709 tests := []struct{ main, b, want string }{
710 { //---------------------------------------------------------------
714 M0(w struct{ f string })
720 func (S) M0(struct{ f string }) {}
722 `6:12: cannot use b[.]S{} [(]value of type b[.]S[)] as I0 value in variable declaration: b[.]S does not implement I0 [(]wrong type for method M0[)]
723 .*have M0[(]struct{f string /[*] package b [*]/ }[)]
724 .*want M0[(]struct{f string /[*] package main [*]/ }[)]`},
726 { //---------------------------------------------------------------
736 func (S) M1(struct{ string }) {}
738 `6:12: cannot use b[.]S{} [(]value of type b[.]S[)] as I1 value in variable declaration: b[.]S does not implement I1 [(]wrong type for method M1[)]
739 .*have M1[(]struct{string /[*] package b [*]/ }[)]
740 .*want M1[(]struct{string /[*] package main [*]/ }[)]`},
742 { //---------------------------------------------------------------
746 M2(y struct{ f struct{ f string } })
752 func (S) M2(struct{ f struct{ f string } }) {}
754 `6:12: cannot use b[.]S{} [(]value of type b[.]S[)] as I2 value in variable declaration: b[.]S does not implement I2 [(]wrong type for method M2[)]
755 .*have M2[(]struct{f struct{f string} /[*] package b [*]/ }[)]
756 .*want M2[(]struct{f struct{f string} /[*] package main [*]/ }[)]`},
758 { //---------------------------------------------------------------
762 M3(z struct{ F struct{ f string } })
768 func (S) M3(struct{ F struct{ f string } }) {}
770 `6:12: cannot use b[.]S{} [(]value of type b[.]S[)] as I3 value in variable declaration: b[.]S does not implement I3 [(]wrong type for method M3[)]
771 .*have M3[(]struct{F struct{f string /[*] package b [*]/ }}[)]
772 .*want M3[(]struct{F struct{f string /[*] package main [*]/ }}[)]`},
774 { //---------------------------------------------------------------
778 M4(_ struct { *string })
784 func (S) M4(struct { *string }) {}
786 `6:12: cannot use b[.]S{} [(]value of type b[.]S[)] as I4 value in variable declaration: b[.]S does not implement I4 [(]wrong type for method M4[)]
787 .*have M4[(]struct{[*]string /[*] package b [*]/ }[)]
788 .*want M4[(]struct{[*]string /[*] package main [*]/ }[)]`},
790 { //---------------------------------------------------------------
793 type t struct{ A int }
801 type t struct{ A int }
802 func (S) M5(struct {S;t}) {}
804 `7:12: cannot use b[.]S{} [(]value of type b[.]S[)] as I5 value in variable declaration: b[.]S does not implement I5 [(]wrong type for method M5[)]
805 .*have M5[(]struct{b[.]S; b[.]t}[)]
806 .*want M5[(]struct{b[.]S; t}[)]`},
809 test := func(main, b, want string) {
810 re := regexp.MustCompile(want)
811 bpkg := mustTypecheck("b", b, nil)
812 mast := mustParse("main.go", main)
813 conf := Config{Importer: importHelper{pkg: bpkg}}
814 _, err := conf.Check(mast.PkgName.Value, []*syntax.File{mast}, nil)
816 t.Error("Expected failure, but it did not")
817 } else if got := err.Error(); !re.MatchString(got) {
818 t.Errorf("Wanted match for\n\t%s\n but got\n\t%s", want, got)
819 } else if testing.Verbose() {
820 t.Logf("Saw expected\n\t%s", err.Error())
823 for _, t := range tests {
824 test(t.main, t.b, t.want)