diff --git a/go/ast/astutil/rewrite.go b/go/ast/astutil/rewrite.go index 729e9c856a..f430b21b9b 100644 --- a/go/ast/astutil/rewrite.go +++ b/go/ast/astutil/rewrite.go @@ -293,6 +293,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast. a.apply(n, "Fields", nil, n.Fields) case *ast.FuncType: + if tparams := typeparams.ForFuncType(n); tparams != nil { + a.apply(n, "TypeParams", nil, tparams) + } a.apply(n, "Params", nil, n.Params) a.apply(n, "Results", nil, n.Results) @@ -405,6 +408,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast. case *ast.TypeSpec: a.apply(n, "Doc", nil, n.Doc) a.apply(n, "Name", nil, n.Name) + if tparams := typeparams.ForTypeSpec(n); tparams != nil { + a.apply(n, "TypeParams", nil, tparams) + } a.apply(n, "Type", nil, n.Type) a.apply(n, "Comment", nil, n.Comment) diff --git a/go/ast/astutil/rewrite_test.go b/go/ast/astutil/rewrite_test.go index 9d23170a5d..4ef6fe99de 100644 --- a/go/ast/astutil/rewrite_test.go +++ b/go/ast/astutil/rewrite_test.go @@ -202,20 +202,30 @@ func init() { type T[P1, P2 any] int type R T[int, string] + +func F[Q1 any](q Q1) {} `, + // TODO: note how the rewrite adds a trailing comma in "func F". + // Is that a bug in the test, or in astutil.Apply? want: `package p -type S[P1, P2 any] int32 +type S[R1, P2 any] int32 type R S[int32, string] + +func F[X1 any](q X1,) {} `, post: func(c *astutil.Cursor) bool { if ident, ok := c.Node().(*ast.Ident); ok { - if ident.Name == "int" { + switch ident.Name { + case "int": c.Replace(ast.NewIdent("int32")) - } - if ident.Name == "T" { + case "T": c.Replace(ast.NewIdent("S")) + case "P1": + c.Replace(ast.NewIdent("R1")) + case "Q1": + c.Replace(ast.NewIdent("X1")) } } return true