Skip to content

Commit ee566d5

Browse files
committed
go/doc: don't drop "factory" functions with dot-imported result types
Fixes #13742. Change-Id: I7c8b51b60e31402bf708bf8d70e07fd06295e8ce Reviewed-on: https://go-review.googlesource.com/18393 Reviewed-by: Russ Cox <[email protected]>
1 parent d91ec5b commit ee566d5

File tree

5 files changed

+105
-7
lines changed

5 files changed

+105
-7
lines changed

src/go/doc/reader.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ type reader struct {
151151
notes map[string][]*Note
152152

153153
// declarations
154-
imports map[string]int
155-
values []*Value // consts and vars
156-
types map[string]*namedType
157-
funcs methodSet
154+
imports map[string]int
155+
hasDotImp bool // if set, package contains a dot import
156+
values []*Value // consts and vars
157+
types map[string]*namedType
158+
funcs methodSet
158159

159160
// support for package-local error type declarations
160161
errorDecl bool // if set, type "error" was declared locally
@@ -471,6 +472,9 @@ func (r *reader) readFile(src *ast.File) {
471472
if s, ok := spec.(*ast.ImportSpec); ok {
472473
if import_, err := strconv.Unquote(s.Path.Value); err == nil {
473474
r.imports[import_] = 1
475+
if s.Name != nil && s.Name.Name == "." {
476+
r.hasDotImp = true
477+
}
474478
}
475479
}
476480
}
@@ -641,11 +645,12 @@ func (r *reader) computeMethodSets() {
641645
func (r *reader) cleanupTypes() {
642646
for _, t := range r.types {
643647
visible := r.isVisible(t.name)
644-
if t.decl == nil && (predeclaredTypes[t.name] || t.isEmbedded && visible) {
648+
if t.decl == nil && (predeclaredTypes[t.name] || visible && (t.isEmbedded || r.hasDotImp)) {
645649
// t.name is a predeclared type (and was not redeclared in this package),
646650
// or it was embedded somewhere but its declaration is missing (because
647-
// the AST is incomplete): move any associated values, funcs, and methods
648-
// back to the top-level so that they are not lost.
651+
// the AST is incomplete), or we have a dot-import (and all bets are off):
652+
// move any associated values, funcs, and methods back to the top-level so
653+
// that they are not lost.
649654
// 1) move values
650655
r.values = append(r.values, t.values...)
651656
// 2) move factory functions
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
PACKAGE issue13742
3+
4+
IMPORTPATH
5+
testdata/issue13742
6+
7+
IMPORTS
8+
go/ast
9+
10+
FILENAMES
11+
testdata/issue13742.go
12+
13+
FUNCTIONS
14+
// Both F0 and G0 should appear as functions.
15+
func F0(Node)
16+
17+
// Both F1 and G1 should appear as functions.
18+
func F1(ast.Node)
19+
20+
//
21+
func G0() Node
22+
23+
//
24+
func G1() ast.Node
25+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
PACKAGE issue13742
3+
4+
IMPORTPATH
5+
testdata/issue13742
6+
7+
IMPORTS
8+
go/ast
9+
10+
FILENAMES
11+
testdata/issue13742.go
12+
13+
FUNCTIONS
14+
// Both F0 and G0 should appear as functions.
15+
func F0(Node)
16+
17+
// Both F1 and G1 should appear as functions.
18+
func F1(ast.Node)
19+
20+
//
21+
func G0() Node
22+
23+
//
24+
func G1() ast.Node
25+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
PACKAGE issue13742
3+
4+
IMPORTPATH
5+
testdata/issue13742
6+
7+
IMPORTS
8+
go/ast
9+
10+
FILENAMES
11+
testdata/issue13742.go
12+
13+
FUNCTIONS
14+
// Both F0 and G0 should appear as functions.
15+
func F0(Node)
16+
17+
// Both F1 and G1 should appear as functions.
18+
func F1(ast.Node)
19+
20+
//
21+
func G0() Node
22+
23+
//
24+
func G1() ast.Node
25+

src/go/doc/testdata/issue13742.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package issue13742
6+
7+
import (
8+
"go/ast"
9+
. "go/ast"
10+
)
11+
12+
// Both F0 and G0 should appear as functions.
13+
func F0(Node) {}
14+
func G0() Node { return nil }
15+
16+
// Both F1 and G1 should appear as functions.
17+
func F1(ast.Node) {}
18+
func G1() ast.Node { return nil }

0 commit comments

Comments
 (0)