Debian Codegraph @

gopls/internal/util/fingerprint/fingerprint_test.go

raw symbols SCIP
1 // Copyright 2025 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 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 // Test runs the fingerprint encoder, decoder, and printer
20 // on the types of all package-level symbols in gopls, and ensures
21 // that parse+print is lossless.
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 // Record the fingerprint of each logical type (equivalence
34 // class of types.Types) and assert that they are all equal.
35 // (Non-tricky types only.)
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 // untyped constant
51 }
52
53 fp, tricky := fingerprint.Encode(typ) // check Type encoder doesn't panic
54
55 // All equivalent (non-tricky) types have the same fingerprint.
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) // check parser doesn't panic
66 fp2 := tree.String() // check formatter doesn't pannic
67
68 // A parse+print round-trip should be lossless.
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 // TestMatches exercises the matching algorithm for generic types.
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 // optional field or method
133 want bool
134 }{
135 {"Eq", "casefold", "Equal", true},
136 {"A", "AString", "", true},
137 {"A", "Eq", "", false}, // completely unrelated
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}, // different instantiations of Named
149 {"E1", "E2", "", true}, // byte and rune are just aliases
150 {"E2", "E3", "", false},
151 // The following tests cover all of the type param cases of unify.
152 {"F1", "F2", "", true}, // F1[*int] = F2[int]
153 {"F3", "F4", "", false}, // would require U identical to *U, prevented by occur check
154 {"F5", "F6", "", true}, // one param is bound, the other is not
155 {"F6", "F7", "", false}, // both are bound
156 {"F5", "F8", "", true}, // T=*int, U=int, V=int
157 {"F5", "F9", "", false}, // T is unbound, V is bound, and T occurs in V
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 // Matches is symmetric
194 check(test.b, test.a, test.want)
195 // Matches is reflexive
196 check(test.a, test.a, true)
197 check(test.b, test.b, true)
198 }
199 }
200
201 // Generic methods were added in go1.27 but cannot satisfy
202 // interfaces, so their fingerprints aren't really important,
203 // but we test them nonetheless.
204 //
205 // TODO(adonovan): merge into test above once go1.27 is assured.
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 // optional field or method
227 want bool
228 }{
229 // C.F does match (unify with) I.F.
230 // However, C does not satisfy I since
231 // only non-generic methods are relevant.
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 // Matches is symmetric
269 check(test.b, test.a, test.want)
270 // Matches is reflexive
271 check(test.a, test.a, true)
272 check(test.b, test.b, true)
273 }
274 }