From adeb7e640b04526c38c481aff85b923ca14fc92b Mon Sep 17 00:00:00 2001 From: Thomas Wanielista Date: Fri, 22 Dec 2017 16:17:56 -0500 Subject: 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/go/doc/reader.go | 6 ++--- src/go/doc/testdata/issue18063.0.golden | 45 --------------------------------- src/go/doc/testdata/issue18063.1.golden | 45 --------------------------------- src/go/doc/testdata/issue18063.2.golden | 45 --------------------------------- src/go/doc/testdata/issue18063.go | 33 ------------------------ src/go/doc/testdata/issue22856.0.golden | 45 +++++++++++++++++++++++++++++++++ src/go/doc/testdata/issue22856.1.golden | 45 +++++++++++++++++++++++++++++++++ src/go/doc/testdata/issue22856.2.golden | 45 +++++++++++++++++++++++++++++++++ src/go/doc/testdata/issue22856.go | 27 ++++++++++++++++++++ 9 files changed, 165 insertions(+), 171 deletions(-) delete mode 100644 src/go/doc/testdata/issue18063.0.golden delete mode 100644 src/go/doc/testdata/issue18063.1.golden delete mode 100644 src/go/doc/testdata/issue18063.2.golden delete mode 100644 src/go/doc/testdata/issue18063.go create mode 100644 src/go/doc/testdata/issue22856.0.golden create mode 100644 src/go/doc/testdata/issue22856.1.golden create mode 100644 src/go/doc/testdata/issue22856.2.golden create mode 100644 src/go/doc/testdata/issue22856.go (limited to 'src') diff --git a/src/go/doc/reader.go b/src/go/doc/reader.go index 5d6f6e8fb0..05c3786ef6 100644 --- a/src/go/doc/reader.go +++ b/src/go/doc/reader.go @@ -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) { diff --git a/src/go/doc/testdata/issue18063.0.golden b/src/go/doc/testdata/issue18063.0.golden deleted file mode 100644 index 0afbc169c2..0000000000 --- a/src/go/doc/testdata/issue18063.0.golden +++ /dev/null @@ -1,45 +0,0 @@ -// -PACKAGE issue18063 - -IMPORTPATH - testdata/issue18063 - -FILENAMES - testdata/issue18063.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 - - // NewSlice3 is not a factory function because 3 nested slices of ... - func NewSlice3() [][][]T - - // NewSliceOfSlice is not a factory function because slices of a ... - func NewSliceOfSlice() [][]T - - -TYPES - // - type T struct{} - - // - func New() T - - // - func NewPointer() *T - - // - func NewPointerOfPointer() **T - - // - func NewPointerSlice() []*T - - // - func NewSlice() []T - diff --git a/src/go/doc/testdata/issue18063.1.golden b/src/go/doc/testdata/issue18063.1.golden deleted file mode 100644 index 0afbc169c2..0000000000 --- a/src/go/doc/testdata/issue18063.1.golden +++ /dev/null @@ -1,45 +0,0 @@ -// -PACKAGE issue18063 - -IMPORTPATH - testdata/issue18063 - -FILENAMES - testdata/issue18063.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 - - // NewSlice3 is not a factory function because 3 nested slices of ... - func NewSlice3() [][][]T - - // NewSliceOfSlice is not a factory function because slices of a ... - func NewSliceOfSlice() [][]T - - -TYPES - // - type T struct{} - - // - func New() T - - // - func NewPointer() *T - - // - func NewPointerOfPointer() **T - - // - func NewPointerSlice() []*T - - // - func NewSlice() []T - diff --git a/src/go/doc/testdata/issue18063.2.golden b/src/go/doc/testdata/issue18063.2.golden deleted file mode 100644 index 0afbc169c2..0000000000 --- a/src/go/doc/testdata/issue18063.2.golden +++ /dev/null @@ -1,45 +0,0 @@ -// -PACKAGE issue18063 - -IMPORTPATH - testdata/issue18063 - -FILENAMES - testdata/issue18063.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 - - // NewSlice3 is not a factory function because 3 nested slices of ... - func NewSlice3() [][][]T - - // NewSliceOfSlice is not a factory function because slices of a ... - func NewSliceOfSlice() [][]T - - -TYPES - // - type T struct{} - - // - func New() T - - // - func NewPointer() *T - - // - func NewPointerOfPointer() **T - - // - func NewPointerSlice() []*T - - // - func NewSlice() []T - diff --git a/src/go/doc/testdata/issue18063.go b/src/go/doc/testdata/issue18063.go deleted file mode 100644 index 1193af51e7..0000000000 --- a/src/go/doc/testdata/issue18063.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package issue18063 - -type T struct{} - -func New() T { return T{} } -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{}} } - -// NewSliceOfSlice is not a factory function because slices of a slice of -// type *T are not factory functions of type T. -func NewSliceOfSlice() [][]T { return []T{[]T{}} } - -// NewPointerSliceOfSlice is not a factory function because slices of a -// slice of type *T are not factory functions of type T. -func NewPointerSliceOfSlice() [][]*T { return []*T{[]*T{}} } - -// NewSlice3 is not a factory function because 3 nested slices of type T -// are not factory functions of type T. -func NewSlice3() [][][]T { return []T{[]T{[]T{}}} } diff --git a/src/go/doc/testdata/issue22856.0.golden b/src/go/doc/testdata/issue22856.0.golden new file mode 100644 index 0000000000..a88f43f4bd --- /dev/null +++ b/src/go/doc/testdata/issue22856.0.golden @@ -0,0 +1,45 @@ +// +PACKAGE issue22856 + +IMPORTPATH + testdata/issue22856 + +FILENAMES + testdata/issue22856.go + +FUNCTIONS + // NewPointerSliceOfSlice is not a factory function because slices ... + func NewPointerSliceOfSlice() [][]*T + + // NewSlice3 is not a factory function because 3 nested slices of ... + func NewSlice3() [][][]T + + // NewSliceOfSlice is not a factory function because slices of a ... + func NewSliceOfSlice() [][]T + + +TYPES + // + type T struct{} + + // + func New() T + + // + func NewArray() [1]T + + // + func NewPointer() *T + + // + func NewPointerArray() [1]*T + + // + func NewPointerOfPointer() **T + + // + func NewPointerSlice() []*T + + // + func NewSlice() []T + diff --git a/src/go/doc/testdata/issue22856.1.golden b/src/go/doc/testdata/issue22856.1.golden new file mode 100644 index 0000000000..a88f43f4bd --- /dev/null +++ b/src/go/doc/testdata/issue22856.1.golden @@ -0,0 +1,45 @@ +// +PACKAGE issue22856 + +IMPORTPATH + testdata/issue22856 + +FILENAMES + testdata/issue22856.go + +FUNCTIONS + // NewPointerSliceOfSlice is not a factory function because slices ... + func NewPointerSliceOfSlice() [][]*T + + // NewSlice3 is not a factory function because 3 nested slices of ... + func NewSlice3() [][][]T + + // NewSliceOfSlice is not a factory function because slices of a ... + func NewSliceOfSlice() [][]T + + +TYPES + // + type T struct{} + + // + func New() T + + // + func NewArray() [1]T + + // + func NewPointer() *T + + // + func NewPointerArray() [1]*T + + // + func NewPointerOfPointer() **T + + // + func NewPointerSlice() []*T + + // + func NewSlice() []T + diff --git a/src/go/doc/testdata/issue22856.2.golden b/src/go/doc/testdata/issue22856.2.golden new file mode 100644 index 0000000000..a88f43f4bd --- /dev/null +++ b/src/go/doc/testdata/issue22856.2.golden @@ -0,0 +1,45 @@ +// +PACKAGE issue22856 + +IMPORTPATH + testdata/issue22856 + +FILENAMES + testdata/issue22856.go + +FUNCTIONS + // NewPointerSliceOfSlice is not a factory function because slices ... + func NewPointerSliceOfSlice() [][]*T + + // NewSlice3 is not a factory function because 3 nested slices of ... + func NewSlice3() [][][]T + + // NewSliceOfSlice is not a factory function because slices of a ... + func NewSliceOfSlice() [][]T + + +TYPES + // + type T struct{} + + // + func New() T + + // + func NewArray() [1]T + + // + func NewPointer() *T + + // + func NewPointerArray() [1]*T + + // + func NewPointerOfPointer() **T + + // + func NewPointerSlice() []*T + + // + func NewSlice() []T + diff --git a/src/go/doc/testdata/issue22856.go b/src/go/doc/testdata/issue22856.go new file mode 100644 index 0000000000..f4569981aa --- /dev/null +++ b/src/go/doc/testdata/issue22856.go @@ -0,0 +1,27 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package issue22856 + +type T struct{} + +func New() T { return T{} } +func NewPointer() *T { return &T{} } +func NewPointerSlice() []*T { return []*T{&T{}} } +func NewSlice() []T { return []T{T{}} } +func NewPointerOfPointer() **T { x := &T{}; return &x } +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. +func NewSliceOfSlice() [][]T { return []T{[]T{}} } + +// NewPointerSliceOfSlice is not a factory function because slices of a +// slice of type *T are not factory functions of type T. +func NewPointerSliceOfSlice() [][]*T { return []*T{[]*T{}} } + +// NewSlice3 is not a factory function because 3 nested slices of type T +// are not factory functions of type T. +func NewSlice3() [][][]T { return []T{[]T{[]T{}}} } -- cgit v1.3-5-g9baa