diff options
author | Robert Griesemer <gri@golang.org> | 2022-01-25 09:21:39 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2022-01-26 22:09:36 +0000 |
commit | ca6a5c0d518463b40c3e6e56655d45d9fd60e1f7 (patch) | |
tree | fa970672eae6ca611c0ece6c9992a5ab38e7b456 | |
parent | c8b0dcea4a3e67289ccf985b10616200817cca86 (diff) | |
download | go-ca6a5c0d518463b40c3e6e56655d45d9fd60e1f7.tar.gz go-ca6a5c0d518463b40c3e6e56655d45d9fd60e1f7.zip |
go/types, types2: print underlying type of comparable as "interface{comparable}"
For #50791.
Change-Id: Ib12009d2895146e55ec3a51aa8ceafe58dfd82a5
Reviewed-on: https://go-review.googlesource.com/c/go/+/380754
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
-rw-r--r-- | src/cmd/compile/internal/types2/typestring.go | 18 | ||||
-rw-r--r-- | src/cmd/compile/internal/types2/typestring_test.go | 4 | ||||
-rw-r--r-- | src/go/types/typestring.go | 18 | ||||
-rw-r--r-- | src/go/types/typestring_test.go | 5 |
4 files changed, 33 insertions, 12 deletions
diff --git a/src/cmd/compile/internal/types2/typestring.go b/src/cmd/compile/internal/types2/typestring.go index 4d03eba657..ada0529929 100644 --- a/src/cmd/compile/internal/types2/typestring.go +++ b/src/cmd/compile/internal/types2/typestring.go @@ -201,12 +201,18 @@ func (w *typeWriter) typ(typ Type) { } case *Interface: - if t == universeAny.Type() && w.ctxt == nil { - // When not hashing, we can try to improve type strings by writing "any" - // for a type that is pointer-identical to universeAny. This logic should - // be deprecated by more robust handling for aliases. - w.string("any") - break + if w.ctxt == nil { + if t == universeAny.Type() { + // When not hashing, we can try to improve type strings by writing "any" + // for a type that is pointer-identical to universeAny. This logic should + // be deprecated by more robust handling for aliases. + w.string("any") + break + } + if t == universeComparable.Type().(*Named).underlying { + w.string("interface{comparable}") + break + } } if t.implicit { if len(t.methods) == 0 && len(t.embeddeds) == 1 { diff --git a/src/cmd/compile/internal/types2/typestring_test.go b/src/cmd/compile/internal/types2/typestring_test.go index eda6835588..c0689e866c 100644 --- a/src/cmd/compile/internal/types2/typestring_test.go +++ b/src/cmd/compile/internal/types2/typestring_test.go @@ -93,6 +93,10 @@ var independentTestTypes = []testEntry{ dup(`interface{String() string; m(int) float32}`), dup("interface{int|float32|complex128}"), dup("interface{int|~float32|~complex128}"), + dup("any"), + dup("interface{comparable}"), + {"comparable", "interface{comparable}"}, + {"error", "interface{Error() string}"}, // maps dup("map[string]int"), diff --git a/src/go/types/typestring.go b/src/go/types/typestring.go index cf86f9f720..80210a2f34 100644 --- a/src/go/types/typestring.go +++ b/src/go/types/typestring.go @@ -202,12 +202,18 @@ func (w *typeWriter) typ(typ Type) { } case *Interface: - if t == universeAny.Type() && w.ctxt == nil { - // When not hashing, we can try to improve type strings by writing "any" - // for a type that is pointer-identical to universeAny. This logic should - // be deprecated by more robust handling for aliases. - w.string("any") - break + if w.ctxt == nil { + if t == universeAny.Type() { + // When not hashing, we can try to improve type strings by writing "any" + // for a type that is pointer-identical to universeAny. This logic should + // be deprecated by more robust handling for aliases. + w.string("any") + break + } + if t == universeComparable.Type().(*Named).underlying { + w.string("interface{comparable}") + break + } } if t.implicit { if len(t.methods) == 0 && len(t.embeddeds) == 1 { diff --git a/src/go/types/typestring_test.go b/src/go/types/typestring_test.go index 14ab9b6002..b7b843516e 100644 --- a/src/go/types/typestring_test.go +++ b/src/go/types/typestring_test.go @@ -97,6 +97,11 @@ var independentTestTypes = []testEntry{ dup(`interface{String() string; m(int) float32}`), dup("interface{int|float32|complex128}"), dup("interface{int|~float32|~complex128}"), + dup("any"), + dup("interface{comparable}"), + // TODO(gri) adjust test for EvalCompositeTest + // {"comparable", "interface{comparable}"}, + // {"error", "interface{Error() string}"}, // maps dup("map[string]int"), |