mirror of
https://github.com/golang/go
synced 2024-11-19 21:24:40 -07:00
go/doc: don't lose factory functions of non-exported types
Fixes #2824. R=rsc, r CC=golang-dev https://golang.org/cl/5615043
This commit is contained in:
parent
44122ed069
commit
212ba8076e
@ -154,6 +154,10 @@ type reader struct {
|
|||||||
funcs methodSet
|
funcs methodSet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *reader) isVisible(name string) bool {
|
||||||
|
return r.mode&AllDecls != 0 || ast.IsExported(name)
|
||||||
|
}
|
||||||
|
|
||||||
// lookupType returns the base type with the given name.
|
// lookupType returns the base type with the given name.
|
||||||
// If the base type has not been encountered yet, a new
|
// If the base type has not been encountered yet, a new
|
||||||
// type with the given name but no associated declaration
|
// type with the given name but no associated declaration
|
||||||
@ -343,7 +347,7 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
|
|||||||
// strip function body
|
// strip function body
|
||||||
fun.Body = nil
|
fun.Body = nil
|
||||||
|
|
||||||
// determine if it should be associated with a type
|
// associate methods with the receiver type, if any
|
||||||
if fun.Recv != nil {
|
if fun.Recv != nil {
|
||||||
// method
|
// method
|
||||||
recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type)
|
recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type)
|
||||||
@ -363,17 +367,16 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// perhaps a factory function
|
// associate factory functions with the first visible result type, if any
|
||||||
// determine result type, if any
|
|
||||||
if fun.Type.Results.NumFields() >= 1 {
|
if fun.Type.Results.NumFields() >= 1 {
|
||||||
res := fun.Type.Results.List[0]
|
res := fun.Type.Results.List[0]
|
||||||
if len(res.Names) <= 1 {
|
if len(res.Names) <= 1 {
|
||||||
// exactly one (named or anonymous) result associated
|
// exactly one (named or anonymous) result associated
|
||||||
// with the first type in result signature (there may
|
// with the first type in result signature (there may
|
||||||
// be more than one result)
|
// be more than one result)
|
||||||
if n, imp := baseTypeName(res.Type); !imp {
|
if n, imp := baseTypeName(res.Type); !imp && r.isVisible(n) {
|
||||||
if typ := r.lookupType(n); typ != nil {
|
if typ := r.lookupType(n); typ != nil {
|
||||||
// associate Func with typ
|
// associate function with typ
|
||||||
typ.funcs.set(fun)
|
typ.funcs.set(fun)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -580,7 +583,7 @@ func (r *reader) computeMethodSets() {
|
|||||||
//
|
//
|
||||||
func (r *reader) cleanupTypes() {
|
func (r *reader) cleanupTypes() {
|
||||||
for _, t := range r.types {
|
for _, t := range r.types {
|
||||||
visible := r.mode&AllDecls != 0 || ast.IsExported(t.name)
|
visible := r.isVisible(t.name)
|
||||||
if t.decl == nil && (predeclaredTypes[t.name] || t.isEmbedded && visible) {
|
if t.decl == nil && (predeclaredTypes[t.name] || t.isEmbedded && visible) {
|
||||||
// t.name is a predeclared type (and was not redeclared in this package),
|
// t.name is a predeclared type (and was not redeclared in this package),
|
||||||
// or it was embedded somewhere but its declaration is missing (because
|
// or it was embedded somewhere but its declaration is missing (because
|
||||||
|
13
src/pkg/go/doc/testdata/f.0.golden
vendored
Normal file
13
src/pkg/go/doc/testdata/f.0.golden
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// The package f is a go/doc test for functions and factory ...
|
||||||
|
PACKAGE f
|
||||||
|
|
||||||
|
IMPORTPATH
|
||||||
|
testdata/f
|
||||||
|
|
||||||
|
FILENAMES
|
||||||
|
testdata/f.go
|
||||||
|
|
||||||
|
FUNCTIONS
|
||||||
|
// Exported must always be visible. Was issue 2824.
|
||||||
|
func Exported() private
|
||||||
|
|
16
src/pkg/go/doc/testdata/f.1.golden
vendored
Normal file
16
src/pkg/go/doc/testdata/f.1.golden
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// The package f is a go/doc test for functions and factory ...
|
||||||
|
PACKAGE f
|
||||||
|
|
||||||
|
IMPORTPATH
|
||||||
|
testdata/f
|
||||||
|
|
||||||
|
FILENAMES
|
||||||
|
testdata/f.go
|
||||||
|
|
||||||
|
TYPES
|
||||||
|
//
|
||||||
|
type private struct{}
|
||||||
|
|
||||||
|
// Exported must always be visible. Was issue 2824.
|
||||||
|
func Exported() private
|
||||||
|
|
13
src/pkg/go/doc/testdata/f.2.golden
vendored
Normal file
13
src/pkg/go/doc/testdata/f.2.golden
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// The package f is a go/doc test for functions and factory ...
|
||||||
|
PACKAGE f
|
||||||
|
|
||||||
|
IMPORTPATH
|
||||||
|
testdata/f
|
||||||
|
|
||||||
|
FILENAMES
|
||||||
|
testdata/f.go
|
||||||
|
|
||||||
|
FUNCTIONS
|
||||||
|
// Exported must always be visible. Was issue 2824.
|
||||||
|
func Exported() private
|
||||||
|
|
14
src/pkg/go/doc/testdata/f.go
vendored
Normal file
14
src/pkg/go/doc/testdata/f.go
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// Copyright 2011 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.
|
||||||
|
|
||||||
|
// The package f is a go/doc test for functions and factory methods.
|
||||||
|
package f
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Factory functions for non-exported types must not get lost.
|
||||||
|
|
||||||
|
type private struct{}
|
||||||
|
|
||||||
|
// Exported must always be visible. Was issue 2824.
|
||||||
|
func Exported() private {}
|
Loading…
Reference in New Issue
Block a user