Debian Codegraph @

gopls/internal/golang/implementation_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 golang
6
7 import (
8 "go/types"
9 "maps"
10 "testing"
11
12 "golang.org/x/tools/internal/testfiles"
13 "golang.org/x/tools/txtar"
14 )
15
16 func TestUnify(t *testing.T) {
17 // Most cases from TestMatches in gopls/internal/util/fingerprint/fingerprint_test.go.
18 const src = `
19 -- go.mod --
20 module example.com
21 go 1.24
22
23 -- a/a.go --
24 package a
25
26 type Int = int
27 type String = string
28
29 // Eq.Equal matches casefold.Equal.
30 type Eq[T any] interface { Equal(T, T) bool }
31 type casefold struct{}
32 func (casefold) Equal(x, y string) bool
33
34 // A matches AString.
35 type A[T any] = struct { x T }
36 type AString = struct { x string }
37
38 func C1[T any](int, T, ...string) T { panic(0) }
39 func C2[U any](int, int, ...U) bool { panic(0) }
40 func C3(int, bool, ...string) rune
41 func C4(int, bool, ...string)
42 func C5(int, float64, bool, string) bool
43 func C6(int, bool, ...string) bool
44
45 func DAny[T any](Named[T]) { panic(0) }
46 func DString(Named[string])
47 func DInt(Named[int])
48
49 type Named[T any] struct { x T }
50
51 func E1(byte) rune
52 func E2(uint8) int32
53 func E3(int8) uint32
54
55 // generic vs. generic
56 func F1[T any](T) { panic(0) }
57 func F2[T any](*T) { panic(0) }
58 func F3[T any](T, T) { panic(0) }
59 func F4[U any](U, *U) {panic(0) }
60 func F4a[U any](U, Named[U]) {panic(0) }
61 func F5[T, U any](T, U, U) { panic(0) }
62 func F6[T any](T, int, T) { panic(0) }
63 func F7[T any](bool, T, T) { panic(0) }
64 func F8[V any](*V, int, int) { panic(0) }
65 func F9[V any](V, *V, V) { panic(0) }
66 `
67 type tmap = map[*types.TypeParam]types.Type
68
69 var (
70 boolType = types.Typ[types.Bool]
71 intType = types.Typ[types.Int]
72 stringType = types.Typ[types.String]
73 )
74
75 pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0]
76 scope := pkg.Types.Scope()
77
78 tparam := func(name string, index int) *types.TypeParam {
79 obj := scope.Lookup(name)
80 var tps *types.TypeParamList
81 switch obj := obj.(type) {
82 case *types.Func:
83 tps = obj.Signature().TypeParams()
84 case *types.TypeName:
85 if n, ok := obj.Type().(*types.Named); ok {
86 tps = n.TypeParams()
87 } else {
88 tps = obj.Type().(*types.Alias).TypeParams()
89 }
90 default:
91 t.Fatalf("unsupported object of type %T", obj)
92 }
93 return tps.At(index)
94 }
95
96 for _, test := range []struct {
97 x, y string // the symbols in the above source code whose types to unify
98 method string // optional field or method
99 params tmap // initial values of type params
100 want bool // success or failure
101 wantParams tmap // expected output
102 }{
103 {
104 // In Eq[T], T is bound to string.
105 x: "Eq",
106 y: "casefold",
107 method: "Equal",
108 want: true,
109 wantParams: tmap{tparam("Eq", 0): stringType},
110 },
111 {
112 // If we unify A[T] and A[string], T should be bound to string.
113 x: "A",
114 y: "AString",
115 want: true,
116 wantParams: tmap{tparam("A", 0): stringType},
117 },
118 {x: "A", y: "Eq", want: false}, // completely unrelated
119 {
120 // C1's U unifies with C6's bool.
121 x: "C1",
122 y: "C6",
123 wantParams: tmap{tparam("C1", 0): boolType},
124 want: true,
125 },
126 // C1 fails to unify with C2 because C1's T must be bound to both int and bool.
127 {x: "C1", y: "C2", want: false},
128 // The remaining "C" cases fail for less interesting reasons, usually different numbers
129 // or types of parameters or results.
130 {x: "C1", y: "C3", want: false},
131 {x: "C1", y: "C4", want: false},
132 {x: "C1", y: "C5", want: false},
133 {x: "C2", y: "C3", want: false},
134 {x: "C2", y: "C4", want: false},
135 {x: "C3", y: "C4", want: false},
136 {
137 x: "DAny",
138 y: "DString",
139 want: true,
140 wantParams: tmap{tparam("DAny", 0): stringType},
141 },
142 {x: "DString", y: "DInt", want: false}, // different instantiations of Named
143 {x: "E1", y: "E2", want: true}, // byte and rune are just aliases
144 {x: "E2", y: "E3", want: false},
145
146 // The following tests cover all of the type param cases of unify.
147 {
148 // F1[*int] = F2[int], for example
149 // F1's T is bound to a pointer to F2's T.
150 x: "F1",
151 // F2's T is unbound: any instantiation works.
152 y: "F2",
153 want: true,
154 wantParams: tmap{tparam("F1", 0): types.NewPointer(tparam("F2", 0))},
155 },
156 {x: "F3", y: "F4", want: false}, // would require U identical to *U, prevented by occur check
157 {x: "F3", y: "F4a", want: false}, // occur check through Named[T]
158 {
159 x: "F5",
160 y: "F6",
161 want: true,
162 wantParams: tmap{
163 tparam("F5", 0): intType,
164 tparam("F5", 1): intType,
165 tparam("F6", 0): intType,
166 },
167 },
168 {x: "F6", y: "F7", want: false}, // both are bound
169 {
170 x: "F5",
171 y: "F6",
172 params: tmap{tparam("F6", 0): intType}, // consistent with the result
173 want: true,
174 wantParams: tmap{
175 tparam("F5", 0): intType,
176 tparam("F5", 1): intType,
177 tparam("F6", 0): intType,
178 },
179 },
180 {
181 x: "F5",
182 y: "F6",
183 params: tmap{tparam("F6", 0): boolType}, // not consistent
184 want: false,
185 },
186 {x: "F6", y: "F7", want: false}, // both are bound
187 {
188 // T=*V, U=int, V=int
189 x: "F5",
190 y: "F8",
191 want: true,
192 wantParams: tmap{
193 tparam("F5", 0): types.NewPointer(tparam("F8", 0)),
194 tparam("F5", 1): intType,
195 },
196 },
197 {
198 // T=*V, U=int, V=int
199 // Partial initial information is fine, as long as it's consistent.
200 x: "F5",
201 y: "F8",
202 want: true,
203 params: tmap{tparam("F5", 1): intType},
204 wantParams: tmap{
205 tparam("F5", 0): types.NewPointer(tparam("F8", 0)),
206 tparam("F5", 1): intType,
207 },
208 },
209 {
210 // T=*V, U=int, V=int
211 // Partial initial information is fine, as long as it's consistent.
212 x: "F5",
213 y: "F8",
214 want: true,
215 params: tmap{tparam("F5", 0): types.NewPointer(tparam("F8", 0))},
216 wantParams: tmap{
217 tparam("F5", 0): types.NewPointer(tparam("F8", 0)),
218 tparam("F5", 1): intType,
219 },
220 },
221 {x: "F5", y: "F9", want: false}, // T is unbound, V is bound, and T occurs in V
222 {
223 // T bound to Named[T']
224 x: "F1",
225 y: "DAny",
226 want: true,
227 wantParams: tmap{
228 tparam("F1", 0): scope.Lookup("DAny").(*types.Func).Signature().Params().At(0).Type()},
229 },
230 } {
231
232 lookup := func(name string) types.Type {
233 obj := scope.Lookup(name)
234 if obj == nil {
235 t.Fatalf("Lookup %s failed", name)
236 }
237 if test.method != "" {
238 obj, _, _ = types.LookupFieldOrMethod(obj.Type(), true, pkg.Types, test.method)
239 if obj == nil {
240 t.Fatalf("Lookup %s.%s failed", name, test.method)
241 }
242 }
243 return obj.Type()
244 }
245
246 check := func(a, b string, want, compareParams bool) {
247 t.Helper()
248
249 ta := lookup(a)
250 tb := lookup(b)
251
252 var gotParams tmap
253 if test.params == nil {
254 // Get the unifier even if there are no input params.
255 gotParams = tmap{}
256 } else {
257 gotParams = maps.Clone(test.params)
258 }
259 got := unify(ta, tb, gotParams)
260 if got != want {
261 t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s",
262 a, b, test.method, got, ta, tb)
263 return
264 }
265 if !compareParams {
266 return
267 }
268 if !maps.EqualFunc(gotParams, test.wantParams, types.Identical) {
269 t.Errorf("x=%s y=%s method=%s: params: got %v, want %v",
270 a, b, test.method, gotParams, test.wantParams)
271 }
272 }
273
274 check(test.x, test.y, test.want, true)
275 // unify is symmetric
276 check(test.y, test.x, test.want, true)
277 // unify is reflexive
278 check(test.x, test.x, true, false)
279 check(test.y, test.y, true, false)
280 }
281 }