| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package typeparams_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"testing" |
| 13 |
|
| 14 |
. "golang.org/x/exp/typeparams" |
| 15 |
) |
| 16 |
|
| 17 |
func TestGetIndexExprData(t *testing.T) { |
| 18 |
x := &ast.Ident{} |
| 19 |
i := &ast.Ident{} |
| 20 |
|
| 21 |
want := &IndexListExpr{X: x, Lbrack: 1, Indices: []ast.Expr{i}, Rbrack: 2} |
| 22 |
tests := map[ast.Node]bool{ |
| 23 |
&ast.IndexExpr{X: x, Lbrack: 1, Index: i, Rbrack: 2}: true, |
| 24 |
want: true, |
| 25 |
&ast.Ident{}: false, |
| 26 |
} |
| 27 |
|
| 28 |
for n, isIndexExpr := range tests { |
| 29 |
X, lbrack, indices, rbrack := UnpackIndexExpr(n) |
| 30 |
if got := X != nil; got != isIndexExpr { |
| 31 |
t.Errorf("UnpackIndexExpr(%v) = %v, _, _, _; want nil: %t", n, x, !isIndexExpr) |
| 32 |
} |
| 33 |
if X == nil { |
| 34 |
continue |
| 35 |
} |
| 36 |
if X != x || lbrack != 1 || indices[0] != i || rbrack != 2 { |
| 37 |
t.Errorf("UnpackIndexExprData(%v) = %v, %v, %v, %v; want %+v", n, x, lbrack, indices, rbrack, want) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestOriginMethodRecursive(t *testing.T) { |
| 43 |
src := `package p |
| 44 |
|
| 45 |
type N[A any] int |
| 46 |
|
| 47 |
func (r N[B]) m() { r.m(); r.n() } |
| 48 |
|
| 49 |
func (r *N[C]) n() { } |
| 50 |
` |
| 51 |
fset := token.NewFileSet() |
| 52 |
f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 53 |
if err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
info := types.Info{ |
| 57 |
Defs: make(map[*ast.Ident]types.Object), |
| 58 |
Uses: make(map[*ast.Ident]types.Object), |
| 59 |
} |
| 60 |
var conf types.Config |
| 61 |
if _, err := conf.Check("p", fset, []*ast.File{f}, &info); err != nil { |
| 62 |
t.Fatal(err) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
var m, n *types.Func |
| 67 |
var mm, mn *types.Func |
| 68 |
|
| 69 |
for _, decl := range f.Decls { |
| 70 |
fdecl, ok := decl.(*ast.FuncDecl) |
| 71 |
if !ok { |
| 72 |
continue |
| 73 |
} |
| 74 |
def := info.Defs[fdecl.Name].(*types.Func) |
| 75 |
switch fdecl.Name.Name { |
| 76 |
case "m": |
| 77 |
m = def |
| 78 |
ast.Inspect(fdecl.Body, func(n ast.Node) bool { |
| 79 |
if call, ok := n.(*ast.CallExpr); ok { |
| 80 |
sel := call.Fun.(*ast.SelectorExpr) |
| 81 |
use := info.Uses[sel.Sel].(*types.Func) |
| 82 |
switch sel.Sel.Name { |
| 83 |
case "m": |
| 84 |
mm = use |
| 85 |
case "n": |
| 86 |
mn = use |
| 87 |
} |
| 88 |
} |
| 89 |
return true |
| 90 |
}) |
| 91 |
case "n": |
| 92 |
n = def |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
tests := []struct { |
| 97 |
name string |
| 98 |
input, want *types.Func |
| 99 |
}{ |
| 100 |
{"declared m", m, m}, |
| 101 |
{"declared n", n, n}, |
| 102 |
{"used m", mm, m}, |
| 103 |
{"used n", mn, n}, |
| 104 |
} |
| 105 |
|
| 106 |
for _, test := range tests { |
| 107 |
if got := OriginMethod(test.input); got != test.want { |
| 108 |
t.Errorf("OriginMethod(%q) = %v, want %v", test.name, test.input, test.want) |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestOriginMethodUses(t *testing.T) { |
| 114 |
tests := []string{ |
| 115 |
`type T interface { m() }; func _(t T) { t.m() }`, |
| 116 |
`type T[P any] interface { m() P }; func _[A any](t T[A]) { t.m() }`, |
| 117 |
`type T[P any] interface { m() P }; func _(t T[int]) { t.m() }`, |
| 118 |
`type T[P any] int; func (r T[A]) m() { r.m() }`, |
| 119 |
`type T[P any] int; func (r *T[A]) m() { r.m() }`, |
| 120 |
`type T[P any] int; func (r *T[A]) m() {}; func _(t T[int]) { t.m() }`, |
| 121 |
`type T[P any] int; func (r *T[A]) m() {}; func _[A any](t T[A]) { t.m() }`, |
| 122 |
} |
| 123 |
|
| 124 |
for _, src := range tests { |
| 125 |
fset := token.NewFileSet() |
| 126 |
f, err := parser.ParseFile(fset, "p.go", "package p; "+src, 0) |
| 127 |
if err != nil { |
| 128 |
t.Fatal(err) |
| 129 |
} |
| 130 |
info := types.Info{ |
| 131 |
Uses: make(map[*ast.Ident]types.Object), |
| 132 |
} |
| 133 |
var conf types.Config |
| 134 |
pkg, err := conf.Check("p", fset, []*ast.File{f}, &info) |
| 135 |
if err != nil { |
| 136 |
t.Fatal(err) |
| 137 |
} |
| 138 |
|
| 139 |
T := pkg.Scope().Lookup("T").Type() |
| 140 |
obj, _, _ := types.LookupFieldOrMethod(T, true, pkg, "m") |
| 141 |
m := obj.(*types.Func) |
| 142 |
|
| 143 |
ast.Inspect(f, func(n ast.Node) bool { |
| 144 |
if call, ok := n.(*ast.CallExpr); ok { |
| 145 |
sel := call.Fun.(*ast.SelectorExpr) |
| 146 |
use := info.Uses[sel.Sel].(*types.Func) |
| 147 |
orig := OriginMethod(use) |
| 148 |
if orig != m { |
| 149 |
t.Errorf("%s:\nUses[%v] = %v, want %v", src, types.ExprString(sel), use, m) |
| 150 |
} |
| 151 |
} |
| 152 |
return true |
| 153 |
}) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestGenericAssignableTo(t *testing.T) { |
| 158 |
tests := []struct { |
| 159 |
src string |
| 160 |
want bool |
| 161 |
}{ |
| 162 |
|
| 163 |
{` |
| 164 |
type T[P any] interface { |
| 165 |
Accept(P) |
| 166 |
} |
| 167 |
|
| 168 |
type V[Q any] struct { |
| 169 |
Element Q |
| 170 |
} |
| 171 |
|
| 172 |
func (c V[Q]) Accept(q Q) { c.Element = q } |
| 173 |
`, true}, |
| 174 |
|
| 175 |
|
| 176 |
{`type T[P ~int] interface{ A(P) }; type V[Q int] int; func (V[Q]) A(Q) {}`, true}, |
| 177 |
{`type T[P int] interface{ A(P) }; type V[Q ~int] int; func (V[Q]) A(Q) {}`, false}, |
| 178 |
{`type T[P int|string] interface{ A(P) }; type V[Q int] int; func (V[Q]) A(Q) {}`, true}, |
| 179 |
{`type T[P any] interface{ A(P) }; type V[Q any] int; func (V[Q]) A(Q, Q) {}`, false}, |
| 180 |
{`type T[P any] interface{ int; A(P) }; type V[Q any] int; func (V[Q]) A(Q) {}`, false}, |
| 181 |
|
| 182 |
|
| 183 |
{`type T[P any] interface{ ~int; A(P) }; type V[Q any] int; func (V[Q]) A(Q) {}`, true}, |
| 184 |
{`type T[P any] interface{ ~int|string; A(P) }; type V[Q any] int; func (V[Q]) A(Q) {}`, true}, |
| 185 |
{`type T[P any] interface{ int; A(P) }; type V[Q int] int; func (V[Q]) A(Q) {}`, false}, |
| 186 |
|
| 187 |
|
| 188 |
{`type T[P ~struct{ f *P }] interface{ A(P) }; type V[Q ~struct{ f *Q }] int; func (V[Q]) A(Q) {}`, true}, |
| 189 |
{`type T[P ~struct{ f *P }] interface{ A(P) }; type V[Q ~struct{ g *Q }] int; func (V[Q]) A(Q) {}`, false}, |
| 190 |
{`type T[P ~*X, X any] interface{ A(P) X }; type V[Q ~*Y, Y any] int; func (V[Q, Y]) A(Q) (y Y) { return }`, true}, |
| 191 |
{`type T[P ~*X, X any] interface{ A(P) X }; type V[Q ~**Y, Y any] int; func (V[Q, Y]) A(Q) (y Y) { return }`, false}, |
| 192 |
{`type T[P, X any] interface{ A(P) X }; type V[Q ~*Y, Y any] int; func (V[Q, Y]) A(Q) (y Y) { return }`, true}, |
| 193 |
{`type T[P ~*X, X any] interface{ A(P) X }; type V[Q, Y any] int; func (V[Q, Y]) A(Q) (y Y) { return }`, false}, |
| 194 |
{`type T[P, X any] interface{ A(P) X }; type V[Q, Y any] int; func (V[Q, Y]) A(Q) (y Y) { return }`, true}, |
| 195 |
|
| 196 |
|
| 197 |
{`type T[P, X any] interface{ A(P) X }; type V[Q, Y any] int; func (V[Q, Y]) A(Y) (y Q) { return }`, false}, |
| 198 |
|
| 199 |
|
| 200 |
{`type T[P, X any] interface{ A(P) X }; type V[Q, Y int] int; func (V[Q, Y]) A(Y) (y Q) { return }`, false}, |
| 201 |
} |
| 202 |
|
| 203 |
for _, test := range tests { |
| 204 |
fset := token.NewFileSet() |
| 205 |
f, err := parser.ParseFile(fset, "p.go", "package p; "+test.src, 0) |
| 206 |
if err != nil { |
| 207 |
t.Fatalf("%s:\n%v", test.src, err) |
| 208 |
} |
| 209 |
var conf types.Config |
| 210 |
pkg, err := conf.Check("p", fset, []*ast.File{f}, nil) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("%s:\n%v", test.src, err) |
| 213 |
} |
| 214 |
|
| 215 |
V := pkg.Scope().Lookup("V").Type() |
| 216 |
T := pkg.Scope().Lookup("T").Type() |
| 217 |
|
| 218 |
if types.AssignableTo(V, T) { |
| 219 |
t.Fatal("AssignableTo") |
| 220 |
} |
| 221 |
|
| 222 |
if got := GenericAssignableTo(nil, V, T); got != test.want { |
| 223 |
t.Fatalf("%s:\nGenericAssignableTo(%v, %v) = %v, want %v", test.src, V, T, got, test.want) |
| 224 |
} |
| 225 |
} |
| 226 |
} |