Debian Codegraph @

go/types/objectpath/objectpath_go118_test.go

raw symbols SCIP
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 objectpath_test
6
7 import (
8 "go/types"
9 "testing"
10
11 "golang.org/x/tools/go/types/objectpath"
12 )
13
14 // TODO(adonovan): merge this back into objectpath_test.go.
15 func TestGenericPaths(t *testing.T) {
16 const src = `
17 -- go.mod --
18 module x.io
19 go 1.18
20
21 -- b/b.go --
22 package b
23
24 const C int = 1
25
26 type T[TP0 any, TP1 interface{ M0(); M1() }] struct{}
27
28 func (T[RP0, RP1]) M() {}
29
30 type N int
31
32 func (N) M0()
33 func (N) M1()
34
35 type A = T[int, N]
36
37 func F[FP0 any, FP1 interface{ M() }](FP0, FP1) {}
38 `
39
40 pkgmap := loadPackages(t, src, "./b")
41
42 paths := []pathTest{
43 // Good paths
44 {"b", "T", "type b.T[TP0 any, TP1 interface{M0(); M1()}] struct{}", ""},
45 {"b", "T.O", "type b.T[TP0 any, TP1 interface{M0(); M1()}] struct{}", ""},
46 {"b", "T.M0", "func (b.T[RP0, RP1]).M()", ""},
47 {"b", "T.M0.r1O", "type parameter RP1 interface{M0(); M1()}", ""},
48 {"b", "T.M0.r1CM1", "func (interface).M1()", ""},
49 {"b", "T.T0O", "type parameter TP0 any", ""},
50 {"b", "T.T1O", "type parameter TP1 interface{M0(); M1()}", ""},
51 {"b", "T.T1CM0", "func (interface).M0()", ""},
52 {"b", "F.T0O", "type parameter FP0 any", ""},
53 {"b", "F.T1CM0", "func (interface).M()", ""},
54 // Obj of an instance is the generic declaration.
55 {"b", "A.O", "type b.T[TP0 any, TP1 interface{M0(); M1()}] struct{}", ""},
56 {"b", "A.M0", "func (b.T[int, b.N]).M()", ""},
57
58 // Bad paths
59 {"b", "N.C", "", "invalid path: ends with 'C', want [AFMO]"},
60 {"b", "N.CO", "", "cannot apply 'C' to b.N (got *types.Named, want type parameter)"},
61 {"b", "N.T", "", `invalid path: bad numeric operand "" for code 'T'`},
62 {"b", "N.T0", "", "type parameter index 0 out of range [0-0)"},
63 {"b", "T.T2O", "", "type parameter index 2 out of range [0-2)"},
64 {"b", "T.T1M0", "", "cannot apply 'M' to TP1 (got *types.TypeParam, want interface or named)"},
65 {"b", "C.T0", "", "cannot apply 'T' to int (got *types.Basic, want named or signature)"},
66 }
67 for _, test := range paths {
68 if err := testPath(pkgmap, test); err != nil {
69 t.Error(err)
70 }
71 }
72
73 // bad objects
74 for _, test := range []struct {
75 obj types.Object
76 wantErr string
77 }{
78 {types.Universe.Lookup("any"), "predeclared type any = interface{} has no path"},
79 {types.Universe.Lookup("comparable"), "predeclared type comparable interface{comparable} has no path"},
80 } {
81 path, err := objectpath.For(test.obj)
82 if err == nil {
83 t.Errorf("Object(%s) = %q, want error", test.obj, path)
84 continue
85 }
86 if err.Error() != test.wantErr {
87 t.Errorf("Object(%s) error was %q, want %q", test.obj, err, test.wantErr)
88 continue
89 }
90 }
91 }
92
93 func TestGenericPaths_Issue51717(t *testing.T) {
94 const src = `
95 -- go.mod --
96 module x.io
97 go 1.18
98
99 -- p/p.go --
100 package p
101
102 type S struct{}
103
104 func (_ S) M() {
105 // The go vet stackoverflow crash disappears when the following line is removed
106 panic("")
107 }
108
109 func F[WL interface{ N(item W) WL }, W any]() {
110 }
111
112 func main() {}
113 `
114 pkgmap := loadPackages(t, src, "./p")
115
116 paths := []pathTest{
117 {"p", "F.T0CM0.RA0", "var WL", ""},
118 {"p", "F.T0CM0.RA0.CM0", "func (interface).N(item W) WL", ""},
119
120 // Finding S.M0 reproduced the infinite recursion reported in #51717,
121 // because F is searched before S.
122 {"p", "S.M0", "func (p.S).M()", ""},
123 }
124 for _, test := range paths {
125 if err := testPath(pkgmap, test); err != nil {
126 t.Error(err)
127 }
128 }
129 }