mirror of
https://github.com/golang/go
synced 2024-11-19 21:34:45 -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
|
||||
}
|
||||
|
||||
func (r *reader) isVisible(name string) bool {
|
||||
return r.mode&AllDecls != 0 || ast.IsExported(name)
|
||||
}
|
||||
|
||||
// lookupType returns the base type with the given name.
|
||||
// If the base type has not been encountered yet, a new
|
||||
// type with the given name but no associated declaration
|
||||
@ -343,7 +347,7 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
|
||||
// strip function body
|
||||
fun.Body = nil
|
||||
|
||||
// determine if it should be associated with a type
|
||||
// associate methods with the receiver type, if any
|
||||
if fun.Recv != nil {
|
||||
// method
|
||||
recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type)
|
||||
@ -363,17 +367,16 @@ func (r *reader) readFunc(fun *ast.FuncDecl) {
|
||||
return
|
||||
}
|
||||
|
||||
// perhaps a factory function
|
||||
// determine result type, if any
|
||||
// associate factory functions with the first visible result type, if any
|
||||
if fun.Type.Results.NumFields() >= 1 {
|
||||
res := fun.Type.Results.List[0]
|
||||
if len(res.Names) <= 1 {
|
||||
// exactly one (named or anonymous) result associated
|
||||
// with the first type in result signature (there may
|
||||
// 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 {
|
||||
// associate Func with typ
|
||||
// associate function with typ
|
||||
typ.funcs.set(fun)
|
||||
return
|
||||
}
|
||||
@ -580,7 +583,7 @@ func (r *reader) computeMethodSets() {
|
||||
//
|
||||
func (r *reader) cleanupTypes() {
|
||||
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) {
|
||||
// t.name is a predeclared type (and was not redeclared in this package),
|
||||
// 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