1 // Copyright 2018 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.
16 var importedObjectTests = []struct {
21 {"crypto.Hash", "type Hash uint"},
22 {"go/ast.ObjKind", "type ObjKind int"},
23 {"go/types.Qualifier", "type Qualifier func(*Package) string"},
24 {"go/types.Comparable", "func Comparable(T Type) bool"},
25 {"math.Pi", "const Pi untyped float"},
26 {"math.Sin", "func Sin(x float64) float64"},
27 {"go/ast.NotNilFilter", "func NotNilFilter(_ string, v reflect.Value) bool"},
28 {"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string)"},
31 {"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"},
32 {"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"},
33 {"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"},
34 {"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
35 {"io.ReadWriter", "type ReadWriter interface{Reader; Writer}"},
36 {"go/ast.Node", "type Node interface{End() go/token.Pos; Pos() go/token.Pos}"},
37 {"go/types.Type", "type Type interface{String() string; Underlying() Type}"},
40 func TestImportedTypes(t *testing.T) {
41 skipSpecialPlatforms(t)
43 // This package only handles gc export data.
44 if runtime.Compiler != "gc" {
45 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
48 for _, test := range importedObjectTests {
49 s := strings.Split(test.name, ".")
51 t.Fatal("inconsistent test data")
56 pkg, err := Import(make(map[string]*types.Package), importPath, ".", nil)
62 obj := pkg.Scope().Lookup(objName)
64 t.Errorf("%s: object not found", test.name)
68 got := types.ObjectString(obj, types.RelativeTo(pkg))
70 t.Errorf("%s: got %q; want %q", test.name, got, test.want)
73 if named, _ := obj.Type().(*types.Named); named != nil {
74 verifyInterfaceMethodRecvs(t, named, 0)
79 // verifyInterfaceMethodRecvs verifies that method receiver types
80 // are named if the methods belong to a named interface type.
81 func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
82 // avoid endless recursion in case of an embedding bug that lead to a cycle
84 t.Errorf("%s: embeds itself", named)
88 iface, _ := named.Underlying().(*types.Interface)
90 return // not an interface
93 // check explicitly declared methods
94 for i := 0; i < iface.NumExplicitMethods(); i++ {
95 m := iface.ExplicitMethod(i)
96 recv := m.Type().(*types.Signature).Recv()
98 t.Errorf("%s: missing receiver type", m)
101 if recv.Type() != named {
102 t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
106 // check embedded interfaces (if they are named, too)
107 for i := 0; i < iface.NumEmbeddeds(); i++ {
108 // embedding of interfaces cannot have cycles; recursion will terminate
109 if etype, _ := iface.EmbeddedType(i).(*types.Named); etype != nil {
110 verifyInterfaceMethodRecvs(t, etype, level+1)
114 func TestIssue25301(t *testing.T) {
115 skipSpecialPlatforms(t)
117 // This package only handles gc export data.
118 if runtime.Compiler != "gc" {
119 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
122 // On windows, we have to set the -D option for the compiler to avoid having a drive
123 // letter and an illegal ':' in the import path - just skip it (see also issue #3483).
124 if runtime.GOOS == "windows" {
125 t.Skip("avoid dealing with relative paths/drive letters on windows")
128 compileAndImportPkg(t, "issue25301")