Skip to content

Commit

Permalink
all: gofmt -w -r 'interface{} -> any' src
Browse files Browse the repository at this point in the history
And then revert the bootstrap cmd directories and certain testdata.
And adjust tests as needed.

Not reverting the changes in std that are bootstrapped,
because some of those changes would appear in API docs,
and we want to use any consistently.
Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories
when preparing the bootstrap copy.

A few files changed as a result of running gofmt -w
not because of interface{} -> any but because they
hadn't been updated for the new //go:build lines.

Fixes #49884.

Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09
Reviewed-on: https://go-review.googlesource.com/c/go/+/368254
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
rsc committed Dec 13, 2021
1 parent 083ef54 commit 2580d0e
Show file tree
Hide file tree
Showing 445 changed files with 1,906 additions and 1,875 deletions.
2 changes: 1 addition & 1 deletion src/archive/tar/common.go
Expand Up @@ -538,7 +538,7 @@ type headerFileInfo struct {
func (fi headerFileInfo) Size() int64 { return fi.h.Size }
func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
func (fi headerFileInfo) ModTime() time.Time { return fi.h.ModTime }
func (fi headerFileInfo) Sys() interface{} { return fi.h }
func (fi headerFileInfo) Sys() any { return fi.h }

// Name returns the base name of the file.
func (fi headerFileInfo) Name() string {
Expand Down
4 changes: 2 additions & 2 deletions src/archive/tar/reader_test.go
Expand Up @@ -1363,7 +1363,7 @@ func TestFileReader(t *testing.T) {
wantLCnt int64
wantPCnt int64
}
testFnc interface{} // testRead | testWriteTo | testRemaining
testFnc any // testRead | testWriteTo | testRemaining
)

type (
Expand All @@ -1376,7 +1376,7 @@ func TestFileReader(t *testing.T) {
spd sparseDatas
size int64
}
fileMaker interface{} // makeReg | makeSparse
fileMaker any // makeReg | makeSparse
)

vectors := []struct {
Expand Down
2 changes: 1 addition & 1 deletion src/archive/tar/tar_test.go
Expand Up @@ -23,7 +23,7 @@ import (

type testError struct{ error }

type fileOps []interface{} // []T where T is (string | int64)
type fileOps []any // []T where T is (string | int64)

// testFile is an io.ReadWriteSeeker where the IO operations performed
// on it must match the list of operations in ops.
Expand Down
6 changes: 3 additions & 3 deletions src/archive/tar/writer_test.go
Expand Up @@ -67,7 +67,7 @@ func TestWriter(t *testing.T) {
testClose struct { // Close() == wantErr
wantErr error
}
testFnc interface{} // testHeader | testWrite | testReadFrom | testClose
testFnc any // testHeader | testWrite | testReadFrom | testClose
)

vectors := []struct {
Expand Down Expand Up @@ -1031,7 +1031,7 @@ func TestFileWriter(t *testing.T) {
wantLCnt int64
wantPCnt int64
}
testFnc interface{} // testWrite | testReadFrom | testRemaining
testFnc any // testWrite | testReadFrom | testRemaining
)

type (
Expand All @@ -1044,7 +1044,7 @@ func TestFileWriter(t *testing.T) {
sph sparseHoles
size int64
}
fileMaker interface{} // makeReg | makeSparse
fileMaker any // makeReg | makeSparse
)

vectors := []struct {
Expand Down
2 changes: 1 addition & 1 deletion src/archive/zip/reader.go
Expand Up @@ -670,7 +670,7 @@ func (f *fileListEntry) Size() int64 { return 0 }
func (f *fileListEntry) Mode() fs.FileMode { return fs.ModeDir | 0555 }
func (f *fileListEntry) Type() fs.FileMode { return fs.ModeDir }
func (f *fileListEntry) IsDir() bool { return true }
func (f *fileListEntry) Sys() interface{} { return nil }
func (f *fileListEntry) Sys() any { return nil }

func (f *fileListEntry) ModTime() time.Time {
if f.file == nil {
Expand Down
2 changes: 1 addition & 1 deletion src/archive/zip/struct.go
Expand Up @@ -163,7 +163,7 @@ func (fi headerFileInfo) ModTime() time.Time {
}
func (fi headerFileInfo) Mode() fs.FileMode { return fi.fh.Mode() }
func (fi headerFileInfo) Type() fs.FileMode { return fi.fh.Mode().Type() }
func (fi headerFileInfo) Sys() interface{} { return fi.fh }
func (fi headerFileInfo) Sys() any { return fi.fh }

func (fi headerFileInfo) Info() (fs.FileInfo, error) { return fi, nil }

Expand Down
4 changes: 2 additions & 2 deletions src/builtin/builtin.go
Expand Up @@ -239,7 +239,7 @@ func close(c chan<- Type)
// that point, the program is terminated with a non-zero exit code. This
// termination sequence is called panicking and can be controlled by the
// built-in function recover.
func panic(v interface{})
func panic(v any)

// The recover built-in function allows a program to manage behavior of a
// panicking goroutine. Executing a call to recover inside a deferred
Expand All @@ -250,7 +250,7 @@ func panic(v interface{})
// panicking, or if the argument supplied to panic was nil, recover returns
// nil. Thus the return value from recover reports whether the goroutine is
// panicking.
func recover() interface{}
func recover() any

// The print built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
Expand Down
2 changes: 1 addition & 1 deletion src/bytes/reader_test.go
Expand Up @@ -76,7 +76,7 @@ func TestReaderAt(t *testing.T) {
off int64
n int
want string
wanterr interface{}
wanterr any
}{
{0, 10, "0123456789", nil},
{1, 10, "123456789", io.EOF},
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/api/goapi.go
Expand Up @@ -1071,7 +1071,7 @@ func (w *Walker) emitMethod(m *types.Selection) {
w.emitf("method (%s%s) %s%s", w.typeString(recv), tps, m.Obj().Name(), w.signatureString(sig))
}

func (w *Walker) emitf(format string, args ...interface{}) {
func (w *Walker) emitf(format string, args ...any) {
f := strings.Join(w.scope, ", ") + ", " + fmt.Sprintf(format, args...)
if strings.Contains(f, "\n") {
panic("feature contains newlines: " + f)
Expand Down
1 change: 1 addition & 0 deletions src/cmd/api/testdata/src/issue21181/p/p_generic.go
@@ -1,3 +1,4 @@
//go:build !amd64
// +build !amd64

package p
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/api/testdata/src/pkg/p1/p1.go
Expand Up @@ -197,7 +197,7 @@ var m map[string]int

var chanVar chan int

var ifaceVar interface{} = 5
var ifaceVar any = 5

var assertVar = ifaceVar.(int)

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/api/testdata/src/pkg/p4/p4.go
Expand Up @@ -4,12 +4,12 @@

package p4

type Pair[T1 interface { M() }, T2 ~int] struct {
type Pair[T1 interface{ M() }, T2 ~int] struct {
f1 T1
f2 T2
}

func NewPair[T1 interface { M() }, T2 ~int](v1 T1, v2 T2) Pair[T1, T2] {
func NewPair[T1 interface{ M() }, T2 ~int](v1 T1, v2 T2) Pair[T1, T2] {
return Pair[T1, T2]{f1: v1, f2: v2}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/importer/gcimporter_test.go
Expand Up @@ -258,7 +258,7 @@ var importedObjectTests = []struct {
{"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string)"},

// interfaces
{"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"},
{"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key any) any}"},
{"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"},
{"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"},
{"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/cover/testdata/test.go
Expand Up @@ -151,7 +151,7 @@ func testSwitch() {
}

func testTypeSwitch() {
var x = []interface{}{1, 2.0, "hi"}
var x = []any{1, 2.0, "hi"}
for _, v := range x {
switch func() { check(LINE, 3) }(); v.(type) {
case int:
Expand Down Expand Up @@ -215,7 +215,7 @@ func testEmptySwitches() {
switch 3 {
}
check(LINE, 1)
switch i := (interface{})(3).(int); i {
switch i := (any)(3).(int); i {
}
check(LINE, 1)
c := make(chan int)
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/dist/buildtool.go
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
)
Expand Down Expand Up @@ -288,7 +289,11 @@ func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
}

func bootstrapFixImports(srcFile string) string {
lines := strings.SplitAfter(readfile(srcFile), "\n")
text := readfile(srcFile)
if !strings.Contains(srcFile, "/cmd/") && !strings.Contains(srcFile, `\cmd\`) {
text = regexp.MustCompile(`\bany\b`).ReplaceAllString(text, "interface{}")
}
lines := strings.SplitAfter(text, "\n")
inBlock := false
for i, line := range lines {
if strings.HasPrefix(line, "import (") {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/doc/pkg.go
Expand Up @@ -122,7 +122,7 @@ func trim(path, prefix string) (string, bool) {
// main do function, so it doesn't cause an exit. Allows testing to work
// without running a subprocess. The log prefix will be added when
// logged in main; it is not added here.
func (pkg *Package) Fatalf(format string, args ...interface{}) {
func (pkg *Package) Fatalf(format string, args ...any) {
panic(PackageError(fmt.Sprintf(format, args...)))
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func parsePackage(writer io.Writer, pkg *build.Package, userPath string) *Packag
return p
}

func (pkg *Package) Printf(format string, args ...interface{}) {
func (pkg *Package) Printf(format string, args ...any) {
fmt.Fprintf(&pkg.buf, format, args...)
}

Expand All @@ -235,7 +235,7 @@ func (pkg *Package) newlines(n int) {
// clears the stuff we don't want to print anyway. It's a bit of a magic trick.
func (pkg *Package) emit(comment string, node ast.Node) {
if node != nil {
var arg interface{} = node
var arg any = node
if showSrc {
// Need an extra little dance to get internal comments to appear.
arg = &printer.CommentedNode{
Expand Down
1 change: 1 addition & 0 deletions src/cmd/doc/testdata/nested/ignore.go
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

// Ignored package
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/fix/cftype.go
Expand Up @@ -45,8 +45,8 @@ func typefix(f *ast.File, badType func(string) bool) bool {

// step 1: Find all the nils with the offending types.
// Compute their replacement.
badNils := map[interface{}]ast.Expr{}
walk(f, func(n interface{}) {
badNils := map[any]ast.Expr{}
walk(f, func(n any) {
if i, ok := n.(*ast.Ident); ok && i.Name == "nil" && badType(typeof[n]) {
badNils[n] = &ast.BasicLit{ValuePos: i.NamePos, Kind: token.INT, Value: "0"}
}
Expand All @@ -58,7 +58,7 @@ func typefix(f *ast.File, badType func(string) bool) bool {
if len(badNils) > 0 {
exprType := reflect.TypeOf((*ast.Expr)(nil)).Elem()
exprSliceType := reflect.TypeOf(([]ast.Expr)(nil))
walk(f, func(n interface{}) {
walk(f, func(n any) {
if n == nil {
return
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func typefix(f *ast.File, badType func(string) bool) bool {
// Now we need unsafe.Pointer as an intermediate cast.
// (*unsafe.Pointer)(x) where x is type *bad -> (*unsafe.Pointer)(unsafe.Pointer(x))
// (*bad.type)(x) where x is type *unsafe.Pointer -> (*bad.type)(unsafe.Pointer(x))
walk(f, func(n interface{}) {
walk(f, func(n any) {
if n == nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/fix/fix.go
Expand Up @@ -43,15 +43,15 @@ func register(f fix) {
// walk traverses the AST x, calling visit(y) for each node y in the tree but
// also with a pointer to each ast.Expr, ast.Stmt, and *ast.BlockStmt,
// in a bottom-up traversal.
func walk(x interface{}, visit func(interface{})) {
func walk(x any, visit func(any)) {
walkBeforeAfter(x, nop, visit)
}

func nop(interface{}) {}
func nop(any) {}

// walkBeforeAfter is like walk but calls before(x) before traversing
// x's children and after(x) afterward.
func walkBeforeAfter(x interface{}, before, after func(interface{})) {
func walkBeforeAfter(x any, before, after func(any)) {
before(x)

switch n := x.(type) {
Expand Down Expand Up @@ -390,7 +390,7 @@ func renameTop(f *ast.File, old, new string) bool {
// Rename top-level old to new, both unresolved names
// (probably defined in another file) and names that resolve
// to a declaration we renamed.
walk(f, func(n interface{}) {
walk(f, func(n any) {
id, ok := n.(*ast.Ident)
if ok && isTopName(id, old) {
id.Name = new
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/fix/gotypes.go
Expand Up @@ -36,7 +36,7 @@ func fixGoExact(f *ast.File) bool {
// This one is harder because the import name changes.
// First find the import spec.
var importSpec *ast.ImportSpec
walk(f, func(n interface{}) {
walk(f, func(n any) {
if importSpec != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/fix/main.go
Expand Up @@ -245,7 +245,7 @@ func processFile(filename string, useStdin bool) error {
return os.WriteFile(f.Name(), newSrc, 0)
}

func gofmt(n interface{}) string {
func gofmt(n any) string {
var gofmtBuf bytes.Buffer
if err := format.Node(&gofmtBuf, fset, n); err != nil {
return "<" + err.Error() + ">"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/fix/netipv6zone.go
Expand Up @@ -26,7 +26,7 @@ func netipv6zone(f *ast.File) bool {
}

fixed := false
walk(f, func(n interface{}) {
walk(f, func(n any) {
cl, ok := n.(*ast.CompositeLit)
if !ok {
return
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/fix/printerconfig.go
Expand Up @@ -23,7 +23,7 @@ func printerconfig(f *ast.File) bool {
}

fixed := false
walk(f, func(n interface{}) {
walk(f, func(n any) {
cl, ok := n.(*ast.CompositeLit)
if !ok {
return
Expand Down
12 changes: 6 additions & 6 deletions src/cmd/fix/typecheck.go
Expand Up @@ -142,9 +142,9 @@ func (typ *Type) dot(cfg *TypeConfig, name string) string {
// typeof maps AST nodes to type information in gofmt string form.
// assign maps type strings to lists of expressions that were assigned
// to values of another type that were assigned to that type.
func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, assign map[string][]interface{}) {
typeof = make(map[interface{}]string)
assign = make(map[string][]interface{})
func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[any]string, assign map[string][]any) {
typeof = make(map[any]string)
assign = make(map[string][]any)
cfg1 := &TypeConfig{}
*cfg1 = *cfg // make copy so we can add locally
copied := false
Expand Down Expand Up @@ -296,7 +296,7 @@ func makeExprList(a []*ast.Ident) []ast.Expr {
// Typecheck1 is the recursive form of typecheck.
// It is like typecheck but adds to the information in typeof
// instead of allocating a new map.
func typecheck1(cfg *TypeConfig, f interface{}, typeof map[interface{}]string, assign map[string][]interface{}) {
func typecheck1(cfg *TypeConfig, f any, typeof map[any]string, assign map[string][]any) {
// set sets the type of n to typ.
// If isDecl is true, n is being declared.
set := func(n ast.Expr, typ string, isDecl bool) {
Expand Down Expand Up @@ -368,7 +368,7 @@ func typecheck1(cfg *TypeConfig, f interface{}, typeof map[interface{}]string, a
// the curfn stack.
var curfn []*ast.FuncType

before := func(n interface{}) {
before := func(n any) {
// push function type on stack
switch n := n.(type) {
case *ast.FuncDecl:
Expand All @@ -379,7 +379,7 @@ func typecheck1(cfg *TypeConfig, f interface{}, typeof map[interface{}]string, a
}

// After is the real type checker.
after := func(n interface{}) {
after := func(n any) {
if n == nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/go/internal/base/base.go
Expand Up @@ -117,12 +117,12 @@ func Exit() {
os.Exit(exitStatus)
}

func Fatalf(format string, args ...interface{}) {
func Fatalf(format string, args ...any) {
Errorf(format, args...)
Exit()
}

func Errorf(format string, args ...interface{}) {
func Errorf(format string, args ...any) {
log.Printf(format, args...)
SetExitStatus(1)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func GetExitStatus() int {
// Run runs the command, with stdout and stderr
// connected to the go command's own stdout and stderr.
// If the command fails, Run reports the error using Errorf.
func Run(cmdargs ...interface{}) {
func Run(cmdargs ...any) {
cmdline := str.StringList(cmdargs...)
if cfg.BuildN || cfg.BuildX {
fmt.Printf("%s\n", strings.Join(cmdline, " "))
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/cmdflag/flag.go
Expand Up @@ -92,7 +92,7 @@ func ParseOne(fs *flag.FlagSet, args []string) (f *flag.Flag, remainingArgs []st
// Use fs.Set instead of f.Value.Set below so that any subsequent call to
// fs.Visit will correctly visit the flags that have been set.

failf := func(format string, a ...interface{}) (*flag.Flag, []string, error) {
failf := func(format string, a ...any) (*flag.Flag, []string, error) {
return f, args, fmt.Errorf(format, a...)
}

Expand Down

0 comments on commit 2580d0e

Please sign in to comment.