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>
This commit is contained in:
Nathan Nguyen 2025-12-05 03:25:33 -05:00 committed by Sean Liao
parent 1b291b70df
commit 388eb10f50
3 changed files with 46 additions and 3 deletions

View 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.
{

View 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
}

View 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.
}