From 59964663e58d60719c707d9d59d7424d6f2ea7e0 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 18 Jan 2023 13:08:49 -0500 Subject: go/types: add a test that generated files match types2 Adjust the go/types file generation to run in a test, so that we can easily reuse the existing logic to verify that the current content of go/types matches the expected result of generating from types2. This test will enforce that we don't forget to regenerate go/types when making changes to types2. Change-Id: Iee14b1402065f7f0ecbcf28000e07a06c08fa42e Reviewed-on: https://go-review.googlesource.com/c/go/+/462758 Run-TryBot: Robert Findley TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer Auto-Submit: Robert Findley --- src/go/types/array.go | 2 +- src/go/types/basic.go | 2 +- src/go/types/chan.go | 2 +- src/go/types/context.go | 2 +- src/go/types/context_test.go | 2 +- src/go/types/gccgosizes.go | 2 +- src/go/types/generate.go | 2 +- src/go/types/generate_test.go | 354 +++++++++++++++++++++++++++++++++++++++ src/go/types/generator.go | 342 ------------------------------------- src/go/types/hilbert_test.go | 2 +- src/go/types/infer.go | 2 +- src/go/types/instantiate.go | 2 +- src/go/types/instantiate_test.go | 2 +- src/go/types/lookup.go | 2 +- src/go/types/main_test.go | 2 +- src/go/types/map.go | 2 +- src/go/types/named.go | 2 +- src/go/types/object.go | 2 +- src/go/types/object_test.go | 2 +- src/go/types/objset.go | 2 +- src/go/types/package.go | 2 +- src/go/types/pointer.go | 2 +- src/go/types/predicates.go | 2 +- src/go/types/scope.go | 2 +- src/go/types/selection.go | 2 +- src/go/types/sizes.go | 2 +- src/go/types/slice.go | 2 +- src/go/types/subst.go | 2 +- src/go/types/termlist.go | 2 +- src/go/types/termlist_test.go | 2 +- src/go/types/tuple.go | 2 +- src/go/types/typelists.go | 2 +- src/go/types/typeparam.go | 2 +- src/go/types/typeterm.go | 2 +- src/go/types/typeterm_test.go | 2 +- src/go/types/under.go | 2 +- src/go/types/unify.go | 2 +- src/go/types/universe.go | 2 +- src/go/types/util_test.go | 2 +- src/go/types/validtype.go | 2 +- 40 files changed, 392 insertions(+), 380 deletions(-) create mode 100644 src/go/types/generate_test.go delete mode 100644 src/go/types/generator.go diff --git a/src/go/types/array.go b/src/go/types/array.go index 759e2f2e67..f19ce6e528 100644 --- a/src/go/types/array.go +++ b/src/go/types/array.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/basic.go b/src/go/types/basic.go index 7d6decdb1a..d483616413 100644 --- a/src/go/types/basic.go +++ b/src/go/types/basic.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/chan.go b/src/go/types/chan.go index 565e9d2e9d..940620067c 100644 --- a/src/go/types/chan.go +++ b/src/go/types/chan.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/context.go b/src/go/types/context.go index 81b2f139eb..56368e1060 100644 --- a/src/go/types/context.go +++ b/src/go/types/context.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/context_test.go b/src/go/types/context_test.go index 1340bf2053..c9f6dce728 100644 --- a/src/go/types/context_test.go +++ b/src/go/types/context_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/gccgosizes.go b/src/go/types/gccgosizes.go index d98105c69a..9152c811e1 100644 --- a/src/go/types/gccgosizes.go +++ b/src/go/types/gccgosizes.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/generate.go b/src/go/types/generate.go index 6759f117b9..7fec743170 100644 --- a/src/go/types/generate.go +++ b/src/go/types/generate.go @@ -3,6 +3,6 @@ // license that can be found in the LICENSE file. // This file exists only to drive go:generate. -//go:generate go run generator.go +//go:generate go test -run=Generate -write=all package types diff --git a/src/go/types/generate_test.go b/src/go/types/generate_test.go new file mode 100644 index 0000000000..b6942279a8 --- /dev/null +++ b/src/go/types/generate_test.go @@ -0,0 +1,354 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file implements a custom generator to create various go/types +// source files from the corresponding types2 files. + +package types_test + +import ( + "bytes" + "flag" + "go/ast" + "go/format" + "go/parser" + "go/token" + "internal/diff" + "os" + "path/filepath" + "runtime" + "strings" + "testing" +) + +var filesToWrite = flag.String("write", "", `go/types files to generate, or "all" for all files`) + +const ( + srcDir = "/src/cmd/compile/internal/types2/" + dstDir = "/src/go/types/" +) + +// TestGenerate verifies that generated files in go/types match their types2 +// counterpart. If -write is set, this test actually writes the expected +// content to go/types; otherwise, it just compares with the existing content. +func TestGenerate(t *testing.T) { + // If filesToWrite is set, write the generated content to disk. + // In the special case of "all", write all files in filemap. + write := *filesToWrite != "" + var files []string // files to process + if *filesToWrite != "" && *filesToWrite != "all" { + files = strings.Split(*filesToWrite, ",") + } else { + for file := range filemap { + files = append(files, file) + } + } + + for _, filename := range files { + generate(t, filename, write) + } +} + +func generate(t *testing.T, filename string, write bool) { + // parse src + srcFilename := filepath.FromSlash(runtime.GOROOT() + srcDir + filename) + file, err := parser.ParseFile(fset, srcFilename, nil, parser.ParseComments) + if err != nil { + t.Fatal(err) + } + + // fix package name + file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types") + + // rewrite AST as needed + if action := filemap[filename]; action != nil { + action(file) + } + + // format AST + var buf bytes.Buffer + buf.WriteString("// Code generated by \"go test -run=Generate -write=all\"; DO NOT EDIT.\n\n") + if err := format.Node(&buf, fset, file); err != nil { + t.Fatal(err) + } + generatedContent := buf.Bytes() + + dstFilename := filepath.FromSlash(runtime.GOROOT() + dstDir + filename) + onDiskContent, err := os.ReadFile(dstFilename) + if err != nil { + t.Fatalf("reading %q: %v", filename, err) + } + + if d := diff.Diff(filename+" (on disk)", onDiskContent, filename+" (generated)", generatedContent); d != nil { + if write { + t.Logf("applying change:\n%s", d) + if err := os.WriteFile(dstFilename, generatedContent, 0o644); err != nil { + t.Fatalf("writing %q: %v", filename, err) + } + } else { + t.Errorf("generated file content does not match:\n%s", string(d)) + } + } +} + +type action func(in *ast.File) + +var filemap = map[string]action{ + "array.go": nil, + "basic.go": nil, + "chan.go": nil, + "context.go": nil, + "context_test.go": nil, + "gccgosizes.go": nil, + "hilbert_test.go": nil, + "infer.go": func(f *ast.File) { fixTokenPos(f); fixInferSig(f) }, + // "initorder.go": fixErrErrorfCall, // disabled for now due to unresolved error_ use implications for gopls + "instantiate.go": func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) }, + "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) }, + "lookup.go": nil, + "main_test.go": nil, + "map.go": nil, + "named.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) }, + "object.go": func(f *ast.File) { fixTokenPos(f); renameIdent(f, "NewTypeNameLazy", "_NewTypeNameLazy") }, + "object_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) }, + "objset.go": nil, + "package.go": nil, + "pointer.go": nil, + "predicates.go": nil, + "scope.go": func(f *ast.File) { + fixTokenPos(f) + renameIdent(f, "Squash", "squash") + renameIdent(f, "InsertLazy", "_InsertLazy") + }, + "selection.go": nil, + "sizes.go": func(f *ast.File) { renameIdent(f, "IsSyncAtomicAlign64", "_IsSyncAtomicAlign64") }, + "slice.go": nil, + "subst.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) }, + "termlist.go": nil, + "termlist_test.go": nil, + "tuple.go": nil, + "typelists.go": nil, + "typeparam.go": nil, + "typeterm_test.go": nil, + "typeterm.go": nil, + "under.go": nil, + "unify.go": fixSprintf, + "universe.go": fixGlobalTypVarDecl, + "util_test.go": fixTokenPos, + "validtype.go": nil, +} + +// TODO(gri) We should be able to make these rewriters more configurable/composable. +// For now this is a good starting point. + +// renameIdent renames an identifier. +// Note: This doesn't change the use of the identifier in comments. +func renameIdent(f *ast.File, from, to string) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.Ident: + if n.Name == from { + n.Name = to + } + return false + } + return true + }) +} + +// renameImportPath renames an import path. +func renameImportPath(f *ast.File, from, to string) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.ImportSpec: + if n.Path.Kind == token.STRING && n.Path.Value == from { + n.Path.Value = to + return false + } + } + return true + }) +} + +// fixTokenPos changes imports of "cmd/compile/internal/syntax" to "go/token", +// uses of syntax.Pos to token.Pos, and calls to x.IsKnown() to x.IsValid(). +func fixTokenPos(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.ImportSpec: + // rewrite import path "cmd/compile/internal/syntax" to "go/token" + if n.Path.Kind == token.STRING && n.Path.Value == `"cmd/compile/internal/syntax"` { + n.Path.Value = `"go/token"` + return false + } + case *ast.SelectorExpr: + // rewrite syntax.Pos to token.Pos + if x, _ := n.X.(*ast.Ident); x != nil && x.Name == "syntax" && n.Sel.Name == "Pos" { + x.Name = "token" + return false + } + case *ast.CallExpr: + // rewrite x.IsKnown() to x.IsValid() + if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && fun.Sel.Name == "IsKnown" && len(n.Args) == 0 { + fun.Sel.Name = "IsValid" + return false + } + } + return true + }) +} + +// fixInferSig updates the Checker.infer signature to use a positioner instead of a token.Position +// as first argument, renames the argument from "pos" to "posn", and updates a few internal uses of +// "pos" to "posn" and "posn.Pos()" respectively. +func fixInferSig(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.FuncDecl: + if n.Name.Name == "infer" { + // rewrite (pos token.Pos, ...) to (posn positioner, ...) + par := n.Type.Params.List[0] + if len(par.Names) == 1 && par.Names[0].Name == "pos" { + par.Names[0] = newIdent(par.Names[0].Pos(), "posn") + par.Type = newIdent(par.Type.Pos(), "positioner") + return true + } + } + case *ast.CallExpr: + if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil { + switch selx.Sel.Name { + case "renameTParams": + // rewrite check.renameTParams(pos, ... ) to check.renameTParams(posn.Pos(), ... ) + if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" { + pos := n.Args[0].Pos() + fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")} + arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos} + n.Args[0] = arg + return false + } + case "errorf": + // rewrite check.errorf(pos, ...) to check.errorf(posn, ...) + if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" { + pos := n.Args[0].Pos() + arg := newIdent(pos, "posn") + n.Args[0] = arg + return false + } + } + } + } + return true + }) +} + +// fixErrErrorfCall updates calls of the form err.errorf(obj, ...) to err.errorf(obj.Pos(), ...). +func fixErrErrorfCall(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.CallExpr: + if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil { + if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "err" { + switch selx.Sel.Name { + case "errorf": + // rewrite err.errorf(obj, ... ) to err.errorf(obj.Pos(), ... ) + if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" { + pos := n.Args[0].Pos() + fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")} + arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos} + n.Args[0] = arg + return false + } + } + } + } + } + return true + }) +} + +// fixCheckErrorfCall updates calls of the form check.errorf(pos, ...) to check.errorf(atPos(pos), ...). +func fixCheckErrorfCall(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.CallExpr: + if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil { + if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "check" { + switch selx.Sel.Name { + case "errorf": + // rewrite check.errorf(pos, ... ) to check.errorf(atPos(pos), ... ) + if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" { + pos := n.Args[0].Pos() + fun := newIdent(pos, "atPos") + arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Ellipsis: token.NoPos, Rparen: pos} + n.Args[0] = arg + return false + } + } + } + } + } + return true + }) +} + +// fixTraceSel renames uses of x.Trace to x.trace, where x for any x with a Trace field. +func fixTraceSel(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.SelectorExpr: + // rewrite x.Trace to x._Trace (for Config.Trace) + if n.Sel.Name == "Trace" { + n.Sel.Name = "_Trace" + return false + } + } + return true + }) +} + +// fixGlobalTypVarDecl changes the global Typ variable from an array to a slice +// (in types2 we use an array for efficiency, in go/types it's a slice and we +// cannot change that). +func fixGlobalTypVarDecl(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.ValueSpec: + // rewrite type Typ = [...]Type{...} to type Typ = []Type{...} + if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 { + n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil + return false + } + } + return true + }) +} + +// fixSprintf adds an extra nil argument for the *token.FileSet parameter in sprintf calls. +func fixSprintf(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.CallExpr: + if fun, _ := n.Fun.(*ast.Ident); fun != nil && fun.Name == "sprintf" && len(n.Args) >= 4 /* ... args */ { + n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil")) + return false + } + } + return true + }) +} + +// newIdent returns a new identifier with the given position and name. +func newIdent(pos token.Pos, name string) *ast.Ident { + id := ast.NewIdent(name) + id.NamePos = pos + return id +} + +// insert inserts x at list[at] and moves the remaining elements up. +func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr { + list = append(list, nil) + copy(list[at+1:], list[at:]) + list[at] = x + return list +} diff --git a/src/go/types/generator.go b/src/go/types/generator.go deleted file mode 100644 index fc6295d3c8..0000000000 --- a/src/go/types/generator.go +++ /dev/null @@ -1,342 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build ignore - -// This file implements a custom generator to create various go/types -// source files from the corresponding types2 files. - -package main - -import ( - "bytes" - "flag" - "fmt" - "go/ast" - "go/format" - "go/parser" - "go/token" - "log" - "os" - "path/filepath" - "runtime" - "strings" -) - -const ( - srcDir = "cmd/compile/internal/types2" - dstDir = "go/types" -) - -var fset = token.NewFileSet() - -func main() { - flag.Parse() - - // process provided filenames, if any - if flag.NArg() > 0 { - for _, filename := range flag.Args() { - generate(filename, filemap[filename]) - } - fmt.Printf("generated %d files\n", flag.NArg()) - return - } - - // otherwise process per filemap below - for filename, action := range filemap { - generate(filename, action) - } - fmt.Printf("generated %d files\n", len(filemap)) -} - -func generate(filename string, action action) { - // parse src - srcFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + srcDir + "/" + filename) - file, err := parser.ParseFile(fset, srcFilename, nil, parser.ParseComments) - if err != nil { - log.Fatal(err) - } - - // fix package name - file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types") - - // rewrite AST as needed - if action != nil { - action(file) - } - - // format AST - var buf bytes.Buffer - buf.WriteString("// Code generated by \"go run generator.go\"; DO NOT EDIT.\n\n") - if err := format.Node(&buf, fset, file); err != nil { - log.Fatal(err) - } - - // write dst - dstFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + dstDir + "/" + filename) - if err := os.WriteFile(dstFilename, buf.Bytes(), 0o644); err != nil { - log.Fatal(err) - } -} - -type action func(in *ast.File) - -var filemap = map[string]action{ - "array.go": nil, - "basic.go": nil, - "chan.go": nil, - "context.go": nil, - "context_test.go": nil, - "gccgosizes.go": nil, - "hilbert_test.go": nil, - "infer.go": func(f *ast.File) { fixTokenPos(f); fixInferSig(f) }, - // "initorder.go": fixErrErrorfCall, // disabled for now due to unresolved error_ use implications for gopls - "instantiate.go": func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) }, - "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) }, - "lookup.go": nil, - "main_test.go": nil, - "map.go": nil, - "named.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) }, - "object.go": func(f *ast.File) { fixTokenPos(f); renameIdent(f, "NewTypeNameLazy", "_NewTypeNameLazy") }, - "object_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) }, - "objset.go": nil, - "package.go": nil, - "pointer.go": nil, - "predicates.go": nil, - "scope.go": func(f *ast.File) { - fixTokenPos(f) - renameIdent(f, "Squash", "squash") - renameIdent(f, "InsertLazy", "_InsertLazy") - }, - "selection.go": nil, - "sizes.go": func(f *ast.File) { renameIdent(f, "IsSyncAtomicAlign64", "_IsSyncAtomicAlign64") }, - "slice.go": nil, - "subst.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) }, - "termlist.go": nil, - "termlist_test.go": nil, - "tuple.go": nil, - "typelists.go": nil, - "typeparam.go": nil, - "typeterm_test.go": nil, - "typeterm.go": nil, - "under.go": nil, - "unify.go": fixSprintf, - "universe.go": fixGlobalTypVarDecl, - "util_test.go": fixTokenPos, - "validtype.go": nil, -} - -// TODO(gri) We should be able to make these rewriters more configurable/composable. -// For now this is a good starting point. - -// renameIdent renames an identifier. -// Note: This doesn't change the use of the identifier in comments. -func renameIdent(f *ast.File, from, to string) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.Ident: - if n.Name == from { - n.Name = to - } - return false - } - return true - }) -} - -// renameImportPath renames an import path. -func renameImportPath(f *ast.File, from, to string) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.ImportSpec: - if n.Path.Kind == token.STRING && n.Path.Value == from { - n.Path.Value = to - return false - } - } - return true - }) -} - -// fixTokenPos changes imports of "cmd/compile/internal/syntax" to "go/token", -// uses of syntax.Pos to token.Pos, and calls to x.IsKnown() to x.IsValid(). -func fixTokenPos(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.ImportSpec: - // rewrite import path "cmd/compile/internal/syntax" to "go/token" - if n.Path.Kind == token.STRING && n.Path.Value == `"cmd/compile/internal/syntax"` { - n.Path.Value = `"go/token"` - return false - } - case *ast.SelectorExpr: - // rewrite syntax.Pos to token.Pos - if x, _ := n.X.(*ast.Ident); x != nil && x.Name == "syntax" && n.Sel.Name == "Pos" { - x.Name = "token" - return false - } - case *ast.CallExpr: - // rewrite x.IsKnown() to x.IsValid() - if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && fun.Sel.Name == "IsKnown" && len(n.Args) == 0 { - fun.Sel.Name = "IsValid" - return false - } - } - return true - }) -} - -// fixInferSig updates the Checker.infer signature to use a positioner instead of a token.Position -// as first argument, renames the argument from "pos" to "posn", and updates a few internal uses of -// "pos" to "posn" and "posn.Pos()" respectively. -func fixInferSig(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.FuncDecl: - if n.Name.Name == "infer" { - // rewrite (pos token.Pos, ...) to (posn positioner, ...) - par := n.Type.Params.List[0] - if len(par.Names) == 1 && par.Names[0].Name == "pos" { - par.Names[0] = newIdent(par.Names[0].Pos(), "posn") - par.Type = newIdent(par.Type.Pos(), "positioner") - return true - } - } - case *ast.CallExpr: - if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil { - switch selx.Sel.Name { - case "renameTParams": - // rewrite check.renameTParams(pos, ... ) to check.renameTParams(posn.Pos(), ... ) - if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" { - pos := n.Args[0].Pos() - fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")} - arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos} - n.Args[0] = arg - return false - } - case "errorf": - // rewrite check.errorf(pos, ...) to check.errorf(posn, ...) - if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" { - pos := n.Args[0].Pos() - arg := newIdent(pos, "posn") - n.Args[0] = arg - return false - } - } - } - } - return true - }) -} - -// fixErrErrorfCall updates calls of the form err.errorf(obj, ...) to err.errorf(obj.Pos(), ...). -func fixErrErrorfCall(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.CallExpr: - if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil { - if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "err" { - switch selx.Sel.Name { - case "errorf": - // rewrite err.errorf(obj, ... ) to err.errorf(obj.Pos(), ... ) - if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" { - pos := n.Args[0].Pos() - fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")} - arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos} - n.Args[0] = arg - return false - } - } - } - } - } - return true - }) -} - -// fixCheckErrorfCall updates calls of the form check.errorf(pos, ...) to check.errorf(atPos(pos), ...). -func fixCheckErrorfCall(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.CallExpr: - if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil { - if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "check" { - switch selx.Sel.Name { - case "errorf": - // rewrite check.errorf(pos, ... ) to check.errorf(atPos(pos), ... ) - if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" { - pos := n.Args[0].Pos() - fun := newIdent(pos, "atPos") - arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Ellipsis: token.NoPos, Rparen: pos} - n.Args[0] = arg - return false - } - } - } - } - } - return true - }) -} - -// fixTraceSel renames uses of x.Trace to x.trace, where x for any x with a Trace field. -func fixTraceSel(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.SelectorExpr: - // rewrite x.Trace to x._Trace (for Config.Trace) - if n.Sel.Name == "Trace" { - n.Sel.Name = "_Trace" - return false - } - } - return true - }) -} - -// fixGlobalTypVarDecl changes the global Typ variable from an array to a slice -// (in types2 we use an array for efficiency, in go/types it's a slice and we -// cannot change that). -func fixGlobalTypVarDecl(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.ValueSpec: - // rewrite type Typ = [...]Type{...} to type Typ = []Type{...} - if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 { - n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil - return false - } - } - return true - }) -} - -// fixSprintf adds an extra nil argument for the *token.FileSet parameter in sprintf calls. -func fixSprintf(f *ast.File) { - ast.Inspect(f, func(n ast.Node) bool { - switch n := n.(type) { - case *ast.CallExpr: - if fun, _ := n.Fun.(*ast.Ident); fun != nil && fun.Name == "sprintf" && len(n.Args) >= 4 /* ... args */ { - n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil")) - return false - } - } - return true - }) -} - -// newIdent returns a new identifier with the given position and name. -func newIdent(pos token.Pos, name string) *ast.Ident { - id := ast.NewIdent(name) - id.NamePos = pos - return id -} - -// insert inserts x at list[at] and moves the remaining elements up. -func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr { - list = append(list, nil) - copy(list[at+1:], list[at:]) - list[at] = x - return list -} diff --git a/src/go/types/hilbert_test.go b/src/go/types/hilbert_test.go index 15e5ae3aca..7da2a7ded1 100644 --- a/src/go/types/hilbert_test.go +++ b/src/go/types/hilbert_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/infer.go b/src/go/types/infer.go index ef14353540..5f91b526d9 100644 --- a/src/go/types/infer.go +++ b/src/go/types/infer.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/instantiate.go b/src/go/types/instantiate.go index ab3a9451c0..11afb7a853 100644 --- a/src/go/types/instantiate.go +++ b/src/go/types/instantiate.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/instantiate_test.go b/src/go/types/instantiate_test.go index 91b2a1aaba..574f3aeb86 100644 --- a/src/go/types/instantiate_test.go +++ b/src/go/types/instantiate_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/lookup.go b/src/go/types/lookup.go index fa2b7b2f4b..2d6501b739 100644 --- a/src/go/types/lookup.go +++ b/src/go/types/lookup.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/main_test.go b/src/go/types/main_test.go index b0630947c1..fda9d102a3 100644 --- a/src/go/types/main_test.go +++ b/src/go/types/main_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2022 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/map.go b/src/go/types/map.go index 9827e8c147..febb0d3a0b 100644 --- a/src/go/types/map.go +++ b/src/go/types/map.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/named.go b/src/go/types/named.go index f1b2685ec3..413eaada27 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/object.go b/src/go/types/object.go index 74b630af9e..e47ef2ebec 100644 --- a/src/go/types/object.go +++ b/src/go/types/object.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/object_test.go b/src/go/types/object_test.go index b0459260a1..bed8de3637 100644 --- a/src/go/types/object_test.go +++ b/src/go/types/object_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/objset.go b/src/go/types/objset.go index 4e7aaac500..e6ea37566e 100644 --- a/src/go/types/objset.go +++ b/src/go/types/objset.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/package.go b/src/go/types/package.go index 7a64aa9fe4..7aa62fb7a3 100644 --- a/src/go/types/package.go +++ b/src/go/types/package.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/pointer.go b/src/go/types/pointer.go index 091824c55e..5b45ab7755 100644 --- a/src/go/types/pointer.go +++ b/src/go/types/pointer.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index c80d41d702..1927844732 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/scope.go b/src/go/types/scope.go index 433f31a478..bf646f6882 100644 --- a/src/go/types/scope.go +++ b/src/go/types/scope.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/selection.go b/src/go/types/selection.go index bc26f39b3f..c79e13c6eb 100644 --- a/src/go/types/selection.go +++ b/src/go/types/selection.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/sizes.go b/src/go/types/sizes.go index fb7a16699a..d32866913e 100644 --- a/src/go/types/sizes.go +++ b/src/go/types/sizes.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/slice.go b/src/go/types/slice.go index 0fb8476d24..934549c2d4 100644 --- a/src/go/types/slice.go +++ b/src/go/types/slice.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/subst.go b/src/go/types/subst.go index 3be5c02be2..30c48e1bad 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/termlist.go b/src/go/types/termlist.go index 46de92459b..9bc631c0e6 100644 --- a/src/go/types/termlist.go +++ b/src/go/types/termlist.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/termlist_test.go b/src/go/types/termlist_test.go index 7499fc6b9e..cf0c190516 100644 --- a/src/go/types/termlist_test.go +++ b/src/go/types/termlist_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/tuple.go b/src/go/types/tuple.go index 8ccf0289a3..e5e3914bb2 100644 --- a/src/go/types/tuple.go +++ b/src/go/types/tuple.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typelists.go b/src/go/types/typelists.go index 864a34fa54..c000de2272 100644 --- a/src/go/types/typelists.go +++ b/src/go/types/typelists.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go index c573fa8633..763fcc61f0 100644 --- a/src/go/types/typeparam.go +++ b/src/go/types/typeparam.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typeterm.go b/src/go/types/typeterm.go index d862add06e..c86442c43f 100644 --- a/src/go/types/typeterm.go +++ b/src/go/types/typeterm.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typeterm_test.go b/src/go/types/typeterm_test.go index bf67f3872d..c6370f44fe 100644 --- a/src/go/types/typeterm_test.go +++ b/src/go/types/typeterm_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/under.go b/src/go/types/under.go index 0c2410e74e..f17d3bcda4 100644 --- a/src/go/types/under.go +++ b/src/go/types/under.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/unify.go b/src/go/types/unify.go index 9db63ca9ec..9fb0c75893 100644 --- a/src/go/types/unify.go +++ b/src/go/types/unify.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2020 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/universe.go b/src/go/types/universe.go index 6984cf849b..d32a8ed4e8 100644 --- a/src/go/types/universe.go +++ b/src/go/types/universe.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/util_test.go b/src/go/types/util_test.go index 4667451a97..205237211f 100644 --- a/src/go/types/util_test.go +++ b/src/go/types/util_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2023 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/validtype.go b/src/go/types/validtype.go index 5fd5f73cfa..d915fef825 100644 --- a/src/go/types/validtype.go +++ b/src/go/types/validtype.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2022 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style -- cgit v1.3