| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package fingerprint_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/types" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/tools/go/packages" |
| 12 |
"golang.org/x/tools/go/types/typeutil" |
| 13 |
"golang.org/x/tools/gopls/internal/util/fingerprint" |
| 14 |
"golang.org/x/tools/internal/testenv" |
| 15 |
"golang.org/x/tools/internal/testfiles" |
| 16 |
"golang.org/x/tools/txtar" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
func Test(t *testing.T) { |
| 23 |
if testing.Short() { |
| 24 |
t.Skip("skipping slow test") |
| 25 |
} |
| 26 |
|
| 27 |
cfg := &packages.Config{Mode: packages.NeedTypes} |
| 28 |
pkgs, err := packages.Load(cfg, "std", "golang.org/x/tools/gopls/...") |
| 29 |
if err != nil { |
| 30 |
t.Fatal(err) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
var fingerprints typeutil.Map |
| 37 |
|
| 38 |
for _, pkg := range pkgs { |
| 39 |
switch pkg.Types.Path() { |
| 40 |
case "unsafe", "builtin": |
| 41 |
continue |
| 42 |
} |
| 43 |
scope := pkg.Types.Scope() |
| 44 |
for _, name := range scope.Names() { |
| 45 |
obj := scope.Lookup(name) |
| 46 |
typ := obj.Type() |
| 47 |
|
| 48 |
if basic, ok := typ.(*types.Basic); ok && |
| 49 |
basic.Info()&types.IsUntyped != 0 { |
| 50 |
continue |
| 51 |
} |
| 52 |
|
| 53 |
fp, tricky := fingerprint.Encode(typ) |
| 54 |
|
| 55 |
|
| 56 |
if !tricky { |
| 57 |
if prevfp, ok := fingerprints.At(typ).(string); !ok { |
| 58 |
fingerprints.Set(typ, fp) |
| 59 |
} else if fp != prevfp { |
| 60 |
t.Errorf("inconsistent fingerprints for type %v:\n- old: %s\n- new: %s", |
| 61 |
typ, fp, prevfp) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
tree := fingerprint.Parse(fp) |
| 66 |
fp2 := tree.String() |
| 67 |
|
| 68 |
|
| 69 |
if fp != fp2 { |
| 70 |
t.Errorf("%s: %v: parse+print changed fingerprint:\n"+ |
| 71 |
"was: %s\ngot: %s\ntype: %v", |
| 72 |
pkg.Fset.Position(obj.Pos()), obj, fp, fp2, typ) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func TestMatches(t *testing.T) { |
| 80 |
const src = ` |
| 81 |
-- go.mod -- |
| 82 |
module example.com |
| 83 |
go 1.24 |
| 84 |
|
| 85 |
-- a/a.go -- |
| 86 |
package a |
| 87 |
|
| 88 |
type Int = int |
| 89 |
type String = string |
| 90 |
|
| 91 |
// Eq.Equal matches casefold.Equal. |
| 92 |
type Eq[T any] interface { Equal(T, T) bool } |
| 93 |
type casefold struct{} |
| 94 |
func (casefold) Equal(x, y string) bool |
| 95 |
|
| 96 |
// A matches AString. |
| 97 |
type A[T any] = struct { x T } |
| 98 |
type AString = struct { x string } |
| 99 |
|
| 100 |
func C1[T any](int, T, ...string) T { panic(0) } |
| 101 |
func C2[U any](int, int, ...U) bool { panic(0) } |
| 102 |
func C3(int, bool, ...string) rune |
| 103 |
func C4(int, bool, ...string) |
| 104 |
func C5(int, float64, bool, string) bool |
| 105 |
func C6(int, bool, ...string) bool |
| 106 |
|
| 107 |
func DAny[T any](Named[T]) { panic(0) } |
| 108 |
func DString(Named[string]) |
| 109 |
func DInt(Named[int]) |
| 110 |
|
| 111 |
type Named[T any] struct { x T } |
| 112 |
|
| 113 |
func E1(byte) rune |
| 114 |
func E2(uint8) int32 |
| 115 |
func E3(int8) uint32 |
| 116 |
|
| 117 |
// generic vs. generic |
| 118 |
func F1[T any](T) { panic(0) } |
| 119 |
func F2[T any](*T) { panic(0) } |
| 120 |
func F3[T any](T, T) { panic(0) } |
| 121 |
func F4[U any](U, *U) { panic(0) } |
| 122 |
func F5[T, U any](T, U, U) { panic(0) } |
| 123 |
func F6[T any](T, int, T) { panic(0) } |
| 124 |
func F7[T any](bool, T, T) { panic(0) } |
| 125 |
func F8[V any](*V, int, int) { panic(0) } |
| 126 |
func F9[V any](V, *V, V) { panic(0) } |
| 127 |
` |
| 128 |
pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0] |
| 129 |
scope := pkg.Types.Scope() |
| 130 |
for _, test := range []struct { |
| 131 |
a, b string |
| 132 |
method string |
| 133 |
want bool |
| 134 |
}{ |
| 135 |
{"Eq", "casefold", "Equal", true}, |
| 136 |
{"A", "AString", "", true}, |
| 137 |
{"A", "Eq", "", false}, |
| 138 |
{"C1", "C2", "", false}, |
| 139 |
{"C1", "C3", "", false}, |
| 140 |
{"C1", "C4", "", false}, |
| 141 |
{"C1", "C5", "", false}, |
| 142 |
{"C1", "C6", "", true}, |
| 143 |
{"C2", "C3", "", false}, |
| 144 |
{"C2", "C4", "", false}, |
| 145 |
{"C3", "C4", "", false}, |
| 146 |
{"DAny", "DString", "", true}, |
| 147 |
{"DAny", "DInt", "", true}, |
| 148 |
{"DString", "DInt", "", false}, |
| 149 |
{"E1", "E2", "", true}, |
| 150 |
{"E2", "E3", "", false}, |
| 151 |
|
| 152 |
{"F1", "F2", "", true}, |
| 153 |
{"F3", "F4", "", false}, |
| 154 |
{"F5", "F6", "", true}, |
| 155 |
{"F6", "F7", "", false}, |
| 156 |
{"F5", "F8", "", true}, |
| 157 |
{"F5", "F9", "", false}, |
| 158 |
} { |
| 159 |
lookup := func(name string) types.Type { |
| 160 |
obj := scope.Lookup(name) |
| 161 |
if obj == nil { |
| 162 |
t.Fatalf("Lookup %s failed", name) |
| 163 |
} |
| 164 |
if test.method != "" { |
| 165 |
obj, _, _ = types.LookupFieldOrMethod(obj.Type(), true, pkg.Types, test.method) |
| 166 |
if obj == nil { |
| 167 |
t.Fatalf("Lookup %s.%s failed", name, test.method) |
| 168 |
} |
| 169 |
} |
| 170 |
return obj.Type() |
| 171 |
} |
| 172 |
|
| 173 |
check := func(sa, sb string, want bool) { |
| 174 |
t.Helper() |
| 175 |
|
| 176 |
a := lookup(sa) |
| 177 |
b := lookup(sb) |
| 178 |
|
| 179 |
afp, _ := fingerprint.Encode(a) |
| 180 |
bfp, _ := fingerprint.Encode(b) |
| 181 |
|
| 182 |
atree := fingerprint.Parse(afp) |
| 183 |
btree := fingerprint.Parse(bfp) |
| 184 |
|
| 185 |
got := fingerprint.Matches(atree, btree) |
| 186 |
if got != want { |
| 187 |
t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", |
| 188 |
sa, sb, test.method, got, a, b) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
check(test.a, test.b, test.want) |
| 193 |
|
| 194 |
check(test.b, test.a, test.want) |
| 195 |
|
| 196 |
check(test.a, test.a, true) |
| 197 |
check(test.b, test.b, true) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func TestMatches_genericMethods(t *testing.T) { |
| 207 |
testenv.NeedsGo1Point(t, 27) |
| 208 |
|
| 209 |
const src = ` |
| 210 |
-- go.mod -- |
| 211 |
module example.com |
| 212 |
go 1.27 |
| 213 |
|
| 214 |
-- a/a.go -- |
| 215 |
package a |
| 216 |
|
| 217 |
type I interface{ F(int) } |
| 218 |
|
| 219 |
type C struct{} |
| 220 |
func (C) F[T any](T) {} |
| 221 |
` |
| 222 |
pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0] |
| 223 |
scope := pkg.Types.Scope() |
| 224 |
for _, test := range []struct { |
| 225 |
a, b string |
| 226 |
method string |
| 227 |
want bool |
| 228 |
}{ |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
{"I", "C", "F", true}, |
| 233 |
} { |
| 234 |
lookup := func(name string) types.Type { |
| 235 |
obj := scope.Lookup(name) |
| 236 |
if obj == nil { |
| 237 |
t.Fatalf("Lookup %s failed", name) |
| 238 |
} |
| 239 |
if test.method != "" { |
| 240 |
obj, _, _ = types.LookupFieldOrMethod(obj.Type(), true, pkg.Types, test.method) |
| 241 |
if obj == nil { |
| 242 |
t.Fatalf("Lookup %s.%s failed", name, test.method) |
| 243 |
} |
| 244 |
} |
| 245 |
return obj.Type() |
| 246 |
} |
| 247 |
|
| 248 |
check := func(sa, sb string, want bool) { |
| 249 |
t.Helper() |
| 250 |
|
| 251 |
a := lookup(sa) |
| 252 |
b := lookup(sb) |
| 253 |
|
| 254 |
afp, _ := fingerprint.Encode(a) |
| 255 |
bfp, _ := fingerprint.Encode(b) |
| 256 |
|
| 257 |
atree := fingerprint.Parse(afp) |
| 258 |
btree := fingerprint.Parse(bfp) |
| 259 |
|
| 260 |
got := fingerprint.Matches(atree, btree) |
| 261 |
if got != want { |
| 262 |
t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", |
| 263 |
sa, sb, test.method, got, a, b) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
check(test.a, test.b, test.want) |
| 268 |
|
| 269 |
check(test.b, test.a, test.want) |
| 270 |
|
| 271 |
check(test.a, test.a, true) |
| 272 |
check(test.b, test.b, true) |
| 273 |
} |
| 274 |
} |