Skip to content

Commit 3c6bebf

Browse files
committed
go/doc: enable AllMethods flag (and fix logic)
- enable AllMethods flag (default: not set) - fix logic determining which methods to show - added respective test case in testdata/e.go for AllMethods = false - added test case set for AllMethods = true The critical changes/files to look at are: - testdata/e{0,1,2}.golden: T4.M should only show up as method of T5 in e2.golden - reader.go: always include top-level methods, and negate former logic for embedded methods (rewrote as a switch for better comprehensability) Fixes #2791. R=rsc, rsc CC=golang-dev https://golang.org/cl/5576057
1 parent c0ecfb0 commit 3c6bebf

14 files changed

+453
-12
lines changed

src/cmd/api/goapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (w *Walker) WalkPackage(name string) {
264264
// (functions and methods). This is done here because
265265
// go/doc is destructive. We can't use the
266266
// *ast.Package after this.
267-
dpkg := doc.New(apkg, name, 0)
267+
dpkg := doc.New(apkg, name, doc.AllMethods)
268268

269269
for _, t := range dpkg.Types {
270270
// Move funcs up to the top-level, not hiding in the Types.

src/pkg/go/doc/doc.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ const (
7878
// New takes ownership of the AST pkg and may edit or overwrite it.
7979
//
8080
func New(pkg *ast.Package, importPath string, mode Mode) *Package {
81-
mode |= AllMethods // TODO(gri) remove this to enable flag
8281
var r reader
8382
r.readPackage(pkg, mode)
8483
r.computeMethodSets()

src/pkg/go/doc/doc_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,5 @@ func test(t *testing.T, mode Mode) {
118118
func Test(t *testing.T) {
119119
test(t, 0)
120120
test(t, AllDecls)
121+
test(t, AllMethods)
121122
}

src/pkg/go/doc/exports.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ func (r *reader) filterDecl(decl ast.Decl) bool {
141141
d.Specs = r.filterSpecList(d.Specs)
142142
return len(d.Specs) > 0
143143
case *ast.FuncDecl:
144+
// ok to filter these methods early because any
145+
// conflicting method will be filtered here, too -
146+
// thus, removing these methods early will not lead
147+
// to the false removal of possible conflicts
144148
return ast.IsExported(d.Name.Name)
145149
}
146150
return false

src/pkg/go/doc/reader.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,13 @@ func sortedFuncs(m methodSet, allMethods bool) []*Func {
708708
list := make([]*Func, len(m))
709709
i := 0
710710
for _, m := range m {
711-
// exclude conflict entries
712-
if m.Decl != nil && (allMethods || ast.IsExported(removeStar(m.Orig))) {
711+
// determine which methods to include
712+
switch {
713+
case m.Decl == nil:
714+
// exclude conflict entry
715+
case allMethods, m.Level == 0, !ast.IsExported(removeStar(m.Orig)):
716+
// forced inclusion, method not embedded, or method
717+
// embedded but original receiver type not exported
713718
list[i] = m
714719
i++
715720
}

src/pkg/go/doc/testdata/a.2.golden

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// comment 0 comment 1
2+
PACKAGE a
3+
4+
IMPORTPATH
5+
testdata/a
6+
7+
FILENAMES
8+
testdata/a0.go
9+
testdata/a1.go
10+
11+
BUGS
12+
// bug0
13+
// bug1

src/pkg/go/doc/testdata/b.2.golden

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
PACKAGE b
3+
4+
IMPORTPATH
5+
testdata/b
6+
7+
IMPORTS
8+
a
9+
10+
FILENAMES
11+
testdata/b.go
12+
13+
CONSTANTS
14+
//
15+
const Pi = 3.14 // Pi
16+
17+
18+
VARIABLES
19+
//
20+
var MaxInt int // MaxInt
21+
22+
23+
FUNCTIONS
24+
//
25+
func F(x int) int
26+
27+
// Always under the package functions list.
28+
func NotAFactory() int
29+
30+
// Associated with uint type if AllDecls is set.
31+
func UintFactory() uint
32+
33+
34+
TYPES
35+
//
36+
type T struct{} // T
37+
38+
//
39+
var V T // v
40+
41+
//
42+
func (x *T) M()
43+

src/pkg/go/doc/testdata/c.2.golden

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
PACKAGE c
3+
4+
IMPORTPATH
5+
testdata/c
6+
7+
IMPORTS
8+
a
9+
10+
FILENAMES
11+
testdata/c.go
12+
13+
TYPES
14+
// A (should see this)
15+
type A struct{}
16+
17+
// B (should see this)
18+
type B struct{}
19+
20+
// C (should see this)
21+
type C struct{}
22+
23+
// D (should see this)
24+
type D struct{}
25+
26+
// E1 (should see this)
27+
type E1 struct{}
28+
29+
// E (should see this for E2 and E3)
30+
type E2 struct{}
31+
32+
// E (should see this for E2 and E3)
33+
type E3 struct{}
34+
35+
// E4 (should see this)
36+
type E4 struct{}
37+
38+
//
39+
type T1 struct{}
40+
41+
//
42+
func (t1 *T1) M()
43+
44+
// T2 must not show methods of local T1
45+
type T2 struct {
46+
a.T1 // not the same as locally declared T1
47+
}
48+

src/pkg/go/doc/testdata/d.2.golden

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
PACKAGE d
3+
4+
IMPORTPATH
5+
testdata/d
6+
7+
FILENAMES
8+
testdata/d1.go
9+
testdata/d2.go
10+
11+
CONSTANTS
12+
// CBx constants should appear before CAx constants.
13+
const (
14+
CB2 = iota // before CB1
15+
CB1 // before CB0
16+
CB0 // at end
17+
)
18+
19+
// CAx constants should appear after CBx constants.
20+
const (
21+
CA2 = iota // before CA1
22+
CA1 // before CA0
23+
CA0 // at end
24+
)
25+
26+
// C0 should be first.
27+
const C0 = 0
28+
29+
// C1 should be second.
30+
const C1 = 1
31+
32+
// C2 should be third.
33+
const C2 = 2
34+
35+
//
36+
const (
37+
// Single const declarations inside ()'s are considered ungrouped
38+
// and show up in sorted order.
39+
Cungrouped = 0
40+
)
41+
42+
43+
VARIABLES
44+
// VBx variables should appear before VAx variables.
45+
var (
46+
VB2 int // before VB1
47+
VB1 int // before VB0
48+
VB0 int // at end
49+
)
50+
51+
// VAx variables should appear after VBx variables.
52+
var (
53+
VA2 int // before VA1
54+
VA1 int // before VA0
55+
VA0 int // at end
56+
)
57+
58+
// V0 should be first.
59+
var V0 uintptr
60+
61+
// V1 should be second.
62+
var V1 uint
63+
64+
// V2 should be third.
65+
var V2 int
66+
67+
//
68+
var (
69+
// Single var declarations inside ()'s are considered ungrouped
70+
// and show up in sorted order.
71+
Vungrouped = 0
72+
)
73+
74+
75+
FUNCTIONS
76+
// F0 should be first.
77+
func F0()
78+
79+
// F1 should be second.
80+
func F1()
81+
82+
// F2 should be third.
83+
func F2()
84+
85+
86+
TYPES
87+
// T0 should be first.
88+
type T0 struct{}
89+
90+
// T1 should be second.
91+
type T1 struct{}
92+
93+
// T2 should be third.
94+
type T2 struct{}
95+
96+
// TG0 should be first.
97+
type TG0 struct{}
98+
99+
// TG1 should be second.
100+
type TG1 struct{}
101+
102+
// TG2 should be third.
103+
type TG2 struct{}
104+

src/pkg/go/doc/testdata/e.0.golden

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ TYPES
2929
// T3.M should appear as method of T3.
3030
func (T3) M()
3131

32-
// T1 has no embedded (level 1) M method due to conflict.
33-
type T4 struct {
34-
T2
35-
// contains filtered or unexported fields
32+
//
33+
type T4 struct{}
34+
35+
// T4.M should appear as method of T5 only if AllMethods is set.
36+
func (*T4) M()
37+
38+
//
39+
type T5 struct {
40+
T4
3641
}
3742

src/pkg/go/doc/testdata/e.1.golden

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ TYPES
3131
// T3.M should appear as method of T3.
3232
func (T3) M()
3333

34-
// T1 has no embedded (level 1) M method due to conflict.
35-
type T4 struct {
36-
t2
37-
T2
34+
//
35+
type T4 struct{}
36+
37+
// T4.M should appear as method of T5 only if AllMethods is set.
38+
func (*T4) M()
39+
40+
//
41+
type T5 struct {
42+
T4
3843
}
3944

4045
//

src/pkg/go/doc/testdata/e.2.golden

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// The package e is a go/doc test for embedded methods.
2+
PACKAGE e
3+
4+
IMPORTPATH
5+
testdata/e
6+
7+
FILENAMES
8+
testdata/e.go
9+
10+
TYPES
11+
// T1 has no embedded (level 1) M method due to conflict.
12+
type T1 struct {
13+
// contains filtered or unexported fields
14+
}
15+
16+
// T2 has only M as top-level method.
17+
type T2 struct {
18+
// contains filtered or unexported fields
19+
}
20+
21+
// T2.M should appear as method of T2.
22+
func (T2) M()
23+
24+
// T3 has only M as top-level method.
25+
type T3 struct {
26+
// contains filtered or unexported fields
27+
}
28+
29+
// T3.M should appear as method of T3.
30+
func (T3) M()
31+
32+
//
33+
type T4 struct{}
34+
35+
// T4.M should appear as method of T5 only if AllMethods is set.
36+
func (*T4) M()
37+
38+
//
39+
type T5 struct {
40+
T4
41+
}
42+
43+
// T4.M should appear as method of T5 only if AllMethods is set.
44+
func (*T5) M()
45+

src/pkg/go/doc/testdata/e.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,16 @@ type T4 struct {
6464
t2
6565
T2
6666
}
67+
68+
// ----------------------------------------------------------------------------
69+
// Don't show embedded methods of exported anonymous fields unless AllMethods
70+
// is set.
71+
72+
type T4 struct{}
73+
74+
// T4.M should appear as method of T5 only if AllMethods is set.
75+
func (*T4) M() {}
76+
77+
type T5 struct {
78+
T4
79+
}

0 commit comments

Comments
 (0)