]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: don't lose factory functions of non-exported types
authorRobert Griesemer <gri@golang.org>
Fri, 3 Feb 2012 03:25:29 +0000 (19:25 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 3 Feb 2012 03:25:29 +0000 (19:25 -0800)
Fixes #2824.

R=rsc, r
CC=golang-dev
https://golang.org/cl/5615043

src/pkg/go/doc/reader.go
src/pkg/go/doc/testdata/f.0.golden [new file with mode: 0644]
src/pkg/go/doc/testdata/f.1.golden [new file with mode: 0644]
src/pkg/go/doc/testdata/f.2.golden [new file with mode: 0644]
src/pkg/go/doc/testdata/f.go [new file with mode: 0644]

index 31648b13e1e1ec6a9246a63e287ff9f3a725b4c0..dcf49f68fd378d5e070fd7dc3cf38f2a876db2a9 100644 (file)
@@ -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
diff --git a/src/pkg/go/doc/testdata/f.0.golden b/src/pkg/go/doc/testdata/f.0.golden
new file mode 100644 (file)
index 0000000..8175901
--- /dev/null
@@ -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
+
diff --git a/src/pkg/go/doc/testdata/f.1.golden b/src/pkg/go/doc/testdata/f.1.golden
new file mode 100644 (file)
index 0000000..ba68e88
--- /dev/null
@@ -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
+
diff --git a/src/pkg/go/doc/testdata/f.2.golden b/src/pkg/go/doc/testdata/f.2.golden
new file mode 100644 (file)
index 0000000..8175901
--- /dev/null
@@ -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
+
diff --git a/src/pkg/go/doc/testdata/f.go b/src/pkg/go/doc/testdata/f.go
new file mode 100644 (file)
index 0000000..a3051e1
--- /dev/null
@@ -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 {}