]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: classify function returning slice or array of T as constructor
authorThomas Wanielista <tomwans@gmail.com>
Fri, 22 Dec 2017 21:17:56 +0000 (16:17 -0500)
committerRobert Griesemer <gri@golang.org>
Tue, 12 Jun 2018 16:37:37 +0000 (16:37 +0000)
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 <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/doc/reader.go
src/go/doc/testdata/issue22856.0.golden [moved from src/go/doc/testdata/issue18063.0.golden with 73% similarity]
src/go/doc/testdata/issue22856.1.golden [moved from src/go/doc/testdata/issue18063.1.golden with 73% similarity]
src/go/doc/testdata/issue22856.2.golden [moved from src/go/doc/testdata/issue18063.2.golden with 73% similarity]
src/go/doc/testdata/issue22856.go [moved from src/go/doc/testdata/issue18063.go with 74% similarity]

index 5d6f6e8fb00cc396e6e5c269316ad40561f62ed8..05c3786ef605f4e09f34adc829668263815b5fc1 100644 (file)
@@ -399,9 +399,9 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
                        // with the first type in result signature (there may
                        // be more than one result)
                        factoryType := res.Type
-                       if t, ok := factoryType.(*ast.ArrayType); ok && t.Len == nil {
-                               // We consider functions that return slices of type T (or
-                               // pointers to T) as factory functions of T.
+                       if t, ok := factoryType.(*ast.ArrayType); ok {
+                               // We consider functions that return slices or arrays of type
+                               // T (or pointers to T) as factory functions of T.
                                factoryType = t.Elt
                        }
                        if n, imp := baseTypeName(factoryType); !imp && r.isVisible(n) {
similarity index 73%
rename from src/go/doc/testdata/issue18063.0.golden
rename to src/go/doc/testdata/issue22856.0.golden
index 0afbc169c2c3311b6402ece107634933997436e8..a88f43f4bd7e219bdaf3b6ca670ac57d0b351324 100644 (file)
@@ -1,19 +1,13 @@
 // 
-PACKAGE issue18063
+PACKAGE issue22856
 
 IMPORTPATH
-       testdata/issue18063
+       testdata/issue22856
 
 FILENAMES
-       testdata/issue18063.go
+       testdata/issue22856.go
 
 FUNCTIONS
-       // NewArray is not a factory function because arrays of type T are ...
-       func NewArray() [1]T
-
-       // NewPointerArray is not a factory function because arrays of ...
-       func NewPointerArray() [1]*T
-
        // NewPointerSliceOfSlice is not a factory function because slices ...
        func NewPointerSliceOfSlice() [][]*T
 
@@ -31,9 +25,15 @@ TYPES
        // 
        func New() T
 
+       // 
+       func NewArray() [1]T
+
        // 
        func NewPointer() *T
 
+       // 
+       func NewPointerArray() [1]*T
+
        // 
        func NewPointerOfPointer() **T
 
similarity index 73%
rename from src/go/doc/testdata/issue18063.1.golden
rename to src/go/doc/testdata/issue22856.1.golden
index 0afbc169c2c3311b6402ece107634933997436e8..a88f43f4bd7e219bdaf3b6ca670ac57d0b351324 100644 (file)
@@ -1,19 +1,13 @@
 // 
-PACKAGE issue18063
+PACKAGE issue22856
 
 IMPORTPATH
-       testdata/issue18063
+       testdata/issue22856
 
 FILENAMES
-       testdata/issue18063.go
+       testdata/issue22856.go
 
 FUNCTIONS
-       // NewArray is not a factory function because arrays of type T are ...
-       func NewArray() [1]T
-
-       // NewPointerArray is not a factory function because arrays of ...
-       func NewPointerArray() [1]*T
-
        // NewPointerSliceOfSlice is not a factory function because slices ...
        func NewPointerSliceOfSlice() [][]*T
 
@@ -31,9 +25,15 @@ TYPES
        // 
        func New() T
 
+       // 
+       func NewArray() [1]T
+
        // 
        func NewPointer() *T
 
+       // 
+       func NewPointerArray() [1]*T
+
        // 
        func NewPointerOfPointer() **T
 
similarity index 73%
rename from src/go/doc/testdata/issue18063.2.golden
rename to src/go/doc/testdata/issue22856.2.golden
index 0afbc169c2c3311b6402ece107634933997436e8..a88f43f4bd7e219bdaf3b6ca670ac57d0b351324 100644 (file)
@@ -1,19 +1,13 @@
 // 
-PACKAGE issue18063
+PACKAGE issue22856
 
 IMPORTPATH
-       testdata/issue18063
+       testdata/issue22856
 
 FILENAMES
-       testdata/issue18063.go
+       testdata/issue22856.go
 
 FUNCTIONS
-       // NewArray is not a factory function because arrays of type T are ...
-       func NewArray() [1]T
-
-       // NewPointerArray is not a factory function because arrays of ...
-       func NewPointerArray() [1]*T
-
        // NewPointerSliceOfSlice is not a factory function because slices ...
        func NewPointerSliceOfSlice() [][]*T
 
@@ -31,9 +25,15 @@ TYPES
        // 
        func New() T
 
+       // 
+       func NewArray() [1]T
+
        // 
        func NewPointer() *T
 
+       // 
+       func NewPointerArray() [1]*T
+
        // 
        func NewPointerOfPointer() **T
 
similarity index 74%
rename from src/go/doc/testdata/issue18063.go
rename to src/go/doc/testdata/issue22856.go
index 1193af51e7c120cd5ae61d34d2fc92d88c032fe8..f4569981aaac659ae081aa3233ce159e0f18ca78 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package issue18063
+package issue22856
 
 type T struct{}
 
@@ -11,14 +11,8 @@ func NewPointer() *T           { return &T{} }
 func NewPointerSlice() []*T    { return []*T{&T{}} }
 func NewSlice() []T            { return []T{T{}} }
 func NewPointerOfPointer() **T { x := &T{}; return &x }
-
-// NewArray is not a factory function because arrays of type T are not
-// factory functions of type T.
-func NewArray() [1]T { return [1]T{T{}} }
-
-// NewPointerArray is not a factory function because arrays of type *T are not
-// factory functions of type T.
-func NewPointerArray() [1]*T { return [1]*T{&T{}} }
+func NewArray() [1]T           { return [1]T{T{}} }
+func NewPointerArray() [1]*T   { return [1]*T{&T{}} }
 
 // NewSliceOfSlice is not a factory function because slices of a slice of
 // type *T are not factory functions of type T.