Debian Codegraph @

go/types/typeutil/callee_test.go

raw symbols SCIP
1 // Copyright 2018 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 typeutil_test
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/parser"
11 "go/token"
12 "go/types"
13 "strings"
14 "testing"
15
16 "golang.org/x/tools/go/types/typeutil"
17 )
18
19 func TestStaticCallee(t *testing.T) {
20 testStaticCallee(t, []string{
21 `package q;
22 func Abs(x int) int {
23 if x < 0 {
24 return -x
25 }
26 return x
27 }`,
28 `package p
29 import "q"
30
31 type T int
32
33 func g(int)
34
35 var f = g
36
37 var x int
38
39 type s struct{ f func(int) }
40 func (s) g(int)
41
42 type I interface{ f(int) }
43
44 var a struct{b struct{c s}}
45
46 var n map[int]func()
47 var m []func()
48
49 func calls() {
50 g(x) // a declared func
51 s{}.g(x) // a concrete method
52 a.b.c.g(x) // same
53 _ = q.Abs(x) // declared func, qualified identifier
54 }
55
56 func noncalls() {
57 _ = T(x) // a type
58 f(x) // a var
59 panic(x) // a built-in
60 s{}.f(x) // a field
61 I(nil).f(x) // interface method
62 m[0]() // a map
63 n[0]() // a slice
64 }
65 `})
66 }
67
68 func TestTypeParamStaticCallee(t *testing.T) {
69 testStaticCallee(t, []string{
70 `package q
71 func R[T any]() {}
72 `,
73 `package p
74 import "q"
75 type I interface{
76 i()
77 }
78
79 type G[T any] func() T
80 func F[T any]() T { var x T; return x }
81
82 type M[T I] struct{ t T }
83 func (m M[T]) noncalls() {
84 m.t.i() // method on a type parameter
85 }
86
87 func (m M[T]) calls() {
88 m.calls() // method on a generic type
89 }
90
91 type Chain[T I] struct{ r struct { s M[T] } }
92
93 type S int
94 func (S) i() {}
95
96 func Multi[TP0, TP1 any](){}
97
98 func calls() {
99 _ = F[int]() // instantiated function
100 _ = (F[int])() // go through parens
101 M[S]{}.calls() // instantiated method
102 Chain[S]{}.r.s.calls() // same as above
103 Multi[int,string]() // multiple type parameters
104 q.R[int]() // different package
105 }
106
107 func noncalls() {
108 _ = G[int](nil)() // instantiated function
109 }
110 `})
111 }
112
113 // testStaticCallee parses and type checks each file content in contents
114 // as a single file package in order. Within functions that have the suffix
115 // "calls" it checks that the CallExprs within have a static callee.
116 // If the function's name == "calls" all calls must have static callees,
117 // and if the name != "calls", the calls must not have static callees.
118 // Failures are reported on t.
119 func testStaticCallee(t *testing.T, contents []string) {
120 fset := token.NewFileSet()
121 packages := make(map[string]*types.Package)
122 cfg := &types.Config{Importer: closure(packages)}
123 info := &types.Info{
124 Instances: make(map[*ast.Ident]types.Instance),
125 Types: make(map[ast.Expr]types.TypeAndValue),
126 Uses: make(map[*ast.Ident]types.Object),
127 Selections: make(map[*ast.SelectorExpr]*types.Selection),
128 FileVersions: make(map[*ast.File]string),
129 }
130
131 var files []*ast.File
132 for i, content := range contents {
133 // parse
134 f, err := parser.ParseFile(fset, fmt.Sprintf("%d.go", i), content, 0)
135 if err != nil {
136 t.Fatal(err)
137 }
138 files = append(files, f)
139
140 // type-check
141 pkg, err := cfg.Check(f.Name.Name, fset, []*ast.File{f}, info)
142 if err != nil {
143 t.Fatal(err)
144 }
145 packages[pkg.Path()] = pkg
146 }
147
148 // check
149 for _, f := range files {
150 for _, decl := range f.Decls {
151 if decl, ok := decl.(*ast.FuncDecl); ok && strings.HasSuffix(decl.Name.Name, "calls") {
152 wantCallee := decl.Name.Name == "calls" // false within func noncalls()
153 ast.Inspect(decl.Body, func(n ast.Node) bool {
154 if call, ok := n.(*ast.CallExpr); ok {
155 fn := typeutil.StaticCallee(info, call)
156 if fn == nil && wantCallee {
157 t.Errorf("%s: StaticCallee returned nil",
158 fset.Position(call.Lparen))
159 } else if fn != nil && !wantCallee {
160 t.Errorf("%s: StaticCallee returned %s, want nil",
161 fset.Position(call.Lparen), fn)
162 }
163 }
164 return true
165 })
166 }
167 }
168 }
169 }