Debian Codegraph @

gopls/internal/test/marker/testdata/implementation/signature.txt

raw
1 Test of local Implementation queries using function signatures.
2
3 Assertions:
4 - Query on "func" of a function type returns the corresponding concrete functions.
5 - Query on "func" of a concrete function returns corresponding function types.
6 - Query on "(" of a dynamic function call returns corresponding function types.
7 - Different signatures (Nullary vs Handler) don't correspond.
8
9 The @loc markers use the suffixes Func, Type, Call for the three kinds.
10 Each query maps between these two sets: {Func} <=> {Type,Call}.
11
12 -- go.mod --
13 module example.com
14 go 1.18
15
16 -- a/a.go --
17 package a
18
19 // R is short for Record.
20 type R struct{}
21
22 // H is short for Handler.
23 type H func(*R) //@ loc(HType, "func"), implementation("func", aFunc, bFunc, cFunc)
24
25 func aFunc(*R) {} //@ loc(aFunc, "func"), implementation("func", HType, hParamType, hCall)
26
27 var bFunc = func(*R) {} //@ loc(bFunc, "func"), implementation("func", hParamType, hCall, HType)
28
29 func nullary() { //@ loc(nullaryFunc, "func"), implementation("func", Nullary, fieldCall)
30 cFunc := func(*R) {} //@ loc(cFunc, "func"), implementation("func", hParamType, hCall, HType)
31 _ = cFunc
32 }
33
34 type Nullary func() //@ loc(Nullary, "func")
35
36 func _(
37 h func(*R)) { //@ loc(hParamType, "func"), implementation("func", aFunc, bFunc, cFunc)
38
39 _ = aFunc // pacify unusedfunc
40 _ = bFunc // pacify unusedfunc
41 _ = nullary // pacify unusedfunc
42 _ = h
43
44 h(nil) //@ loc(hCall, "("), implementation("(", aFunc, bFunc, cFunc)
45 }
46
47 // generics:
48
49 func _[T any](complex128) {
50 f1 := func(T) int { return 0 } //@ loc(f1Func, "func"), implementation("func", fParamType, fCall, f1Call, f2Call)
51 f2 := func(string) int { return 0 } //@ loc(f2Func, "func"), implementation("func", fParamType, fCall, f1Call, f2Call)
52 f3 := func(int) int { return 0 } //@ loc(f3Func, "func"), implementation("func", f1Call)
53
54 f1(*new(T)) //@ loc(f1Call, "("), implementation("(", f1Func, f2Func, f3Func, f4Func)
55 f2("") //@ loc(f2Call, "("), implementation("(", f1Func, f2Func, f4Func)
56 _ = f3 // not called
57 }
58
59 func f4[T any](T) int { return 0 } //@ loc(f4Func, "func"), implementation("func", fParamType, fCall, f1Call, f2Call)
60
61 var _ = f4[string] // pacify unusedfunc
62
63 func _(
64 f func(string) int, //@ loc(fParamType, "func"), implementation("func", f1Func, f2Func, f4Func)
65 err error) {
66
67 f("") //@ loc(fCall, "("), implementation("(", f1Func, f2Func, f4Func)
68
69 struct{x Nullary}{}.x() //@ loc(fieldCall, "("), implementation("(", nullaryFunc)
70
71 // Calls that are not dynamic function calls:
72 _ = len("") //@ implementation("(", err="not a dynamic function call")
73 _ = int(0) //@ implementation("(", err="not a dynamic function call")
74 _ = error.Error(nil) //@ implementation("(", err="not a dynamic function call")
75 _ = err.Error() //@ implementation("(", err="not a dynamic function call")
76 _ = f4(0) //@ implementation("(", err="not a dynamic function call"), loc(f4Call, "(")
77 }
78
79
80