1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:16:11 -07:00

go/types, types2: use strings.Builder instead of bytes.Buffer where possible

Also, consistently use declaration: var buf strings.Builder.
We don't change exported signatures to match go/types (where we
can't change the exported signatures for backward-compatibility).

Change-Id: I75350886aa231889ae2fd5c4008dd4be9ed6e09f
Reviewed-on: https://go-review.googlesource.com/c/go/+/428094
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2022-09-02 12:10:24 -07:00 committed by Gopher Robot
parent 7a86ef2ad8
commit 2392b7061c
21 changed files with 58 additions and 69 deletions

View File

@ -24,10 +24,10 @@
package types2 package types2
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"fmt" "fmt"
"go/constant" "go/constant"
"strings"
) )
// An Error describes a type-checking error; it implements the error interface. // An Error describes a type-checking error; it implements the error interface.
@ -388,7 +388,7 @@ type Initializer struct {
} }
func (init *Initializer) String() string { func (init *Initializer) String() string {
var buf bytes.Buffer var buf strings.Builder
for i, lhs := range init.Lhs { for i, lhs := range init.Lhs {
if i > 0 { if i > 0 {
buf.WriteString(", ") buf.WriteString(", ")

View File

@ -5,7 +5,6 @@
package types2_test package types2_test
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"errors" "errors"
"fmt" "fmt"
@ -894,7 +893,7 @@ func TestImplicitsInfo(t *testing.T) {
} }
func predString(tv TypeAndValue) string { func predString(tv TypeAndValue) string {
var buf bytes.Buffer var buf strings.Builder
pred := func(b bool, s string) { pred := func(b bool, s string) {
if b { if b {
if buf.Len() > 0 { if buf.Len() > 0 {

View File

@ -7,7 +7,6 @@
package types2 package types2
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"fmt" "fmt"
"runtime" "runtime"
@ -64,7 +63,7 @@ func (err *error_) msg(qf Qualifier) string {
if err.empty() { if err.empty() {
return "no error" return "no error"
} }
var buf bytes.Buffer var buf strings.Builder
for i := range err.desc { for i := range err.desc {
p := &err.desc[i] p := &err.desc[i]
if i > 0 { if i > 0 {
@ -106,7 +105,7 @@ func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) strin
case syntax.Expr: case syntax.Expr:
arg = syntax.String(a) arg = syntax.String(a)
case []syntax.Expr: case []syntax.Expr:
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('[') buf.WriteByte('[')
for i, x := range a { for i, x := range a {
if i > 0 { if i > 0 {
@ -121,7 +120,7 @@ func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) strin
case Type: case Type:
arg = typeString(a, qf, debug) arg = typeString(a, qf, debug)
case []Type: case []Type:
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('[') buf.WriteByte('[')
for i, x := range a { for i, x := range a {
if i > 0 { if i > 0 {
@ -132,7 +131,7 @@ func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) strin
buf.WriteByte(']') buf.WriteByte(']')
arg = buf.String() arg = buf.String()
case []*TypeParam: case []*TypeParam:
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('[') buf.WriteByte('[')
for i, x := range a { for i, x := range a {
if i > 0 { if i > 0 {
@ -300,15 +299,15 @@ func posFor(at poser) syntax.Pos {
// stripAnnotations removes internal (type) annotations from s. // stripAnnotations removes internal (type) annotations from s.
func stripAnnotations(s string) string { func stripAnnotations(s string) string {
var b strings.Builder var buf strings.Builder
for _, r := range s { for _, r := range s {
// strip #'s and subscript digits // strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
b.WriteRune(r) buf.WriteRune(r)
} }
} }
if b.Len() < len(s) { if buf.Len() < len(s) {
return b.String() return buf.String()
} }
return s return s
} }

View File

@ -17,7 +17,6 @@ package types2_test
// from source, use golang.org/x/tools/go/loader. // from source, use golang.org/x/tools/go/loader.
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"cmd/compile/internal/types2" "cmd/compile/internal/types2"
"fmt" "fmt"
@ -68,7 +67,7 @@ func Unused() { {}; {{ var x int; _ = x }} } // make sure empty block scopes get
// Print the tree of scopes. // Print the tree of scopes.
// For determinism, we redact addresses. // For determinism, we redact addresses.
var buf bytes.Buffer var buf strings.Builder
pkg.Scope().WriteTo(&buf, 0, true) pkg.Scope().WriteTo(&buf, 0, true)
rx := regexp.MustCompile(` 0x[a-fA-F0-9]*`) rx := regexp.MustCompile(` 0x[a-fA-F0-9]*`)
fmt.Println(rx.ReplaceAllString(buf.String(), "")) fmt.Println(rx.ReplaceAllString(buf.String(), ""))
@ -173,7 +172,7 @@ func fib(x int) int {
// fmt.Println("Types and Values of each expression:") // fmt.Println("Types and Values of each expression:")
// items = nil // items = nil
// for expr, tv := range info.Types { // for expr, tv := range info.Types {
// var buf bytes.Buffer // var buf strings.Builder
// posn := expr.Pos() // posn := expr.Pos()
// tvstr := tv.Type.String() // tvstr := tv.Type.String()
// if tv.Value != nil { // if tv.Value != nil {

View File

@ -338,16 +338,16 @@ func typeParamsString(list []*TypeParam) string {
} }
// general case (n > 2) // general case (n > 2)
var b strings.Builder var buf strings.Builder
for i, tname := range list[:n-1] { for i, tname := range list[:n-1] {
if i > 0 { if i > 0 {
b.WriteString(", ") buf.WriteString(", ")
} }
b.WriteString(tname.obj.name) buf.WriteString(tname.obj.name)
} }
b.WriteString(", and ") buf.WriteString(", and ")
b.WriteString(list[n-1].obj.name) buf.WriteString(list[n-1].obj.name)
return b.String() return buf.String()
} }
// isParameterized reports whether typ contains any of the type parameters of tparams. // isParameterized reports whether typ contains any of the type parameters of tparams.

View File

@ -233,15 +233,15 @@ var _ T[int]
// Copied from errors.go. // Copied from errors.go.
func stripAnnotations(s string) string { func stripAnnotations(s string) string {
var b strings.Builder var buf strings.Builder
for _, r := range s { for _, r := range s {
// strip #'s and subscript digits // strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
b.WriteRune(r) buf.WriteRune(r)
} }
} }
if b.Len() < len(s) { if buf.Len() < len(s) {
return b.String() return buf.String()
} }
return s return s
} }

View File

@ -7,7 +7,6 @@
package types2_test package types2_test
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"fmt" "fmt"
"internal/testenv" "internal/testenv"
@ -427,7 +426,7 @@ func TestIssue29029(t *testing.T) {
// printInfo prints the *Func definitions recorded in info, one *Func per line. // printInfo prints the *Func definitions recorded in info, one *Func per line.
printInfo := func(info *Info) string { printInfo := func(info *Info) string {
var buf bytes.Buffer var buf strings.Builder
for _, obj := range info.Defs { for _, obj := range info.Defs {
if fn, ok := obj.(*Func); ok { if fn, ok := obj.(*Func); ok {
fmt.Fprintln(&buf, fn) fmt.Fprintln(&buf, fn)

View File

@ -5,7 +5,6 @@
package types2_test package types2_test
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"cmd/compile/internal/types2" "cmd/compile/internal/types2"
"errors" "errors"
@ -22,7 +21,7 @@ func checkMono(t *testing.T, body string) error {
} }
files := []*syntax.File{file} files := []*syntax.File{file}
var buf bytes.Buffer var buf strings.Builder
conf := types2.Config{ conf := types2.Config{
Error: func(err error) { fmt.Fprintln(&buf, err) }, Error: func(err error) { fmt.Fprintln(&buf, err) },
Importer: defaultImporter(), Importer: defaultImporter(),

View File

@ -7,7 +7,6 @@
package types2 package types2
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"fmt" "fmt"
"io" "io"
@ -233,7 +232,7 @@ func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
// String returns a string representation of the scope, for debugging. // String returns a string representation of the scope, for debugging.
func (s *Scope) String() string { func (s *Scope) String() string {
var buf bytes.Buffer var buf strings.Builder
s.WriteTo(&buf, 0, false) s.WriteTo(&buf, 0, false)
return buf.String() return buf.String()
} }

View File

@ -4,7 +4,7 @@
package types2 package types2
import "bytes" import "strings"
// A termlist represents the type set represented by the union // A termlist represents the type set represented by the union
// t1 y2 ... tn of the type sets of the terms t1 to tn. // t1 y2 ... tn of the type sets of the terms t1 to tn.
@ -25,7 +25,7 @@ func (xl termlist) String() string {
if len(xl) == 0 { if len(xl) == 0 {
return "∅" return "∅"
} }
var buf bytes.Buffer var buf strings.Builder
for i, x := range xl { for i, x := range xl {
if i > 0 { if i > 0 {
buf.WriteString(termSep) buf.WriteString(termSep)

View File

@ -5,10 +5,10 @@
package types2 package types2
import ( import (
"bytes"
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"fmt" "fmt"
"sort" "sort"
"strings"
) )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -71,7 +71,7 @@ func (s *_TypeSet) String() string {
hasMethods := len(s.methods) > 0 hasMethods := len(s.methods) > 0
hasTerms := s.hasTerms() hasTerms := s.hasTerms()
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('{') buf.WriteByte('{')
if s.comparable { if s.comparable {
buf.WriteString("comparable") buf.WriteString("comparable")

View File

@ -5,7 +5,6 @@
package types_test package types_test
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"go/ast" "go/ast"
@ -896,7 +895,7 @@ func TestImplicitsInfo(t *testing.T) {
} }
func predString(tv TypeAndValue) string { func predString(tv TypeAndValue) string {
var buf bytes.Buffer var buf strings.Builder
pred := func(b bool, s string) { pred := func(b bool, s string) {
if b { if b {
if buf.Len() > 0 { if buf.Len() > 0 {

View File

@ -62,7 +62,7 @@ func (err *error_) msg(fset *token.FileSet, qf Qualifier) string {
if err.empty() { if err.empty() {
return "no error" return "no error"
} }
var buf bytes.Buffer var buf strings.Builder
for i := range err.desc { for i := range err.desc {
p := &err.desc[i] p := &err.desc[i]
if i > 0 { if i > 0 {
@ -164,7 +164,7 @@ func sprintf(fset *token.FileSet, qf Qualifier, debug bool, format string, args
case Type: case Type:
arg = typeString(a, qf, debug) arg = typeString(a, qf, debug)
case []Type: case []Type:
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('[') buf.WriteByte('[')
for i, x := range a { for i, x := range a {
if i > 0 { if i > 0 {
@ -175,7 +175,7 @@ func sprintf(fset *token.FileSet, qf Qualifier, debug bool, format string, args
buf.WriteByte(']') buf.WriteByte(']')
arg = buf.String() arg = buf.String()
case []*TypeParam: case []*TypeParam:
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('[') buf.WriteByte('[')
for i, x := range a { for i, x := range a {
if i > 0 { if i > 0 {
@ -370,15 +370,15 @@ func spanOf(at positioner) posSpan {
// stripAnnotations removes internal (type) annotations from s. // stripAnnotations removes internal (type) annotations from s.
func stripAnnotations(s string) string { func stripAnnotations(s string) string {
var b strings.Builder var buf strings.Builder
for _, r := range s { for _, r := range s {
// strip #'s and subscript digits // strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
b.WriteRune(r) buf.WriteRune(r)
} }
} }
if b.Len() < len(s) { if buf.Len() < len(s) {
return b.String() return buf.String()
} }
return s return s
} }

View File

@ -16,7 +16,6 @@ package types_test
// from source, use golang.org/x/tools/go/loader. // from source, use golang.org/x/tools/go/loader.
import ( import (
"bytes"
"fmt" "fmt"
"go/ast" "go/ast"
"go/format" "go/format"
@ -72,7 +71,7 @@ func Unused() { {}; {{ var x int; _ = x }} } // make sure empty block scopes get
// Print the tree of scopes. // Print the tree of scopes.
// For determinism, we redact addresses. // For determinism, we redact addresses.
var buf bytes.Buffer var buf strings.Builder
pkg.Scope().WriteTo(&buf, 0, true) pkg.Scope().WriteTo(&buf, 0, true)
rx := regexp.MustCompile(` 0x[a-fA-F0-9]*`) rx := regexp.MustCompile(` 0x[a-fA-F0-9]*`)
fmt.Println(rx.ReplaceAllString(buf.String(), "")) fmt.Println(rx.ReplaceAllString(buf.String(), ""))
@ -233,7 +232,7 @@ func fib(x int) int {
fmt.Println("Types and Values of each expression:") fmt.Println("Types and Values of each expression:")
items = nil items = nil
for expr, tv := range info.Types { for expr, tv := range info.Types {
var buf bytes.Buffer var buf strings.Builder
posn := fset.Position(expr.Pos()) posn := fset.Position(expr.Pos())
tvstr := tv.Type.String() tvstr := tv.Type.String()
if tv.Value != nil { if tv.Value != nil {
@ -328,7 +327,7 @@ func mode(tv types.TypeAndValue) string {
} }
func exprString(fset *token.FileSet, expr ast.Expr) string { func exprString(fset *token.FileSet, expr ast.Expr) string {
var buf bytes.Buffer var buf strings.Builder
format.Node(&buf, fset, expr) format.Node(&buf, fset, expr)
return buf.String() return buf.String()
} }

View File

@ -339,16 +339,16 @@ func typeParamsString(list []*TypeParam) string {
} }
// general case (n > 2) // general case (n > 2)
var b strings.Builder var buf strings.Builder
for i, tname := range list[:n-1] { for i, tname := range list[:n-1] {
if i > 0 { if i > 0 {
b.WriteString(", ") buf.WriteString(", ")
} }
b.WriteString(tname.obj.name) buf.WriteString(tname.obj.name)
} }
b.WriteString(", and ") buf.WriteString(", and ")
b.WriteString(list[n-1].obj.name) buf.WriteString(list[n-1].obj.name)
return b.String() return buf.String()
} }
// isParameterized reports whether typ contains any of the type parameters of tparams. // isParameterized reports whether typ contains any of the type parameters of tparams.

View File

@ -239,15 +239,15 @@ var _ T[int]
// Copied from errors.go. // Copied from errors.go.
func stripAnnotations(s string) string { func stripAnnotations(s string) string {
var b strings.Builder var buf strings.Builder
for _, r := range s { for _, r := range s {
// strip #'s and subscript digits // strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
b.WriteRune(r) buf.WriteRune(r)
} }
} }
if b.Len() < len(s) { if buf.Len() < len(s) {
return b.String() return buf.String()
} }
return s return s
} }

View File

@ -7,7 +7,6 @@
package types_test package types_test
import ( import (
"bytes"
"fmt" "fmt"
"go/ast" "go/ast"
"go/importer" "go/importer"
@ -429,7 +428,7 @@ func TestIssue29029(t *testing.T) {
// printInfo prints the *Func definitions recorded in info, one *Func per line. // printInfo prints the *Func definitions recorded in info, one *Func per line.
printInfo := func(info *Info) string { printInfo := func(info *Info) string {
var buf bytes.Buffer var buf strings.Builder
for _, obj := range info.Defs { for _, obj := range info.Defs {
if fn, ok := obj.(*Func); ok { if fn, ok := obj.(*Func); ok {
fmt.Fprintln(&buf, fn) fmt.Fprintln(&buf, fn)

View File

@ -5,7 +5,6 @@
package types_test package types_test
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"go/ast" "go/ast"
@ -25,7 +24,7 @@ func checkMono(t *testing.T, body string) error {
} }
files := []*ast.File{file} files := []*ast.File{file}
var buf bytes.Buffer var buf strings.Builder
conf := types.Config{ conf := types.Config{
Error: func(err error) { fmt.Fprintln(&buf, err) }, Error: func(err error) { fmt.Fprintln(&buf, err) },
Importer: importer.Default(), Importer: importer.Default(),

View File

@ -7,7 +7,6 @@
package types package types
import ( import (
"bytes"
"fmt" "fmt"
"go/token" "go/token"
"io" "io"
@ -233,7 +232,7 @@ func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
// String returns a string representation of the scope, for debugging. // String returns a string representation of the scope, for debugging.
func (s *Scope) String() string { func (s *Scope) String() string {
var buf bytes.Buffer var buf strings.Builder
s.WriteTo(&buf, 0, false) s.WriteTo(&buf, 0, false)
return buf.String() return buf.String()
} }

View File

@ -4,7 +4,7 @@
package types package types
import "bytes" import "strings"
// A termlist represents the type set represented by the union // A termlist represents the type set represented by the union
// t1 y2 ... tn of the type sets of the terms t1 to tn. // t1 y2 ... tn of the type sets of the terms t1 to tn.
@ -25,7 +25,7 @@ func (xl termlist) String() string {
if len(xl) == 0 { if len(xl) == 0 {
return "∅" return "∅"
} }
var buf bytes.Buffer var buf strings.Builder
for i, x := range xl { for i, x := range xl {
if i > 0 { if i > 0 {
buf.WriteString(termSep) buf.WriteString(termSep)

View File

@ -5,10 +5,10 @@
package types package types
import ( import (
"bytes"
"fmt" "fmt"
"go/token" "go/token"
"sort" "sort"
"strings"
) )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -71,7 +71,7 @@ func (s *_TypeSet) String() string {
hasMethods := len(s.methods) > 0 hasMethods := len(s.methods) > 0
hasTerms := s.hasTerms() hasTerms := s.hasTerms()
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('{') buf.WriteByte('{')
if s.comparable { if s.comparable {
buf.WriteString("comparable") buf.WriteString("comparable")