Commit ee566d53 authored by Robert Griesemer's avatar Robert Griesemer

go/doc: don't drop "factory" functions with dot-imported result types

Fixes #13742.

Change-Id: I7c8b51b60e31402bf708bf8d70e07fd06295e8ce
Reviewed-on: https://go-review.googlesource.com/18393Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent d91ec5bb
......@@ -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
......
//
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
//
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
//
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
// 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 }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment