diff options
| author | Nathan Nguyen <nhan13574@gmail.com> | 2025-12-05 03:25:33 -0500 |
|---|---|---|
| committer | Sean Liao <sean@liao.dev> | 2025-12-15 07:32:10 -0800 |
| commit | 388eb10f50f2b693fb68897dd0ddf627b512a885 (patch) | |
| tree | f3612cbd9c52ee2e3a0ac26bfd04800fed194ec1 | |
| parent | 1b291b70dff51732415da5b68debe323704d8e8d (diff) | |
| download | go-388eb10f50f2b693fb68897dd0ddf627b512a885.tar.xz | |
cmd/go: show comparable in go doc interface output
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>
| -rw-r--r-- | src/cmd/go/internal/doc/doc_test.go | 29 | ||||
| -rw-r--r-- | src/cmd/go/internal/doc/pkg.go | 7 | ||||
| -rw-r--r-- | src/cmd/go/internal/doc/testdata/pkg.go | 13 |
3 files changed, 46 insertions, 3 deletions
diff --git a/src/cmd/go/internal/doc/doc_test.go b/src/cmd/go/internal/doc/doc_test.go index f91dcd658f..21b6da149a 100644 --- a/src/cmd/go/internal/doc/doc_test.go +++ b/src/cmd/go/internal/doc/doc_test.go @@ -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. { diff --git a/src/cmd/go/internal/doc/pkg.go b/src/cmd/go/internal/doc/pkg.go index 8020807d4a..3c36d0e05c 100644 --- a/src/cmd/go/internal/doc/pkg.go +++ b/src/cmd/go/internal/doc/pkg.go @@ -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 } diff --git a/src/cmd/go/internal/doc/testdata/pkg.go b/src/cmd/go/internal/doc/testdata/pkg.go index 4d269ff0a2..53b018318f 100644 --- a/src/cmd/go/internal/doc/testdata/pkg.go +++ b/src/cmd/go/internal/doc/testdata/pkg.go @@ -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. +} |
