1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:10:05 -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
import (
"bytes"
"cmd/compile/internal/syntax"
"fmt"
"go/constant"
"strings"
)
// An Error describes a type-checking error; it implements the error interface.
@ -388,7 +388,7 @@ type Initializer struct {
}
func (init *Initializer) String() string {
var buf bytes.Buffer
var buf strings.Builder
for i, lhs := range init.Lhs {
if i > 0 {
buf.WriteString(", ")

View File

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

View File

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

View File

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

View File

@ -338,16 +338,16 @@ func typeParamsString(list []*TypeParam) string {
}
// general case (n > 2)
var b strings.Builder
var buf strings.Builder
for i, tname := range list[:n-1] {
if i > 0 {
b.WriteString(", ")
buf.WriteString(", ")
}
b.WriteString(tname.obj.name)
buf.WriteString(tname.obj.name)
}
b.WriteString(", and ")
b.WriteString(list[n-1].obj.name)
return b.String()
buf.WriteString(", and ")
buf.WriteString(list[n-1].obj.name)
return buf.String()
}
// 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.
func stripAnnotations(s string) string {
var b strings.Builder
var buf strings.Builder
for _, r := range s {
// strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
b.WriteRune(r)
buf.WriteRune(r)
}
}
if b.Len() < len(s) {
return b.String()
if buf.Len() < len(s) {
return buf.String()
}
return s
}

View File

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

View File

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

View File

@ -7,7 +7,6 @@
package types2
import (
"bytes"
"cmd/compile/internal/syntax"
"fmt"
"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.
func (s *Scope) String() string {
var buf bytes.Buffer
var buf strings.Builder
s.WriteTo(&buf, 0, false)
return buf.String()
}

View File

@ -4,7 +4,7 @@
package types2
import "bytes"
import "strings"
// A termlist represents the type set represented by the union
// 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 {
return "∅"
}
var buf bytes.Buffer
var buf strings.Builder
for i, x := range xl {
if i > 0 {
buf.WriteString(termSep)

View File

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

View File

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

View File

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

View File

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

View File

@ -339,16 +339,16 @@ func typeParamsString(list []*TypeParam) string {
}
// general case (n > 2)
var b strings.Builder
var buf strings.Builder
for i, tname := range list[:n-1] {
if i > 0 {
b.WriteString(", ")
buf.WriteString(", ")
}
b.WriteString(tname.obj.name)
buf.WriteString(tname.obj.name)
}
b.WriteString(", and ")
b.WriteString(list[n-1].obj.name)
return b.String()
buf.WriteString(", and ")
buf.WriteString(list[n-1].obj.name)
return buf.String()
}
// 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.
func stripAnnotations(s string) string {
var b strings.Builder
var buf strings.Builder
for _, r := range s {
// strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
b.WriteRune(r)
buf.WriteRune(r)
}
}
if b.Len() < len(s) {
return b.String()
if buf.Len() < len(s) {
return buf.String()
}
return s
}

View File

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

View File

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

View File

@ -7,7 +7,6 @@
package types
import (
"bytes"
"fmt"
"go/token"
"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.
func (s *Scope) String() string {
var buf bytes.Buffer
var buf strings.Builder
s.WriteTo(&buf, 0, false)
return buf.String()
}

View File

@ -4,7 +4,7 @@
package types
import "bytes"
import "strings"
// A termlist represents the type set represented by the union
// 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 {
return "∅"
}
var buf bytes.Buffer
var buf strings.Builder
for i, x := range xl {
if i > 0 {
buf.WriteString(termSep)

View File

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