From 388eb10f50f2b693fb68897dd0ddf627b512a885 Mon Sep 17 00:00:00 2001 From: Nathan Nguyen Date: Fri, 5 Dec 2025 03:25:33 -0500 Subject: [PATCH] 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 Reviewed-by: David Chase Reviewed-by: Michael Matloob Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/doc/doc_test.go | 29 +++++++++++++++++++++++++ src/cmd/go/internal/doc/pkg.go | 7 +++--- 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. +}