From 2507e7897ba17bb33759c1c496d05bb7d459a31d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 18 Jan 2023 14:39:20 -0800 Subject: [PATCH] go/types: provision for generating initorder.go, but disabled for now Add the code to generate initorder.go but do not enable the generation of that file for now because the generated use uses error_ which has implications for gopls use (error_ produces a single error instead of pultiple \t-indented follow-on errors). Change-Id: I5cd8acdeb8845dbb4716f19cf90d88191dd4216c Reviewed-on: https://go-review.googlesource.com/c/go/+/461692 Reviewed-by: Robert Findley Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer --- src/go/types/generator.go | 42 +++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/go/types/generator.go b/src/go/types/generator.go index 2a0b9e5d5c..be599443eb 100644 --- a/src/go/types/generator.go +++ b/src/go/types/generator.go @@ -83,14 +83,15 @@ func generate(filename string, action action) { 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) }, + "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": fixErrorfCall, // disabled for now due to unresolved error_ use implications for gopls "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) }, "lookup.go": nil, "main_test.go": nil, @@ -228,6 +229,31 @@ func fixInferSig(f *ast.File) { }) } +// fixErrorfCall updates calls of the form err.errorf(obj, ...) to err.errorf(obj.Pos(), ...). +func fixErrorfCall(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 + }) +} + // 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 { -- 2.48.1