1
0
mirror of https://github.com/golang/go synced 2024-11-23 22:20:02 -07:00

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 <rsc@golang.org>
This commit is contained in:
Robert Griesemer 2016-01-07 18:40:40 -08:00
parent d91ec5bb40
commit ee566d53ad
5 changed files with 105 additions and 7 deletions

View 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

25
src/go/doc/testdata/issue13742.0.golden vendored Normal file
View File

@ -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

25
src/go/doc/testdata/issue13742.1.golden vendored Normal file
View File

@ -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

25
src/go/doc/testdata/issue13742.2.golden vendored Normal file
View File

@ -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

18
src/go/doc/testdata/issue13742.go vendored Normal file
View File

@ -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 }