Debian Codegraph @

gopls/internal/cache/typerefs/refs_test.go

raw symbols SCIP
1 // Copyright 2023 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 typerefs_test
6
7 import (
8 "context"
9 "fmt"
10 "go/token"
11 "sort"
12 "testing"
13
14 "github.com/google/go-cmp/cmp"
15 "golang.org/x/tools/gopls/internal/cache/metadata"
16 "golang.org/x/tools/gopls/internal/cache/parsego"
17 "golang.org/x/tools/gopls/internal/cache/typerefs"
18 "golang.org/x/tools/gopls/internal/protocol"
19 )
20
21 // TestRefs checks that the analysis reports, for each exported member
22 // of the test package ("p"), its correct dependencies on exported
23 // members of its direct imports (e.g. "ext").
24 func TestRefs(t *testing.T) {
25 ctx := context.Background()
26
27 tests := []struct {
28 label string
29 srcs []string // source for the local package; package name must be p
30 imports map[string]string // for simplicity: importPath -> pkgID/pkgName (we set pkgName == pkgID); 'ext' is always available.
31 want map[string][]string // decl name -> id.<decl name>
32 allowErrs bool // whether we expect parsing errors
33 }{
34 {
35 label: "empty package",
36 want: map[string][]string{},
37 },
38 {
39 label: "fields",
40 srcs: []string{`
41 package p
42
43 import "ext"
44
45 type A struct{ b B }
46 type B func(c C) (d D)
47 type C ext.C
48 type D ext.D
49
50 // Should not be referenced by field names.
51 type b ext.B_
52 type c int.C_
53 type d ext.D_
54 `},
55 want: map[string][]string{
56 "A": {"ext.C", "ext.D"},
57 "B": {"ext.C", "ext.D"},
58 "C": {"ext.C"},
59 "D": {"ext.D"},
60 },
61 },
62 {
63 label: "embedding",
64 srcs: []string{`
65 package p
66
67 import "ext"
68
69 type A struct{
70 B
71 _ struct {
72 C
73 }
74 D
75 }
76 type B ext.B
77 type C ext.C
78 type D interface{
79 B
80 }
81 `},
82 want: map[string][]string{
83 "A": {"ext.B", "ext.C"},
84 "B": {"ext.B"},
85 "C": {"ext.C"},
86 "D": {"ext.B"},
87 },
88 },
89 {
90 label: "constraint embedding",
91 srcs: []string{`
92 package p
93
94 import "ext"
95
96 type A interface{
97 int | B | ~C
98 struct{D}
99 }
100
101 type B ext.B
102 type C ext.C
103 type D ext.D
104 `},
105 want: map[string][]string{
106 "A": {"ext.B", "ext.C", "ext.D"},
107 "B": {"ext.B"},
108 "C": {"ext.C"},
109 "D": {"ext.D"},
110 },
111 },
112 {
113 label: "funcs",
114 srcs: []string{`
115 package p
116
117 import "ext"
118
119 type A ext.A
120 type B ext.B
121 const C B = 2
122 func F(A) B {
123 return C
124 }
125 var V = F(W)
126 var W A
127 `},
128 want: map[string][]string{
129 "A": {"ext.A"},
130 "B": {"ext.B"},
131 "C": {"ext.B"},
132 "F": {"ext.A", "ext.B"},
133 "V": {
134 "ext.A", // via F
135 "ext.B", // via W: can't be eliminated: F could be builtin or generic
136 },
137 "W": {"ext.A"},
138 },
139 },
140 {
141 label: "methods",
142 srcs: []string{`package p
143
144 import "ext"
145
146 type A ext.A
147 type B ext.B
148 `, `package p
149
150 func (A) M(B)
151 func (*B) M(A)
152 `},
153 want: map[string][]string{
154 "A": {"ext.A", "ext.B"},
155 "B": {"ext.A", "ext.B"},
156 },
157 },
158 {
159 label: "initializers",
160 srcs: []string{`
161 package p
162
163 import "ext"
164
165 var A b = C // type does not depend on C
166 type b ext.B
167 var C = d // type does depend on D
168 var d b
169
170 var e = d + a
171
172 var F = func() B { return E }
173
174 var G = struct{
175 A b
176 _ [unsafe.Sizeof(ext.V)]int // array size + Sizeof creates edge to a var
177 _ [unsafe.Sizeof(G)]int // creates a self edge; doesn't affect output though
178 }{}
179
180 var H = (D + A + C*C)
181
182 var I = (A+C).F
183 `},
184 want: map[string][]string{
185 "A": {"ext.B"},
186 "C": {"ext.B"}, // via d
187 "G": {"ext.B", "ext.V"}, // via b,C
188 "H": {"ext.B"}, // via d,A,C
189 "I": {"ext.B"},
190 },
191 },
192 {
193 label: "builtins",
194 srcs: []string{`package p
195
196 import "ext"
197
198 var A = new(b)
199 type b struct{ ext.B }
200
201 type C chan d
202 type d ext.D
203
204 type S []ext.S
205 type t ext.T
206 var U = append(([]*S)(nil), new(t))
207
208 type X map[k]v
209 type k ext.K
210 type v ext.V
211
212 var Z = make(map[k]A)
213
214 // close, delete, and panic cannot occur outside of statements
215 `},
216 want: map[string][]string{
217 "A": {"ext.B"},
218 "C": {"ext.D"},
219 "S": {"ext.S"},
220 "U": {"ext.S", "ext.T"}, // ext.T edge could be eliminated
221 "X": {"ext.K", "ext.V"},
222 "Z": {"ext.B", "ext.K"},
223 },
224 },
225 {
226 label: "builtin shadowing",
227 srcs: []string{`package p
228
229 import "ext"
230
231 var A = new(ext.B)
232 func new() c
233 type c ext.C
234 `},
235 want: map[string][]string{
236 "A": {"ext.B", "ext.C"},
237 },
238 },
239 {
240 label: "named forwarding",
241 srcs: []string{`package p
242
243 import "ext"
244
245 type A B
246 type B c
247 type c ext.C
248 `},
249 want: map[string][]string{
250 "A": {"ext.C"},
251 "B": {"ext.C"},
252 },
253 },
254 {
255 label: "aliases",
256 srcs: []string{`package p
257
258 import "ext"
259
260 type A = B
261 type B = C
262 type C = ext.C
263 `},
264 want: map[string][]string{
265 "A": {"ext.C"},
266 "B": {"ext.C"},
267 "C": {"ext.C"},
268 },
269 },
270 {
271 label: "array length",
272 srcs: []string{`package p
273
274 import "ext"
275 import "unsafe"
276
277 type A [unsafe.Sizeof(ext.B{ext.C})]int
278 type A2 [unsafe.Sizeof(ext.B{f:ext.C})]int // use a KeyValueExpr
279
280 type D [unsafe.Sizeof(struct{ f E })]int
281 type E ext.E
282
283 type F [3]G
284 type G [ext.C]int
285 `},
286 want: map[string][]string{
287 "A": {"ext.B"}, // no ext.C: doesn't enter CompLit
288 "A2": {"ext.B"}, // ditto
289 "D": {"ext.E"},
290 "E": {"ext.E"},
291 "F": {"ext.C"},
292 "G": {"ext.C"},
293 },
294 },
295 {
296 label: "imports",
297 srcs: []string{`package p
298
299 import "ext"
300
301 import (
302 "q"
303 r2 "r"
304 "s" // note: package name is t
305 "z"
306 )
307
308 type A struct {
309 q.Q
310 r2.R
311 s.S // invalid ref
312 z.Z // references both external z.Z as well as package-level type z
313 }
314
315 type B struct {
316 r.R // invalid ref
317 t.T
318 }
319
320 var X int = q.V // X={}: no descent into RHS of 'var v T = rhs'
321 var Y = q.V.W
322
323 type z ext.Z
324 `},
325 imports: map[string]string{"q": "q", "r": "r", "s": "t", "z": "z"},
326 want: map[string][]string{
327 "A": {"ext.Z", "q.Q", "r.R", "z.Z"},
328 "B": {"t.T"},
329 "Y": {"q.V"},
330 },
331 },
332 {
333 label: "import blank",
334 srcs: []string{`package p
335
336 import _ "q"
337
338 type A q.Q
339 `},
340 imports: map[string]string{"q": "q"},
341 want: map[string][]string{},
342 },
343 {
344 label: "import dot",
345 srcs: []string{`package p
346
347 import . "q"
348
349 type A q.Q // not actually an edge, since q is imported .
350 type B struct {
351 C // assumed to be an edge to q
352 D // resolved to package decl
353 }
354
355
356 type E error // unexported, therefore must be universe.error
357 type F Field
358 var G = Field.X
359 `, `package p
360
361 import "ext"
362 import "q"
363
364 type D ext.D
365 `},
366 imports: map[string]string{"q": "q"},
367 want: map[string][]string{
368 "B": {"ext.D", "q.C"},
369 "D": {"ext.D"},
370 "F": {"q.Field"},
371 "G": {"q.Field"},
372 },
373 },
374 {
375 label: "typeparams",
376 srcs: []string{`package p
377
378 import "ext"
379
380 type A[T any] struct {
381 t T
382 b B
383 }
384
385 type B ext.B
386
387 func F1[T any](T, B)
388 func F2[T C]()(T, B)
389
390 type T ext.T
391
392 type C ext.C
393
394 func F3[T1 ~[]T2, T2 ~[]T3](t1 T1, t2 T2)
395 type T3 ext.T3
396 `, `package p
397
398 func (A[B]) M(C) {}
399 `},
400 want: map[string][]string{
401 "A": {"ext.B", "ext.C"},
402 "B": {"ext.B"},
403 "C": {"ext.C"},
404 "F1": {"ext.B"},
405 "F2": {"ext.B", "ext.C"},
406 "F3": {"ext.T3"},
407 "T": {"ext.T"},
408 "T3": {"ext.T3"},
409 },
410 },
411 {
412 label: "instances",
413 srcs: []string{`package p
414
415 import "ext"
416
417 type A[T any] ext.A
418 type B[T1, T2 any] ext.B
419
420 type C A[int]
421 type D B[int, A[E]]
422 type E ext.E
423 `},
424 want: map[string][]string{
425 "A": {"ext.A"},
426 "B": {"ext.B"},
427 "C": {"ext.A"},
428 "D": {"ext.A", "ext.B", "ext.E"},
429 "E": {"ext.E"},
430 },
431 },
432 {
433 label: "duplicate decls",
434 srcs: []string{`package p
435
436 import "a"
437 import "ext"
438
439 type a a.A
440 type A a
441 type b ext.B
442 type C a.A
443 func (C) Foo(x) {} // invalid parameter, but that does not matter
444 type C b
445 func (C) Bar(y) {} // invalid parameter, but that does not matter
446
447 var x ext.X
448 var y ext.Y
449 `},
450 imports: map[string]string{"a": "a", "b": "b"}, // "b" import should not matter, since it isn't in this file
451 want: map[string][]string{
452 "A": {"a.A"},
453 "C": {"a.A", "ext.B", "ext.X", "ext.Y"},
454 },
455 },
456 {
457 label: "invalid decls",
458 srcs: []string{`package p
459
460 import "ext"
461
462 type A B
463
464 func () Foo(B){}
465
466 var B struct{ ext.B
467 `},
468 want: map[string][]string{
469 "A": {"ext.B"},
470 "B": {"ext.B"},
471 "Foo": {"ext.B"},
472 },
473 allowErrs: true,
474 },
475 {
476 label: "unmapped receiver",
477 srcs: []string{`package p
478
479 type P struct{}
480
481 func (a) x(P)
482 `},
483 want: map[string][]string{},
484 allowErrs: true,
485 },
486 {
487 label: "SCC special case",
488 srcs: []string{`package p
489
490 import "ext"
491
492 type X Y
493 type Y struct { Z; *X }
494 type Z map[ext.A]ext.B
495 `},
496 want: map[string][]string{
497 "X": {"ext.A", "ext.B"},
498 "Y": {"ext.A", "ext.B"},
499 "Z": {"ext.A", "ext.B"},
500 },
501 allowErrs: true,
502 },
503 }
504
505 for _, test := range tests {
506 t.Run(test.label, func(t *testing.T) {
507 var pgfs []*parsego.File
508 for i, src := range test.srcs {
509 uri := protocol.DocumentURI(fmt.Sprintf("file:///%d.go", i))
510 pgf, _ := parsego.Parse(ctx, token.NewFileSet(), uri, []byte(src), parsego.Full, false)
511 if !test.allowErrs && pgf.ParseErr != nil {
512 t.Fatalf("ParseGoSrc(...) returned parse errors: %v", pgf.ParseErr)
513 }
514 pgfs = append(pgfs, pgf)
515 }
516
517 imports := map[metadata.ImportPath]*metadata.Package{
518 "ext": {ID: "ext", Name: "ext"}, // this one comes for free
519 }
520 for path, mp := range test.imports {
521 imports[metadata.ImportPath(path)] = &metadata.Package{
522 ID: metadata.PackageID(mp),
523 Name: metadata.PackageName(mp),
524 }
525 }
526
527 data := typerefs.Encode(pgfs, imports)
528
529 got := make(map[string][]string)
530 index := typerefs.NewPackageIndex()
531 for _, class := range typerefs.Decode(index, data) {
532 // We redundantly expand out the name x refs cross product
533 // here since that's what the existing tests expect.
534 for _, name := range class.Decls {
535 var syms []string
536 for _, sym := range class.Refs {
537 syms = append(syms, fmt.Sprintf("%s.%s", index.DeclaringPackage(sym), sym.Name))
538 }
539 sort.Strings(syms)
540 got[name] = syms
541 }
542 }
543
544 if diff := cmp.Diff(test.want, got); diff != "" {
545 t.Errorf("Refs(...) returned unexpected refs (-want +got):\n%s", diff)
546 }
547 })
548 }
549 }