| 1 |
// Copyright 2016 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 |
package types_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"internal/testenv" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
. "go/types" |
| 14 |
) |
| 15 |
|
| 16 |
func TestIsAlias(t *testing.T) { |
| 17 |
check := func(obj *TypeName, want bool) { |
| 18 |
if got := obj.IsAlias(); got != want { |
| 19 |
t.Errorf("%v: got IsAlias = %v; want %v", obj, got, want) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// predeclared types |
| 24 |
check(Unsafe.Scope().Lookup("Pointer").(*TypeName), false) |
| 25 |
for _, name := range Universe.Names() { |
| 26 |
if obj, _ := Universe.Lookup(name).(*TypeName); obj != nil { |
| 27 |
check(obj, name == "any" || name == "byte" || name == "rune") |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// various other types |
| 32 |
pkg := NewPackage("p", "p") |
| 33 |
t1 := NewTypeName(nopos, pkg, "t1", nil) |
| 34 |
n1 := NewNamed(t1, new(Struct), nil) |
| 35 |
t5 := NewTypeName(nopos, pkg, "t5", nil) |
| 36 |
NewTypeParam(t5, nil) |
| 37 |
for _, test := range []struct { |
| 38 |
name *TypeName |
| 39 |
alias bool |
| 40 |
}{ |
| 41 |
{NewTypeName(nopos, nil, "t0", nil), false}, // no type yet |
| 42 |
{NewTypeName(nopos, pkg, "t0", nil), false}, // no type yet |
| 43 |
{t1, false}, // type name refers to named type and vice versa |
| 44 |
{NewTypeName(nopos, nil, "t2", NewInterfaceType(nil, nil)), true}, // type name refers to unnamed type |
| 45 |
{NewTypeName(nopos, pkg, "t3", n1), true}, // type name refers to named type with different type name |
| 46 |
{NewTypeName(nopos, nil, "t4", Typ[Int32]), true}, // type name refers to basic type with different name |
| 47 |
{NewTypeName(nopos, nil, "int32", Typ[Int32]), false}, // type name refers to basic type with same name |
| 48 |
{NewTypeName(nopos, pkg, "int32", Typ[Int32]), true}, // type name is declared in user-defined package (outside Universe) |
| 49 |
{NewTypeName(nopos, nil, "rune", Typ[Rune]), true}, // type name refers to basic type rune which is an alias already |
| 50 |
{t5, false}, // type name refers to type parameter and vice versa |
| 51 |
} { |
| 52 |
check(test.name, test.alias) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// TestEmbeddedMethod checks that an embedded method is represented by |
| 57 |
// the same Func Object as the original method. See also go.dev/issue/34421. |
| 58 |
func TestEmbeddedMethod(t *testing.T) { |
| 59 |
const src = `package p; type I interface { error }` |
| 60 |
pkg := mustTypecheck(src, nil, nil) |
| 61 |
|
| 62 |
// get original error.Error method |
| 63 |
eface := Universe.Lookup("error") |
| 64 |
orig, _, _ := LookupFieldOrMethod(eface.Type(), false, nil, "Error") |
| 65 |
if orig == nil { |
| 66 |
t.Fatalf("original error.Error not found") |
| 67 |
} |
| 68 |
|
| 69 |
// get embedded error.Error method |
| 70 |
iface := pkg.Scope().Lookup("I") |
| 71 |
embed, _, _ := LookupFieldOrMethod(iface.Type(), false, nil, "Error") |
| 72 |
if embed == nil { |
| 73 |
t.Fatalf("embedded error.Error not found") |
| 74 |
} |
| 75 |
|
| 76 |
// original and embedded Error object should be identical |
| 77 |
if orig != embed { |
| 78 |
t.Fatalf("%s (%p) != %s (%p)", orig, orig, embed, embed) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
var testObjects = []struct { |
| 83 |
src string |
| 84 |
obj string |
| 85 |
want string |
| 86 |
alias bool // needs materialized (and possibly generic) aliases |
| 87 |
}{ |
| 88 |
{"import \"io\"; var r io.Reader", "r", "var p.r io.Reader", false}, |
| 89 |
|
| 90 |
{"const c = 1.2", "c", "const p.c untyped float", false}, |
| 91 |
{"const c float64 = 3.14", "c", "const p.c float64", false}, |
| 92 |
|
| 93 |
{"type t struct{f int}", "t", "type p.t struct{f int}", false}, |
| 94 |
{"type t func(int)", "t", "type p.t func(int)", false}, |
| 95 |
{"type t[P any] struct{f P}", "t", "type p.t[P any] struct{f P}", false}, |
| 96 |
{"type t[P any] struct{f P}", "t.P", "type parameter P any", false}, |
| 97 |
{"type C interface{m()}; type t[P C] struct{}", "t.P", "type parameter P p.C", false}, |
| 98 |
|
| 99 |
{"type t = struct{f int}", "t", "type p.t = struct{f int}", false}, |
| 100 |
{"type t = func(int)", "t", "type p.t = func(int)", false}, |
| 101 |
{"type A = B; type B = int", "A", "type p.A = p.B", true}, |
| 102 |
{"type A[P ~int] = struct{}", "A", "type p.A[P ~int] = struct{}", true}, |
| 103 |
{"var v int", "v", "var p.v int", false}, |
| 104 |
|
| 105 |
{"func f(int) string", "f", "func p.f(int) string", false}, |
| 106 |
{"func g[P any](x P){}", "g", "func p.g[P any](x P)", false}, |
| 107 |
{"func g[P interface{~int}](x P){}", "g.P", "type parameter P interface{~int}", false}, |
| 108 |
{"", "any", "type any = interface{}", false}, |
| 109 |
} |
| 110 |
|
| 111 |
func TestObjectString(t *testing.T) { |
| 112 |
testenv.MustHaveGoBuild(t) |
| 113 |
|
| 114 |
for i, test := range testObjects { |
| 115 |
t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 116 |
if test.alias { |
| 117 |
t.Setenv("GODEBUG", "gotypesalias=1") |
| 118 |
} |
| 119 |
|
| 120 |
src := "package p; " + test.src |
| 121 |
pkg, err := typecheck(src, nil, nil) |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("%s: %s", src, err) |
| 124 |
} |
| 125 |
|
| 126 |
names := strings.Split(test.obj, ".") |
| 127 |
if len(names) != 1 && len(names) != 2 { |
| 128 |
t.Fatalf("%s: invalid object path %s", test.src, test.obj) |
| 129 |
} |
| 130 |
|
| 131 |
var obj Object |
| 132 |
for s := pkg.Scope(); s != nil && obj == nil; s = s.Parent() { |
| 133 |
obj = s.Lookup(names[0]) |
| 134 |
} |
| 135 |
if obj == nil { |
| 136 |
t.Fatalf("%s: %s not found", test.src, names[0]) |
| 137 |
} |
| 138 |
|
| 139 |
if len(names) == 2 { |
| 140 |
if typ, ok := obj.Type().(interface{ TypeParams() *TypeParamList }); ok { |
| 141 |
obj = lookupTypeParamObj(typ.TypeParams(), names[1]) |
| 142 |
if obj == nil { |
| 143 |
t.Fatalf("%s: %s not found", test.src, test.obj) |
| 144 |
} |
| 145 |
} else { |
| 146 |
t.Fatalf("%s: %s has no type parameters", test.src, names[0]) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if got := obj.String(); got != test.want { |
| 151 |
t.Errorf("%s: got %s, want %s", test.src, got, test.want) |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func lookupTypeParamObj(list *TypeParamList, name string) Object { |
| 158 |
for i := 0; i < list.Len(); i++ { |
| 159 |
tpar := list.At(i) |
| 160 |
if tpar.Obj().Name() == name { |
| 161 |
return tpar.Obj() |
| 162 |
} |
| 163 |
} |
| 164 |
return nil |
| 165 |
} |