]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: suppress the error message for *package.ident embedded in struct type
authorelpinal <6elpinal@gmail.com>
Sat, 16 Dec 2017 06:04:05 +0000 (15:04 +0900)
committerRobert Griesemer <gri@golang.org>
Wed, 20 Dec 2017 19:05:19 +0000 (19:05 +0000)
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/84436
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/doc/doc_test.go
src/cmd/doc/pkg.go
src/cmd/doc/testdata/pkg.go

index ee7c430cbd9a60e87721f5c30f5c2a897e38f5da..07e59a2d3ed69891ffc35a8f837b421c2047289d 100644 (file)
@@ -292,6 +292,7 @@ var tests = []test{
                        `unexportedField.*int.*Comment on line with unexported 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.`,
                        `io.Reader.*Comment on line with embedded Reader.`,
index 99a00c563260ea1e6671ad17d0fc6e391a9be59c..11011de018ae172a22ee2ca4e8be8f39b14b7909 100644 (file)
@@ -702,9 +702,16 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
        for _, field := range fields.List {
                names := field.Names
                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.
-                       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:
                                if isInterface && ident.Name == "error" && ident.Obj == nil {
                                        // For documentation purposes, we consider the builtin error
@@ -714,12 +721,6 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
                                        continue
                                }
                                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:
                                // An embedded type may refer to a type in another package.
                                names = []*ast.Ident{ident.Sel}
index d0995bbf7d2b8d812fca74813bec9cf250bd82f0..bc069939f81833d12e1450b182b543ba66fba56b 100644 (file)
@@ -60,14 +60,15 @@ func internalFunc(a int) bool
 // Comment about exported type.
 type ExportedType struct {
        // Comment before exported field.
-       ExportedField         int // Comment on line with exported 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.
-       unexportedType            // Comment on line with unexported embedded field.
-       *unexportedType           // Comment on line with unexported embedded *field.
-       io.Reader                 // Comment on line with embedded Reader.
-       error                     // Comment on line with embedded error.
+       ExportedField                   int // Comment on line with exported 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.
+       *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.
+       io.Reader                           // Comment on line with embedded Reader.
+       error                               // Comment on line with embedded error.
 }
 
 // Comment about exported method.