From f1f04d35e901cda8f601aca255f1ee492f421199 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sat, 9 Nov 2024 12:12:11 +0100 Subject: [PATCH] go/printer: don't strip parens around *ast.Ellipsis in return field list Change-Id: Ic6a6d87202f9b531a591dc0ab2ef5fb050251198 --- src/go/printer/nodes.go | 10 +++++++--- src/go/printer/printer_test.go | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/go/printer/nodes.go b/src/go/printer/nodes.go index a295a68d6f8..dd9c0f96c6f 100644 --- a/src/go/printer/nodes.go +++ b/src/go/printer/nodes.go @@ -447,9 +447,13 @@ func (p *printer) signature(sig *ast.FuncType) { // res != nil p.print(blank) if n == 1 && res.List[0].Names == nil { - // single anonymous res; no ()'s - p.expr(stripParensAlways(res.List[0].Type)) - return + // Parser permits ellipsis inside of a return field list, like: func A() (...int), + // removing them would cause a parse error. + if _, ok := res.List[0].Type.(*ast.Ellipsis); !ok { + // single anonymous res; no ()'s + p.expr(stripParensAlways(res.List[0].Type)) + return + } } p.parameters(res, funcParam) } diff --git a/src/go/printer/printer_test.go b/src/go/printer/printer_test.go index 2a9c8be3003..6e0269c2d4c 100644 --- a/src/go/printer/printer_test.go +++ b/src/go/printer/printer_test.go @@ -16,6 +16,7 @@ import ( "io" "os" "path/filepath" + "strings" "testing" "time" ) @@ -863,3 +864,20 @@ func TestEmptyDecl(t *testing.T) { // issue 63566 } } } + +func TestPrintParsableEllipsisInReturnFieldList(t *testing.T) { + const src = "package A\n\nfunc A() (...A)\n" + + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "test.go", src, parser.SkipObjectResolution) + if err != nil { + t.Fatal(err) + } + + var out strings.Builder + Fprint(&out, fset, f) + + if out.String() != src { + t.Fatalf("source = %q; printed as = %q want = %q", src, out.String(), src) + } +}