1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package typeparams_test
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"testing"
. "golang.org/x/tools/internal/typeparams"
)
func TestGetIndexExprData(t *testing.T) {
x := &ast.Ident{}
i := &ast.Ident{}
want := &ast.IndexListExpr{X: x, Lbrack: 1, Indices: []ast.Expr{i}, Rbrack: 2}
tests := map[ast.Node]bool{
&ast.IndexExpr{X: x, Lbrack: 1, Index: i, Rbrack: 2}: true,
want: true,
&ast.Ident{}: false,
}
for n, isIndexExpr := range tests {
X, lbrack, indices, rbrack := UnpackIndexExpr(n)
if got := X != nil; got != isIndexExpr {
t.Errorf("UnpackIndexExpr(%v) = %v, _, _, _; want nil: %t", n, x, !isIndexExpr)
}
if X == nil {
continue
}
if X != x || lbrack != 1 || indices[0] != i || rbrack != 2 {
t.Errorf("UnpackIndexExprData(%v) = %v, %v, %v, %v; want %+v", n, x, lbrack, indices, rbrack, want)
}
}
}
func TestFuncOriginRecursive(t *testing.T) {
src := `package p
type N[A any] int
func (r N[B]) m() { r.m(); r.n() }
func (r *N[C]) n() { }
`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p.go", src, 0)
if err != nil {
t.Fatal(err)
}
info := types.Info{
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
var conf types.Config
if _, err := conf.Check("p", fset, []*ast.File{f}, &info); err != nil {
t.Fatal(err)
}
// Collect objects from types.Info.
var m, n *types.Func // the 'origin' methods in Info.Defs
var mm, mn *types.Func // the methods used in the body of m
for _, decl := range f.Decls {
fdecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
def := info.Defs[fdecl.Name].(*types.Func)
switch fdecl.Name.Name {
case "m":
m = def
ast.Inspect(fdecl.Body, func(n ast.Node) bool {
if call, ok := n.(*ast.CallExpr); ok {
sel := call.Fun.(*ast.SelectorExpr)
use := info.Uses[sel.Sel].(*types.Func)
switch sel.Sel.Name {
case "m":
mm = use
case "n":
mn = use
}
}
return true
})
case "n":
n = def
}
}
tests := []struct {
name string
input, want *types.Func
}{
{"declared m", m, m},
{"declared n", n, n},
{"used m", mm, m},
{"used n", mn, n},
}
for _, test := range tests {
if got := test.input.Origin(); got != test.want {
t.Errorf("Origin(%q) = %v, want %v", test.name, test.input, test.want)
}
}
}
func TestFuncOriginUses(t *testing.T) {
tests := []string{
`type T interface { m() }; func _(t T) { t.m() }`,
`type T[P any] interface { m() P }; func _[A any](t T[A]) { t.m() }`,
`type T[P any] interface { m() P }; func _(t T[int]) { t.m() }`,
`type T[P any] int; func (r T[A]) m() { r.m() }`,
`type T[P any] int; func (r *T[A]) m() { r.m() }`,
`type T[P any] int; func (r *T[A]) m() {}; func _(t T[int]) { t.m() }`,
`type T[P any] int; func (r *T[A]) m() {}; func _[A any](t T[A]) { t.m() }`,
}
for _, src := range tests {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p.go", "package p; "+src, 0)
if err != nil {
t.Fatal(err)
}
info := types.Info{
Uses: make(map[*ast.Ident]types.Object),
}
var conf types.Config
pkg, err := conf.Check("p", fset, []*ast.File{f}, &info)
if err != nil {
t.Fatal(err)
}
// Look up func T.m.
T := pkg.Scope().Lookup("T").Type()
obj, _, _ := types.LookupFieldOrMethod(T, true, pkg, "m")
m := obj.(*types.Func)
// Assert that the origin of each t.m() call is p.T.m.
ast.Inspect(f, func(n ast.Node) bool {
if call, ok := n.(*ast.CallExpr); ok {
sel := call.Fun.(*ast.SelectorExpr)
use := info.Uses[sel.Sel].(*types.Func)
orig := use.Origin()
if orig != m {
t.Errorf("%s:\nUses[%v] = %v, want %v", src, types.ExprString(sel), use, m)
}
}
return true
})
}
}
// Issue #60628 was a crash in gopls caused by inconsistency (#60634) between
// LookupFieldOrMethod and NewFileSet for methods with an illegal
// *T receiver type, where T itself is a pointer.
// This is a regression test for the workaround in the (now deleted) OriginMethod.
func TestFuncOrigin60628(t *testing.T) {
const src = `package p; type T[P any] *int; func (r *T[A]) f() {}`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p.go", src, 0)
if err != nil {
t.Fatal(err)
}
// Expect type error: "invalid receiver type T[A] (pointer or interface type)".
info := types.Info{
Uses: make(map[*ast.Ident]types.Object),
}
var conf types.Config
pkg, _ := conf.Check("p", fset, []*ast.File{f}, &info) // error expected
if pkg == nil {
t.Fatal("no package")
}
// Look up methodset of *T.
T := pkg.Scope().Lookup("T").Type()
mset := types.NewMethodSet(types.NewPointer(T))
if mset.Len() == 0 {
t.Errorf("NewMethodSet(*T) is empty")
}
for i := 0; i < mset.Len(); i++ {
sel := mset.At(i)
m := sel.Obj().(*types.Func)
// TODO(adonovan): check the consistency property required to fix #60634.
if false {
m2, _, _ := types.LookupFieldOrMethod(T, true, m.Pkg(), m.Name())
if m2 != m {
t.Errorf("LookupFieldOrMethod(%v, indirect=true, %v) = %v, want %v",
T, m, m2, m)
}
}
// Check the workaround.
if m.Origin() == nil {
t.Errorf("Origin(%v) = nil", m)
}
}
}
func TestGenericAssignableTo(t *testing.T) {
tests := []struct {
src string
want bool
}{
// The inciting issue: golang/go#50887.
{`
type T[P any] interface {
Accept(P)
}
type V[Q any] struct {
Element Q
}
func (c V[Q]) Accept(q Q) { c.Element = q }
`, true},
// Various permutations on constraints and signatures.
{`type T[P ~int] interface{ A(P) }; type V[Q int] int; func (V[Q]) A(Q) {}`, true},
{`type T[P int] interface{ A(P) }; type V[Q ~int] int; func (V[Q]) A(Q) {}`, false},
{`type T[P int|string] interface{ A(P) }; type V[Q int] int; func (V[Q]) A(Q) {}`, true},
{`type T[P any] interface{ A(P) }; type V[Q any] int; func (V[Q]) A(Q, Q) {}`, false},
{`type T[P any] interface{ int; A(P) }; type V[Q any] int; func (V[Q]) A(Q) {}`, false},
// Various structural restrictions on T.
{`type T[P any] interface{ ~int; A(P) }; type V[Q any] int; func (V[Q]) A(Q) {}`, true},
{`type T[P any] interface{ ~int|string; A(P) }; type V[Q any] int; func (V[Q]) A(Q) {}`, true},
{`type T[P any] interface{ int; A(P) }; type V[Q int] int; func (V[Q]) A(Q) {}`, false},
// Various recursive constraints.
{`type T[P ~struct{ f *P }] interface{ A(P) }; type V[Q ~struct{ f *Q }] int; func (V[Q]) A(Q) {}`, true},
{`type T[P ~struct{ f *P }] interface{ A(P) }; type V[Q ~struct{ g *Q }] int; func (V[Q]) A(Q) {}`, false},
{`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},
{`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},
{`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},
{`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},
{`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},
// In this test case, we reverse the type parameters in the signature of V.A
{`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},
// It would be nice to return true here: V can only be instantiated with
// [int, int], so the identity of the type parameters should not matter.
{`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},
}
for _, test := range tests {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p.go", "package p; "+test.src, 0)
if err != nil {
t.Fatalf("%s:\n%v", test.src, err)
}
var conf types.Config
pkg, err := conf.Check("p", fset, []*ast.File{f}, nil)
if err != nil {
t.Fatalf("%s:\n%v", test.src, err)
}
V := pkg.Scope().Lookup("V").Type()
T := pkg.Scope().Lookup("T").Type()
if types.AssignableTo(V, T) {
t.Fatal("AssignableTo")
}
if got := GenericAssignableTo(nil, V, T); got != test.want {
t.Fatalf("%s:\nGenericAssignableTo(%v, %v) = %v, want %v", test.src, V, T, got, test.want)
}
}
}
|