| 1 | // Copyright 2021 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 typeparams_test |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/parser" |
| 10 | "go/token" |
| 11 | "go/types" |
| 12 | "testing" |
| 13 | |
| 14 | . "golang.org/x/tools/internal/typeparams" |
| 15 | ) |
| 16 | |
| 17 | func TestGetIndexExprData(t *testing.T) { |
| 18 | x := &ast.Ident{} |
| 19 | i := &ast.Ident{} |
| 20 | |
| 21 | want := &ast.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 TestFuncOriginRecursive(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 | // Collect objects from types.Info. |
| 66 | var m, n *types.Func // the 'origin' methods in Info.Defs |
| 67 | var mm, mn *types.Func // the methods used in the body of m |
| 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 := test.input.Origin(); got != test.want { |
| 108 | t.Errorf("Origin(%q) = %v, want %v", test.name, test.input, test.want) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestFuncOriginUses(t *testing.T) { |
| 114 | |
| 115 | tests := []string{ |
| 116 | `type T interface { m() }; func _(t T) { t.m() }`, |
| 117 | `type T[P any] interface { m() P }; func _[A any](t T[A]) { t.m() }`, |
| 118 | `type T[P any] interface { m() P }; func _(t T[int]) { t.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() { r.m() }`, |
| 121 | `type T[P any] int; func (r *T[A]) m() {}; func _(t T[int]) { t.m() }`, |
| 122 | `type T[P any] int; func (r *T[A]) m() {}; func _[A any](t T[A]) { t.m() }`, |
| 123 | } |
| 124 | |
| 125 | for _, src := range tests { |
| 126 | fset := token.NewFileSet() |
| 127 | f, err := parser.ParseFile(fset, "p.go", "package p; "+src, 0) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | info := types.Info{ |
| 132 | Uses: make(map[*ast.Ident]types.Object), |
| 133 | } |
| 134 | var conf types.Config |
| 135 | pkg, err := conf.Check("p", fset, []*ast.File{f}, &info) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | |
| 140 | // Look up func T.m. |
| 141 | T := pkg.Scope().Lookup("T").Type() |
| 142 | obj, _, _ := types.LookupFieldOrMethod(T, true, pkg, "m") |
| 143 | m := obj.(*types.Func) |
| 144 | |
| 145 | // Assert that the origin of each t.m() call is p.T.m. |
| 146 | ast.Inspect(f, func(n ast.Node) bool { |
| 147 | if call, ok := n.(*ast.CallExpr); ok { |
| 148 | sel := call.Fun.(*ast.SelectorExpr) |
| 149 | use := info.Uses[sel.Sel].(*types.Func) |
| 150 | orig := use.Origin() |
| 151 | if orig != m { |
| 152 | t.Errorf("%s:\nUses[%v] = %v, want %v", src, types.ExprString(sel), use, m) |
| 153 | } |
| 154 | } |
| 155 | return true |
| 156 | }) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Issue #60628 was a crash in gopls caused by inconsistency (#60634) between |
| 161 | // LookupFieldOrMethod and NewFileSet for methods with an illegal |
| 162 | // *T receiver type, where T itself is a pointer. |
| 163 | // This is a regression test for the workaround in the (now deleted) OriginMethod. |
| 164 | func TestFuncOrigin60628(t *testing.T) { |
| 165 | const src = `package p; type T[P any] *int; func (r *T[A]) f() {}` |
| 166 | fset := token.NewFileSet() |
| 167 | f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 168 | if err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | |
| 172 | // Expect type error: "invalid receiver type T[A] (pointer or interface type)". |
| 173 | info := types.Info{ |
| 174 | Uses: make(map[*ast.Ident]types.Object), |
| 175 | } |
| 176 | var conf types.Config |
| 177 | pkg, _ := conf.Check("p", fset, []*ast.File{f}, &info) // error expected |
| 178 | if pkg == nil { |
| 179 | t.Fatal("no package") |
| 180 | } |
| 181 | |
| 182 | // Look up methodset of *T. |
| 183 | T := pkg.Scope().Lookup("T").Type() |
| 184 | mset := types.NewMethodSet(types.NewPointer(T)) |
| 185 | if mset.Len() == 0 { |
| 186 | t.Errorf("NewMethodSet(*T) is empty") |
| 187 | } |
| 188 | for sel := range mset.Methods() { |
| 189 | m := sel.Obj().(*types.Func) |
| 190 | |
| 191 | // TODO(adonovan): check the consistency property required to fix #60634. |
| 192 | if false { |
| 193 | m2, _, _ := types.LookupFieldOrMethod(T, true, m.Pkg(), m.Name()) |
| 194 | if m2 != m { |
| 195 | t.Errorf("LookupFieldOrMethod(%v, indirect=true, %v) = %v, want %v", |
| 196 | T, m, m2, m) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Check the workaround. |
| 201 | if m.Origin() == nil { |
| 202 | t.Errorf("Origin(%v) = nil", m) |
| 203 | } |
| 204 | } |
| 205 | } |