Skip to content

Commit adeb7e6

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

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

src/go/doc/reader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
399399
// with the first type in result signature (there may
400400
// be more than one result)
401401
factoryType := res.Type
402-
if t, ok := factoryType.(*ast.ArrayType); ok && t.Len == nil {
403-
// We consider functions that return slices of type T (or
404-
// pointers to T) as factory functions of T.
402+
if t, ok := factoryType.(*ast.ArrayType); ok {
403+
// We consider functions that return slices or arrays of type
404+
// T (or pointers to T) as factory functions of T.
405405
factoryType = t.Elt
406406
}
407407
if n, imp := baseTypeName(factoryType); !imp && r.isVisible(n) {

src/go/doc/testdata/issue18063.0.golden renamed to src/go/doc/testdata/issue22856.0.golden

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
//
2-
PACKAGE issue18063
2+
PACKAGE issue22856
33

44
IMPORTPATH
5-
testdata/issue18063
5+
testdata/issue22856
66

77
FILENAMES
8-
testdata/issue18063.go
8+
testdata/issue22856.go
99

1010
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-
1711
// NewPointerSliceOfSlice is not a factory function because slices ...
1812
func NewPointerSliceOfSlice() [][]*T
1913

@@ -31,9 +25,15 @@ TYPES
3125
//
3226
func New() T
3327

28+
//
29+
func NewArray() [1]T
30+
3431
//
3532
func NewPointer() *T
3633

34+
//
35+
func NewPointerArray() [1]*T
36+
3737
//
3838
func NewPointerOfPointer() **T
3939

src/go/doc/testdata/issue18063.1.golden renamed to src/go/doc/testdata/issue22856.1.golden

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
//
2-
PACKAGE issue18063
2+
PACKAGE issue22856
33

44
IMPORTPATH
5-
testdata/issue18063
5+
testdata/issue22856
66

77
FILENAMES
8-
testdata/issue18063.go
8+
testdata/issue22856.go
99

1010
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-
1711
// NewPointerSliceOfSlice is not a factory function because slices ...
1812
func NewPointerSliceOfSlice() [][]*T
1913

@@ -31,9 +25,15 @@ TYPES
3125
//
3226
func New() T
3327

28+
//
29+
func NewArray() [1]T
30+
3431
//
3532
func NewPointer() *T
3633

34+
//
35+
func NewPointerArray() [1]*T
36+
3737
//
3838
func NewPointerOfPointer() **T
3939

src/go/doc/testdata/issue18063.2.golden renamed to src/go/doc/testdata/issue22856.2.golden

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
//
2-
PACKAGE issue18063
2+
PACKAGE issue22856
33

44
IMPORTPATH
5-
testdata/issue18063
5+
testdata/issue22856
66

77
FILENAMES
8-
testdata/issue18063.go
8+
testdata/issue22856.go
99

1010
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-
1711
// NewPointerSliceOfSlice is not a factory function because slices ...
1812
func NewPointerSliceOfSlice() [][]*T
1913

@@ -31,9 +25,15 @@ TYPES
3125
//
3226
func New() T
3327

28+
//
29+
func NewArray() [1]T
30+
3431
//
3532
func NewPointer() *T
3633

34+
//
35+
func NewPointerArray() [1]*T
36+
3737
//
3838
func NewPointerOfPointer() **T
3939

src/go/doc/testdata/issue18063.go renamed to src/go/doc/testdata/issue22856.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package issue18063
5+
package issue22856
66

77
type T struct{}
88

@@ -11,14 +11,8 @@ func NewPointer() *T { return &T{} }
1111
func NewPointerSlice() []*T { return []*T{&T{}} }
1212
func NewSlice() []T { return []T{T{}} }
1313
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{}} }
14+
func NewArray() [1]T { return [1]T{T{}} }
15+
func NewPointerArray() [1]*T { return [1]*T{&T{}} }
2216

2317
// NewSliceOfSlice is not a factory function because slices of a slice of
2418
// type *T are not factory functions of type T.

0 commit comments

Comments
 (0)