]> git.feebdaed.xyz Git - 0xmirror/go.git/commitdiff
cmd/go: show comparable in go doc interface output
authorNathan Nguyen <nhan13574@gmail.com>
Fri, 5 Dec 2025 08:25:33 +0000 (03:25 -0500)
committerSean Liao <sean@liao.dev>
Mon, 15 Dec 2025 15:32:10 +0000 (07:32 -0800)
The go doc command now displays the comparable constraint when embedded
in an interface, matching the existing behavior for the error type.

Previously, when an interface embedded comparable, it was not shown in
the documentation and incorrectly triggered the "Has unexported methods"
message even when no unexported methods existed.

Fixes #76125

Change-Id: Idaae07fcb1dfc79b1fae374f9fc68df0052ff38e
Reviewed-on: https://go-review.googlesource.com/c/go/+/727100
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

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

index f91dcd658f247b6b966d9eaa77f7c2e60629de90..21b6da149a6d2f82722d38f32d8c55c5746c41dd 100644 (file)
@@ -629,6 +629,35 @@ var tests = []test{
                        `Has unexported methods`,
                },
        },
+       // Interface with comparable constraint.
+       {
+               "interface type with comparable",
+               []string{p, `ExportedComparableInterface`},
+               []string{
+                       `Comment about exported interface with comparable`, // Include comment.
+                       `type ExportedComparableInterface interface`,       // Interface definition.
+                       `comparable.*Comment on line with comparable`,      // Comparable should be shown.
+                       `ExportedMethod\(\).*Comment on line with exported method`,
+                       `Has unexported methods`,
+               },
+               []string{
+                       `unexportedMethod`, // No unexported method.
+               },
+       },
+       // Interface with only comparable (no unexported methods).
+       {
+               "interface type with comparable only",
+               []string{p, `ExportedComparableOnlyInterface`},
+               []string{
+                       `ExportedComparableOnlyInterface has only comparable`, // Include comment.
+                       `type ExportedComparableOnlyInterface interface`,      // Interface definition.
+                       `comparable.*Comment on line with comparable`,         // Comparable should be shown.
+                       `ExportedMethod\(\).*Comment on line with exported method`,
+               },
+               []string{
+                       `Has unexported methods`, // Should NOT appear - no unexported methods.
+               },
+       },
 
        // Interface method.
        {
index 8020807d4a3f777cd3f0afa6024ec6c6ff86a058..3c36d0e05cfffcc0c072417def01b82cb66d0564 100644 (file)
@@ -947,10 +947,11 @@ func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldLis
                        constraint := false
                        switch ident := ty.(type) {
                        case *ast.Ident:
-                               if isInterface && ident.Name == "error" && ident.Obj == nil {
+                               if isInterface && ident.Obj == nil &&
+                                       (ident.Name == "error" || ident.Name == "comparable") {
                                        // For documentation purposes, we consider the builtin error
-                                       // type special when embedded in an interface, such that it
-                                       // always gets shown publicly.
+                                       // and comparable types special when embedded in an interface,
+                                       // such that they always get shown publicly.
                                        list = append(list, field)
                                        continue
                                }
index 4d269ff0a2295908a7df7313b25ac7b88bdb2566..53b018318f3cc738a0025fdacb15a8b69402306a 100644 (file)
@@ -252,3 +252,16 @@ type TildeConstraint interface {
 type StructConstraint interface {
        struct { F int }
 }
+
+// Comment about exported interface with comparable.
+type ExportedComparableInterface interface {
+       comparable         // Comment on line with comparable.
+       ExportedMethod()   // Comment on line with exported method.
+       unexportedMethod() // Comment on line with unexported method.
+}
+
+// ExportedComparableOnlyInterface has only comparable and exported method (no unexported).
+type ExportedComparableOnlyInterface interface {
+       comparable       // Comment on line with comparable.
+       ExportedMethod() // Comment on line with exported method.
+}