]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: don't drop "factory" functions with dot-imported result types
authorRobert Griesemer <gri@golang.org>
Fri, 8 Jan 2016 02:40:40 +0000 (18:40 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 8 Jan 2016 04:10:51 +0000 (04:10 +0000)
Fixes #13742.

Change-Id: I7c8b51b60e31402bf708bf8d70e07fd06295e8ce
Reviewed-on: https://go-review.googlesource.com/18393
Reviewed-by: Russ Cox <rsc@golang.org>
src/go/doc/reader.go
src/go/doc/testdata/issue13742.0.golden [new file with mode: 0644]
src/go/doc/testdata/issue13742.1.golden [new file with mode: 0644]
src/go/doc/testdata/issue13742.2.golden [new file with mode: 0644]
src/go/doc/testdata/issue13742.go [new file with mode: 0644]

index f5c02b684d4cacbade35ae983823db74aece76c3..e4e7b7c1c7db945e50fb8d4e9527bfef21426a82 100644 (file)
@@ -151,10 +151,11 @@ type reader struct {
        notes     map[string][]*Note
 
        // declarations
-       imports map[string]int
-       values  []*Value // consts and vars
-       types   map[string]*namedType
-       funcs   methodSet
+       imports   map[string]int
+       hasDotImp bool     // if set, package contains a dot import
+       values    []*Value // consts and vars
+       types     map[string]*namedType
+       funcs     methodSet
 
        // support for package-local error type declarations
        errorDecl bool                 // if set, type "error" was declared locally
@@ -471,6 +472,9 @@ func (r *reader) readFile(src *ast.File) {
                                        if s, ok := spec.(*ast.ImportSpec); ok {
                                                if import_, err := strconv.Unquote(s.Path.Value); err == nil {
                                                        r.imports[import_] = 1
+                                                       if s.Name != nil && s.Name.Name == "." {
+                                                               r.hasDotImp = true
+                                                       }
                                                }
                                        }
                                }
@@ -641,11 +645,12 @@ func (r *reader) computeMethodSets() {
 func (r *reader) cleanupTypes() {
        for _, t := range r.types {
                visible := r.isVisible(t.name)
-               if t.decl == nil && (predeclaredTypes[t.name] || t.isEmbedded && visible) {
+               if t.decl == nil && (predeclaredTypes[t.name] || visible && (t.isEmbedded || r.hasDotImp)) {
                        // t.name is a predeclared type (and was not redeclared in this package),
                        // or it was embedded somewhere but its declaration is missing (because
-                       // the AST is incomplete): move any associated values, funcs, and methods
-                       // back to the top-level so that they are not lost.
+                       // the AST is incomplete), or we have a dot-import (and all bets are off):
+                       // move any associated values, funcs, and methods back to the top-level so
+                       // that they are not lost.
                        // 1) move values
                        r.values = append(r.values, t.values...)
                        // 2) move factory functions
diff --git a/src/go/doc/testdata/issue13742.0.golden b/src/go/doc/testdata/issue13742.0.golden
new file mode 100644 (file)
index 0000000..8dee9aa
--- /dev/null
@@ -0,0 +1,25 @@
+// 
+PACKAGE issue13742
+
+IMPORTPATH
+       testdata/issue13742
+
+IMPORTS
+       go/ast
+
+FILENAMES
+       testdata/issue13742.go
+
+FUNCTIONS
+       // Both F0 and G0 should appear as functions. 
+       func F0(Node)
+
+       // Both F1 and G1 should appear as functions. 
+       func F1(ast.Node)
+
+       // 
+       func G0() Node
+
+       // 
+       func G1() ast.Node
+
diff --git a/src/go/doc/testdata/issue13742.1.golden b/src/go/doc/testdata/issue13742.1.golden
new file mode 100644 (file)
index 0000000..8dee9aa
--- /dev/null
@@ -0,0 +1,25 @@
+// 
+PACKAGE issue13742
+
+IMPORTPATH
+       testdata/issue13742
+
+IMPORTS
+       go/ast
+
+FILENAMES
+       testdata/issue13742.go
+
+FUNCTIONS
+       // Both F0 and G0 should appear as functions. 
+       func F0(Node)
+
+       // Both F1 and G1 should appear as functions. 
+       func F1(ast.Node)
+
+       // 
+       func G0() Node
+
+       // 
+       func G1() ast.Node
+
diff --git a/src/go/doc/testdata/issue13742.2.golden b/src/go/doc/testdata/issue13742.2.golden
new file mode 100644 (file)
index 0000000..8dee9aa
--- /dev/null
@@ -0,0 +1,25 @@
+// 
+PACKAGE issue13742
+
+IMPORTPATH
+       testdata/issue13742
+
+IMPORTS
+       go/ast
+
+FILENAMES
+       testdata/issue13742.go
+
+FUNCTIONS
+       // Both F0 and G0 should appear as functions. 
+       func F0(Node)
+
+       // Both F1 and G1 should appear as functions. 
+       func F1(ast.Node)
+
+       // 
+       func G0() Node
+
+       // 
+       func G1() ast.Node
+
diff --git a/src/go/doc/testdata/issue13742.go b/src/go/doc/testdata/issue13742.go
new file mode 100644 (file)
index 0000000..dbc1941
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2016 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 issue13742
+
+import (
+       "go/ast"
+       . "go/ast"
+)
+
+// Both F0 and G0 should appear as functions.
+func F0(Node)  {}
+func G0() Node { return nil }
+
+// Both F1 and G1 should appear as functions.
+func F1(ast.Node)  {}
+func G1() ast.Node { return nil }