From ee566d53adb075c63dc036adb96ba643478a1e00 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 7 Jan 2016 18:40:40 -0800 Subject: [PATCH] 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 --- src/go/doc/reader.go | 19 ++++++++++++------- src/go/doc/testdata/issue13742.0.golden | 25 +++++++++++++++++++++++++ src/go/doc/testdata/issue13742.1.golden | 25 +++++++++++++++++++++++++ src/go/doc/testdata/issue13742.2.golden | 25 +++++++++++++++++++++++++ src/go/doc/testdata/issue13742.go | 18 ++++++++++++++++++ 5 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 src/go/doc/testdata/issue13742.0.golden create mode 100644 src/go/doc/testdata/issue13742.1.golden create mode 100644 src/go/doc/testdata/issue13742.2.golden create mode 100644 src/go/doc/testdata/issue13742.go diff --git a/src/go/doc/reader.go b/src/go/doc/reader.go index f5c02b684d..e4e7b7c1c7 100644 --- a/src/go/doc/reader.go +++ b/src/go/doc/reader.go @@ -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 index 0000000000..8dee9aaa80 --- /dev/null +++ b/src/go/doc/testdata/issue13742.0.golden @@ -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 index 0000000000..8dee9aaa80 --- /dev/null +++ b/src/go/doc/testdata/issue13742.1.golden @@ -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 index 0000000000..8dee9aaa80 --- /dev/null +++ b/src/go/doc/testdata/issue13742.2.golden @@ -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 index 0000000000..dbc19411a6 --- /dev/null +++ b/src/go/doc/testdata/issue13742.go @@ -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 }