Commit 5fd6bb4c authored by Larz Conwell's avatar Larz Conwell Committed by Russ Cox

go/doc: hide methods on locally-declared predeclared types

Currently if you declare a type overwriting a predeclared type
and export methods on it they will be exposed in godoc, even
though the type itself is not exported. This corrects that
by making all methods on these types hidden, since that's
the expected output.

Fixes #9860

Change-Id: I14037bdcef1b4bbefcf299a143bac8bf363718e0
Reviewed-on: https://go-review.googlesource.com/20610Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent eee727d0
...@@ -645,7 +645,9 @@ func (r *reader) computeMethodSets() { ...@@ -645,7 +645,9 @@ func (r *reader) computeMethodSets() {
func (r *reader) cleanupTypes() { func (r *reader) cleanupTypes() {
for _, t := range r.types { for _, t := range r.types {
visible := r.isVisible(t.name) visible := r.isVisible(t.name)
if t.decl == nil && (predeclaredTypes[t.name] || visible && (t.isEmbedded || r.hasDotImp)) { predeclared := predeclaredTypes[t.name]
if t.decl == nil && (predeclared || visible && (t.isEmbedded || r.hasDotImp)) {
// t.name is a predeclared type (and was not redeclared in this package), // t.name is a predeclared type (and was not redeclared in this package),
// or it was embedded somewhere but its declaration is missing (because // or it was embedded somewhere but its declaration is missing (because
// the AST is incomplete), or we have a dot-import (and all bets are off): // the AST is incomplete), or we have a dot-import (and all bets are off):
...@@ -660,6 +662,7 @@ func (r *reader) cleanupTypes() { ...@@ -660,6 +662,7 @@ func (r *reader) cleanupTypes() {
r.funcs[name] = f r.funcs[name] = f
} }
// 3) move methods // 3) move methods
if !predeclared {
for name, m := range t.methods { for name, m := range t.methods {
// don't overwrite functions with the same name - drop them // don't overwrite functions with the same name - drop them
if _, found := r.funcs[name]; !found { if _, found := r.funcs[name]; !found {
...@@ -667,6 +670,7 @@ func (r *reader) cleanupTypes() { ...@@ -667,6 +670,7 @@ func (r *reader) cleanupTypes() {
} }
} }
} }
}
// remove types w/o declaration or which are not visible // remove types w/o declaration or which are not visible
if t.decl == nil || !visible { if t.decl == nil || !visible {
delete(r.types, t.name) delete(r.types, t.name)
......
// Package predeclared is a go/doc test for handling of exported ...
PACKAGE predeclared
IMPORTPATH
testdata/predeclared
FILENAMES
testdata/predeclared.go
// Package predeclared is a go/doc test for handling of exported ...
PACKAGE predeclared
IMPORTPATH
testdata/predeclared
FILENAMES
testdata/predeclared.go
TYPES
//
type bool int
// Must not be visible.
func (b bool) String() string
//
type error struct{}
// Must not be visible.
func (e error) Error() string
// Package predeclared is a go/doc test for handling of exported ...
PACKAGE predeclared
IMPORTPATH
testdata/predeclared
FILENAMES
testdata/predeclared.go
// 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 predeclared is a go/doc test for handling of
// exported methods on locally-defined predeclared types.
// See issue 9860.
package predeclared
type error struct{}
// Must not be visible.
func (e error) Error() string {
return ""
}
type bool int
// Must not be visible.
func (b bool) String() string {
return ""
}
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