go/types,cmd/compile/internal/types2: add String methods

This change adds String methods to the types TypeList, TypeParamList
and Instance. The format is unspecified and is provided merely as
a debugging aid.

Fixes #79287

Change-Id: Ic283ea57bac6465b47b8b81b1fa4590641b639cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/780581
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Alan Donovan 2026-05-20 16:28:45 -04:00 committed by Gopher Robot
parent bbf60f3bbd
commit 91a81e5ae1
6 changed files with 68 additions and 0 deletions

3
api/next/79287.txt Normal file
View file

@ -0,0 +1,3 @@
pkg go/types, method (*TypeList) String() string #79287
pkg go/types, method (*TypeParamList) String() string #79287
pkg go/types, method (Instance) String() string #79287

View file

@ -0,0 +1 @@
<!-- nothing to see here but some String methods -->

View file

@ -437,6 +437,10 @@ type Instance struct {
Type Type
}
func (inst Instance) String() string {
return fmt.Sprintf("%s%s", inst.TypeArgs, inst.Type)
}
// An Initializer describes a package-level variable, or a list of variables in case
// of a multi-valued initialization expression, and the corresponding initialization
// expression.

View file

@ -4,6 +4,8 @@
package types2
import "bytes"
// TypeParamList holds a list of type parameters.
type TypeParamList struct{ tparams []*TypeParam }
@ -24,6 +26,19 @@ func (l *TypeParamList) list() []*TypeParam {
return l.tparams
}
func (l *TypeParamList) String() string {
var buf bytes.Buffer
buf.WriteByte('[')
for i, tparam := range l.tparams {
if i > 0 {
buf.WriteString(", ")
}
WriteType(&buf, tparam, nil)
}
buf.WriteByte(']')
return buf.String()
}
// TypeList holds a list of types.
type TypeList struct{ types []Type }
@ -52,6 +67,19 @@ func (l *TypeList) list() []Type {
return l.types
}
func (l *TypeList) String() string {
var buf bytes.Buffer
buf.WriteByte('[')
for i, t := range l.types {
if i > 0 {
buf.WriteString(", ")
}
WriteType(&buf, t, nil)
}
buf.WriteByte(']')
return buf.String()
}
// ----------------------------------------------------------------------------
// Implementation

View file

@ -445,6 +445,10 @@ type Instance struct {
Type Type
}
func (inst Instance) String() string {
return fmt.Sprintf("%s%s", inst.TypeArgs, inst.Type)
}
// An Initializer describes a package-level variable, or a list of variables in case
// of a multi-valued initialization expression, and the corresponding initialization
// expression.

View file

@ -7,6 +7,8 @@
package types
import "bytes"
// TypeParamList holds a list of type parameters.
type TypeParamList struct{ tparams []*TypeParam }
@ -27,6 +29,19 @@ func (l *TypeParamList) list() []*TypeParam {
return l.tparams
}
func (l *TypeParamList) String() string {
var buf bytes.Buffer
buf.WriteByte('[')
for i, tparam := range l.tparams {
if i > 0 {
buf.WriteString(", ")
}
WriteType(&buf, tparam, nil)
}
buf.WriteByte(']')
return buf.String()
}
// TypeList holds a list of types.
type TypeList struct{ types []Type }
@ -55,6 +70,19 @@ func (l *TypeList) list() []Type {
return l.types
}
func (l *TypeList) String() string {
var buf bytes.Buffer
buf.WriteByte('[')
for i, t := range l.types {
if i > 0 {
buf.WriteString(", ")
}
WriteType(&buf, t, nil)
}
buf.WriteByte(']')
return buf.String()
}
// ----------------------------------------------------------------------------
// Implementation