]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: don't log on constraint type elements
authorIan Lance Taylor <iant@golang.org>
Sat, 18 Dec 2021 23:54:38 +0000 (15:54 -0800)
committerIan Lance Taylor <iant@golang.org>
Mon, 20 Dec 2021 23:32:14 +0000 (23:32 +0000)
Fixes #50256

Change-Id: I2327a0b28f8173c801ed2946bec8083967667027
Reviewed-on: https://go-review.googlesource.com/c/go/+/373314
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/doc/doc_test.go
src/cmd/doc/pkg.go
src/cmd/doc/testdata/pkg.go

index af7793133ef68bd0d064ee75a1f4e6f80e9396c5..0ff9edcde356be227b3e6f8f7412254983076b73 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "bytes"
        "flag"
+       "log"
        "os"
        "path/filepath"
        "regexp"
@@ -125,6 +126,9 @@ var tests = []test{
                        `func MultiLineFunc\(x interface{ ... }\) \(r struct{ ... }\)`, // Multi line function.
                        `var LongLine = newLongLine\(("someArgument[1-4]", ){4}...\)`,  // Long list of arguments.
                        `type T1 = T2`,                                                 // Type alias
+                       `type SimpleConstraint interface{ ... }`,
+                       `type TildeConstraint interface{ ... }`,
+                       `type StructConstraint interface{ ... }`,
                },
                []string{
                        `const internalConstant = 2`,       // No internal constants.
@@ -199,6 +203,9 @@ var tests = []test{
                        `Comment about exported method`,
                        `type T1 = T2`,
                        `type T2 int`,
+                       `type SimpleConstraint interface {`,
+                       `type TildeConstraint interface {`,
+                       `type StructConstraint interface {`,
                },
                []string{
                        `constThree`,
@@ -822,13 +829,19 @@ var tests = []test{
 
 func TestDoc(t *testing.T) {
        maybeSkip(t)
+       defer log.SetOutput(log.Writer())
        for _, test := range tests {
                var b bytes.Buffer
                var flagSet flag.FlagSet
+               var logbuf bytes.Buffer
+               log.SetOutput(&logbuf)
                err := do(&b, &flagSet, test.args)
                if err != nil {
                        t.Fatalf("%s %v: %s\n", test.name, test.args, err)
                }
+               if logbuf.Len() > 0 {
+                       t.Errorf("%s %v: unexpected log messages:\n%s", test.name, test.args, logbuf.Bytes())
+               }
                output := b.Bytes()
                failed := false
                for j, yes := range test.yes {
index f51efe08af5c1ab35055f92aff6b466484ba52c1..02666007300f8f24302812d15635fe80b9349f47 100644 (file)
@@ -865,6 +865,7 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
                if len(names) == 0 {
                        // 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).
+                       // Or a type embedded in a constraint.
                        // Nothing else is allowed.
                        ty := field.Type
                        if se, ok := field.Type.(*ast.StarExpr); !isInterface && ok {
@@ -872,6 +873,7 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
                                // embedded types in structs.
                                ty = se.X
                        }
+                       constraint := false
                        switch ident := ty.(type) {
                        case *ast.Ident:
                                if isInterface && ident.Name == "error" && ident.Obj == nil {
@@ -885,8 +887,12 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
                        case *ast.SelectorExpr:
                                // An embedded type may refer to a type in another package.
                                names = []*ast.Ident{ident.Sel}
+                       default:
+                               // An approximation or union or type
+                               // literal in an interface.
+                               constraint = true
                        }
-                       if names == nil {
+                       if names == nil && !constraint {
                                // Can only happen if AST is incorrect. Safe to continue with a nil list.
                                log.Print("invalid program: unexpected type for embedded field")
                        }
index 5ece8325651ebad4d2e39af1e67e1b8272992624..a693c7491897996ff0219f532a4ff3cc9379dfd1 100644 (file)
@@ -238,3 +238,15 @@ type ExportedFormattedType struct {
        // Text after pre-formatted block.
        ExportedField int
 }
+
+type SimpleConstraint interface {
+       ~int | ~float64
+}
+
+type TildeConstraint interface {
+       ~int
+}
+
+type StructConstraint interface {
+       struct { F int }
+}