Skip to content

Commit 33484a6

Browse files
tomwansgriesemer
authored andcommitted
go/doc: classify function returning slice of T as constructor
Previously, go/doc would only consider functions that return types of T or any number of pointers to T: *T, **T, etc. This change expands the definition of a constructor to also include functions that return slices of a type (or pointer to that type) in its first return. With this change, the following return types classify a function as a constructor of type T: T *T **T (and so on) []T []*T []**T (and so on) Fixes #18063. Change-Id: I9a1a689933e13c6b8eb80b74ceec85bd4cab236d Reviewed-on: https://go-review.googlesource.com/54971 Reviewed-by: Robert Griesemer <[email protected]>
1 parent edaa0ff commit 33484a6

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

src/go/doc/reader.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,13 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
391391
// exactly one (named or anonymous) result associated
392392
// with the first type in result signature (there may
393393
// be more than one result)
394-
if n, imp := baseTypeName(res.Type); !imp && r.isVisible(n) {
394+
factoryType := res.Type
395+
if t, ok := factoryType.(*ast.ArrayType); ok && t.Len == nil {
396+
// We consider functions that return slices of type T (or
397+
// pointers to T) as factory functions of T.
398+
factoryType = t.Elt
399+
}
400+
if n, imp := baseTypeName(factoryType); !imp && r.isVisible(n) {
395401
if typ := r.lookupType(n); typ != nil {
396402
// associate function with typ
397403
typ.funcs.set(fun)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
PACKAGE issue18063
3+
4+
IMPORTPATH
5+
testdata/issue18063
6+
7+
FILENAMES
8+
testdata/issue18063.go
9+
10+
FUNCTIONS
11+
// NewArray is not a factory function because arrays of type T are ...
12+
func NewArray() [1]T
13+
14+
// NewPointerArray is not a factory function because arrays of ...
15+
func NewPointerArray() [1]*T
16+
17+
// NewPointerSliceOfSlice is not a factory function because slices ...
18+
func NewPointerSliceOfSlice() [][]*T
19+
20+
// NewSlice3 is not a factory function because 3 nested slices of ...
21+
func NewSlice3() [][][]T
22+
23+
// NewSliceOfSlice is not a factory function because slices of a ...
24+
func NewSliceOfSlice() [][]T
25+
26+
27+
TYPES
28+
//
29+
type T struct{}
30+
31+
//
32+
func New() T
33+
34+
//
35+
func NewPointer() *T
36+
37+
//
38+
func NewPointerOfPointer() **T
39+
40+
//
41+
func NewPointerSlice() []*T
42+
43+
//
44+
func NewSlice() []T
45+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
PACKAGE issue18063
3+
4+
IMPORTPATH
5+
testdata/issue18063
6+
7+
FILENAMES
8+
testdata/issue18063.go
9+
10+
FUNCTIONS
11+
// NewArray is not a factory function because arrays of type T are ...
12+
func NewArray() [1]T
13+
14+
// NewPointerArray is not a factory function because arrays of ...
15+
func NewPointerArray() [1]*T
16+
17+
// NewPointerSliceOfSlice is not a factory function because slices ...
18+
func NewPointerSliceOfSlice() [][]*T
19+
20+
// NewSlice3 is not a factory function because 3 nested slices of ...
21+
func NewSlice3() [][][]T
22+
23+
// NewSliceOfSlice is not a factory function because slices of a ...
24+
func NewSliceOfSlice() [][]T
25+
26+
27+
TYPES
28+
//
29+
type T struct{}
30+
31+
//
32+
func New() T
33+
34+
//
35+
func NewPointer() *T
36+
37+
//
38+
func NewPointerOfPointer() **T
39+
40+
//
41+
func NewPointerSlice() []*T
42+
43+
//
44+
func NewSlice() []T
45+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
PACKAGE issue18063
3+
4+
IMPORTPATH
5+
testdata/issue18063
6+
7+
FILENAMES
8+
testdata/issue18063.go
9+
10+
FUNCTIONS
11+
// NewArray is not a factory function because arrays of type T are ...
12+
func NewArray() [1]T
13+
14+
// NewPointerArray is not a factory function because arrays of ...
15+
func NewPointerArray() [1]*T
16+
17+
// NewPointerSliceOfSlice is not a factory function because slices ...
18+
func NewPointerSliceOfSlice() [][]*T
19+
20+
// NewSlice3 is not a factory function because 3 nested slices of ...
21+
func NewSlice3() [][][]T
22+
23+
// NewSliceOfSlice is not a factory function because slices of a ...
24+
func NewSliceOfSlice() [][]T
25+
26+
27+
TYPES
28+
//
29+
type T struct{}
30+
31+
//
32+
func New() T
33+
34+
//
35+
func NewPointer() *T
36+
37+
//
38+
func NewPointerOfPointer() **T
39+
40+
//
41+
func NewPointerSlice() []*T
42+
43+
//
44+
func NewSlice() []T
45+

src/go/doc/testdata/issue18063.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2017 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 issue18063
6+
7+
type T struct{}
8+
9+
func New() T { return T{} }
10+
func NewPointer() *T { return &T{} }
11+
func NewPointerSlice() []*T { return []*T{&T{}} }
12+
func NewSlice() []T { return []T{T{}} }
13+
func NewPointerOfPointer() **T { x := &T{}; return &x }
14+
15+
// NewArray is not a factory function because arrays of type T are not
16+
// factory functions of type T.
17+
func NewArray() [1]T { return [1]T{T{}} }
18+
19+
// NewPointerArray is not a factory function because arrays of type *T are not
20+
// factory functions of type T.
21+
func NewPointerArray() [1]*T { return [1]*T{&T{}} }
22+
23+
// NewSliceOfSlice is not a factory function because slices of a slice of
24+
// type *T are not factory functions of type T.
25+
func NewSliceOfSlice() [][]T { return []T{[]T{}} }
26+
27+
// NewPointerSliceOfSlice is not a factory function because slices of a
28+
// slice of type *T are not factory functions of type T.
29+
func NewPointerSliceOfSlice() [][]*T { return []*T{[]*T{}} }
30+
31+
// NewSlice3 is not a factory function because 3 nested slices of type T
32+
// are not factory functions of type T.
33+
func NewSlice3() [][][]T { return []T{[]T{[]T{}}} }

0 commit comments

Comments
 (0)