Skip to content

Commit

Permalink
go/doc: propagate types from unexported constants
Browse files Browse the repository at this point in the history
When constants were declared using unexported constants,
the type information was lost when those constants were filtered out.
This CL propagates the type information of unexported constants
so that it is available for display.

This is a follow-up to CL 144110044, which fixed this problem
specifically for _ constants.

Updates golang#5397.

Change-Id: I3f0c767a4007d88169a5634ab2870deea4e6a740
  • Loading branch information
josharian committed Dec 30, 2014
1 parent fcd61eb commit 4d8be75
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
35 changes: 35 additions & 0 deletions src/go/doc/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func filterIdentList(list []*ast.Ident, blankOk bool) []*ast.Ident {
return list[0:j]
}

// hasExportedOrBlankName reports whether list contains any exported or blank names.
//
func hasExportedOrBlankName(list []*ast.Ident) bool {
for _, x := range list {
if x.IsExported() || x.Name == "_" {
return true
}
}
return false
}

// removeErrorField removes anonymous fields named "error" from an interface.
// This is called when "error" has been determined to be a local name,
// not the predeclared type.
Expand Down Expand Up @@ -166,6 +177,30 @@ func (r *reader) filterSpec(spec ast.Spec, tok token.Token) bool {
}

func (r *reader) filterSpecList(list []ast.Spec, tok token.Token) []ast.Spec {
if tok == token.CONST {
// Propagate any type information that will get lost
// when unexported constants are filtered.
var prevType ast.Expr
for _, spec := range list {
spec := spec.(*ast.ValueSpec)
if spec.Type == nil && prevType != nil {
// copy the type to the current spec
name, _ := baseTypeName(prevType)
spec.Type = &ast.Ident{
NamePos: spec.Pos(),
Name: name,
}
}
if hasExportedOrBlankName(spec.Names) {
// both exported and blank names are preserved
// so there's no need to propagate the type
prevType = nil
} else {
prevType = spec.Type
}
}
}

j := 0
for _, s := range list {
if r.filterSpec(s, tok) {
Expand Down
12 changes: 11 additions & 1 deletion src/go/doc/testdata/blank.0.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ FILENAMES
testdata/blank.go

CONSTANTS
// T constants counting from unexported constants.
const (
C1 T
C2

C3

C4 int
)

// Package constants.
const (
_ int = iota
Expand All @@ -28,7 +38,7 @@ TYPES
//
type T int

// T constants.
// T constants counting from a blank constant.
const (
_ T = iota
T1
Expand Down
14 changes: 13 additions & 1 deletion src/go/doc/testdata/blank.1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ FILENAMES
testdata/blank.go

CONSTANTS
// T constants counting from unexported constants.
const (
tweedledee T = iota
tweedledum
C1
C2
alice
C3
redQueen int = iota
C4
)

// Package constants.
const (
_ int = iota
Expand Down Expand Up @@ -37,7 +49,7 @@ TYPES
//
type T int

// T constants.
// T constants counting from a blank constant.
const (
_ T = iota
T1
Expand Down
12 changes: 11 additions & 1 deletion src/go/doc/testdata/blank.2.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ FILENAMES
testdata/blank.go

CONSTANTS
// T constants counting from unexported constants.
const (
C1 T
C2

C3

C4 int
)

// Package constants.
const (
_ int = iota
Expand All @@ -28,7 +38,7 @@ TYPES
//
type T int

// T constants.
// T constants counting from a blank constant.
const (
_ T = iota
T1
Expand Down
14 changes: 13 additions & 1 deletion src/go/doc/testdata/blank.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ package blank

type T int

// T constants.
// T constants counting from a blank constant.
const (
_ T = iota
T1
T2
)

// T constants counting from unexported constants.
const (
tweedledee T = iota
tweedledum
C1
C2
alice
C3
redQueen int = iota
C4
)

// Package constants.
const (
_ int = iota
Expand Down

0 comments on commit 4d8be75

Please sign in to comment.