| 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 typesinternal_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"go/ast" |
| 11 |
"go/format" |
| 12 |
"go/importer" |
| 13 |
"go/parser" |
| 14 |
"go/token" |
| 15 |
"go/types" |
| 16 |
"testing" |
| 17 |
|
| 18 |
ti "honnef.co/go/tools/internal/xtools-internal/typesinternal" |
| 19 |
) |
| 20 |
|
| 21 |
func TestClassifyCallAndUsed(t *testing.T) { |
| 22 |
const src = ` |
| 23 |
package p |
| 24 |
|
| 25 |
func g(int) |
| 26 |
|
| 27 |
type A[T any] *T |
| 28 |
|
| 29 |
func F[T any](T) {} |
| 30 |
|
| 31 |
type S struct{ f func(int) } |
| 32 |
func (S) g(int) |
| 33 |
|
| 34 |
type I interface{ m(int) } |
| 35 |
|
| 36 |
var ( |
| 37 |
z S |
| 38 |
a struct{b struct{c S}} |
| 39 |
f = g |
| 40 |
m map[int]func() |
| 41 |
n []func() |
| 42 |
p *int |
| 43 |
) |
| 44 |
|
| 45 |
func tests[T int]() { |
| 46 |
var zt T |
| 47 |
|
| 48 |
g(1) |
| 49 |
f(1) |
| 50 |
println() |
| 51 |
z.g(1) // a concrete method |
| 52 |
a.b.c.g(1) // same |
| 53 |
S.g(z, 1) // method expression |
| 54 |
z.f(1) // struct field |
| 55 |
I(nil).m(1) // interface method, then type conversion (preorder traversal) |
| 56 |
m[0]() // a map |
| 57 |
n[0]() // a slice |
| 58 |
F[int](1) // instantiated function |
| 59 |
F[T](zt) // generic function |
| 60 |
func() {}() // function literal |
| 61 |
_=[]byte("") // type expression |
| 62 |
_=A[int](p) // instantiated type |
| 63 |
_=T(1) // type param |
| 64 |
// parenthesized forms |
| 65 |
(z.g)(1) |
| 66 |
(z).g(1) |
| 67 |
|
| 68 |
|
| 69 |
// A[T](1) // generic type: illegal |
| 70 |
} |
| 71 |
` |
| 72 |
|
| 73 |
fset := token.NewFileSet() |
| 74 |
cfg := &types.Config{ |
| 75 |
Error: func(err error) { t.Fatal(err) }, |
| 76 |
Importer: importer.Default(), |
| 77 |
} |
| 78 |
info := ti.NewTypesInfo() |
| 79 |
// parse |
| 80 |
f, err := parser.ParseFile(fset, "classify.go", src, 0) |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
|
| 85 |
// type-check |
| 86 |
pkg, err := cfg.Check(f.Name.Name, fset, []*ast.File{f}, info) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
|
| 91 |
lookup := func(sym string) types.Object { |
| 92 |
return pkg.Scope().Lookup(sym) |
| 93 |
} |
| 94 |
|
| 95 |
member := func(sym, fieldOrMethod string) types.Object { |
| 96 |
obj, _, _ := types.LookupFieldOrMethod(lookup(sym).Type(), false, pkg, fieldOrMethod) |
| 97 |
return obj |
| 98 |
} |
| 99 |
|
| 100 |
printlnObj := types.Universe.Lookup("println") |
| 101 |
|
| 102 |
typeParam := lookup("tests").Type().(*types.Signature).TypeParams().At(0).Obj() |
| 103 |
|
| 104 |
// Expected Calls are in the order of CallExprs at the end of src, above. |
| 105 |
wants := []struct { |
| 106 |
kind ti.CallKind |
| 107 |
usedObj types.Object // the object obtained from the result of UsedIdent |
| 108 |
}{ |
| 109 |
{ti.CallStatic, lookup("g")}, // g |
| 110 |
{ti.CallDynamic, lookup("f")}, // f |
| 111 |
{ti.CallBuiltin, printlnObj}, // println |
| 112 |
{ti.CallStatic, member("S", "g")}, // z.g |
| 113 |
{ti.CallStatic, member("S", "g")}, // a.b.c.g |
| 114 |
{ti.CallStatic, member("S", "g")}, // S.g(z, 1) |
| 115 |
{ti.CallDynamic, member("z", "f")}, // z.f |
| 116 |
{ti.CallInterface, member("I", "m")}, // I(nil).m |
| 117 |
{ti.CallConversion, lookup("I")}, // I(nil) |
| 118 |
{ti.CallDynamic, nil}, // m[0] |
| 119 |
{ti.CallDynamic, nil}, // n[0] |
| 120 |
{ti.CallStatic, lookup("F")}, // F[int] |
| 121 |
{ti.CallStatic, lookup("F")}, // F[T] |
| 122 |
{ti.CallDynamic, nil}, // f(){} |
| 123 |
{ti.CallConversion, nil}, // []byte |
| 124 |
{ti.CallConversion, lookup("A")}, // A[int] |
| 125 |
{ti.CallConversion, typeParam}, // T |
| 126 |
{ti.CallStatic, member("S", "g")}, // (z.g) |
| 127 |
{ti.CallStatic, member("S", "g")}, // (z).g |
| 128 |
} |
| 129 |
|
| 130 |
i := 0 |
| 131 |
ast.Inspect(f, func(n ast.Node) bool { |
| 132 |
if call, ok := n.(*ast.CallExpr); ok { |
| 133 |
if i >= len(wants) { |
| 134 |
t.Fatal("more calls than wants") |
| 135 |
} |
| 136 |
var buf bytes.Buffer |
| 137 |
if err := format.Node(&buf, fset, n); err != nil { |
| 138 |
t.Fatal(err) |
| 139 |
} |
| 140 |
prefix := fmt.Sprintf("%s (#%d)", buf.String(), i) |
| 141 |
|
| 142 |
gotKind := ti.ClassifyCall(info, call) |
| 143 |
want := wants[i] |
| 144 |
|
| 145 |
if gotKind != want.kind { |
| 146 |
t.Errorf("%s kind: got %s, want %s", prefix, gotKind, want.kind) |
| 147 |
} |
| 148 |
|
| 149 |
w := want.usedObj |
| 150 |
if g := info.Uses[ti.UsedIdent(info, call.Fun)]; g != w { |
| 151 |
t.Errorf("%s used obj: got %v (%[2]T), want %v", prefix, g, w) |
| 152 |
} |
| 153 |
i++ |
| 154 |
} |
| 155 |
return true |
| 156 |
}) |
| 157 |
if i != len(wants) { |
| 158 |
t.Fatal("more wants than calls") |
| 159 |
} |
| 160 |
} |