Commit 0504cd68 authored by elpinal's avatar elpinal Committed by Robert Griesemer

cmd/doc: suppress the error message for *package.ident embedded in struct type

The current implementation prints a log, "invalid program: unexpected
type for embedded field", when the form *package.ident is embedded in
a struct declaration.

Note that since valid qualified identifiers must be exported, the result
for a valid program does not change.

Change-Id: If8b9d7056c56b6a6c5482eb749168a63c65ef685
Reviewed-on: https://go-review.googlesource.com/84436Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 9c64c65d
...@@ -292,6 +292,7 @@ var tests = []test{ ...@@ -292,6 +292,7 @@ var tests = []test{
`unexportedField.*int.*Comment on line with unexported field.`, `unexportedField.*int.*Comment on line with unexported field.`,
`ExportedEmbeddedType.*Comment on line with exported embedded field.`, `ExportedEmbeddedType.*Comment on line with exported embedded field.`,
`\*ExportedEmbeddedType.*Comment on line with exported embedded \*field.`, `\*ExportedEmbeddedType.*Comment on line with exported embedded \*field.`,
`\*qualified.ExportedEmbeddedType.*Comment on line with exported embedded \*selector.field.`,
`unexportedType.*Comment on line with unexported embedded field.`, `unexportedType.*Comment on line with unexported embedded field.`,
`\*unexportedType.*Comment on line with unexported embedded \*field.`, `\*unexportedType.*Comment on line with unexported embedded \*field.`,
`io.Reader.*Comment on line with embedded Reader.`, `io.Reader.*Comment on line with embedded Reader.`,
......
...@@ -702,9 +702,16 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis ...@@ -702,9 +702,16 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
for _, field := range fields.List { for _, field := range fields.List {
names := field.Names names := field.Names
if len(names) == 0 { if len(names) == 0 {
// Embedded type. Use the name of the type. It must be of type ident or *ident. // Embedded type. Use the name of the type. It must be of the form ident or
// pkg.ident (for structs and interfaces), or *ident or *pkg.ident (structs only).
// Nothing else is allowed. // Nothing else is allowed.
switch ident := field.Type.(type) { ty := field.Type
if se, ok := field.Type.(*ast.StarExpr); !isInterface && ok {
// The form *ident or *pkg.ident is only valid on
// embedded types in structs.
ty = se.X
}
switch ident := ty.(type) {
case *ast.Ident: case *ast.Ident:
if isInterface && ident.Name == "error" && ident.Obj == nil { if isInterface && ident.Name == "error" && ident.Obj == nil {
// For documentation purposes, we consider the builtin error // For documentation purposes, we consider the builtin error
...@@ -714,12 +721,6 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis ...@@ -714,12 +721,6 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
continue continue
} }
names = []*ast.Ident{ident} names = []*ast.Ident{ident}
case *ast.StarExpr:
// Must have the form *identifier.
// This is only valid on embedded types in structs.
if ident, ok := ident.X.(*ast.Ident); ok && !isInterface {
names = []*ast.Ident{ident}
}
case *ast.SelectorExpr: case *ast.SelectorExpr:
// An embedded type may refer to a type in another package. // An embedded type may refer to a type in another package.
names = []*ast.Ident{ident.Sel} names = []*ast.Ident{ident.Sel}
......
...@@ -64,6 +64,7 @@ type ExportedType struct { ...@@ -64,6 +64,7 @@ type ExportedType struct {
unexportedField int // Comment on line with unexported field. unexportedField int // Comment on line with unexported field.
ExportedEmbeddedType // Comment on line with exported embedded field. ExportedEmbeddedType // Comment on line with exported embedded field.
*ExportedEmbeddedType // Comment on line with exported embedded *field. *ExportedEmbeddedType // Comment on line with exported embedded *field.
*qualified.ExportedEmbeddedType // Comment on line with exported embedded *selector.field.
unexportedType // Comment on line with unexported embedded field. unexportedType // Comment on line with unexported embedded field.
*unexportedType // Comment on line with unexported embedded *field. *unexportedType // Comment on line with unexported embedded *field.
io.Reader // Comment on line with embedded Reader. io.Reader // Comment on line with embedded Reader.
......
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