1
0
mirror of https://github.com/golang/go synced 2024-11-24 04:10:14 -07:00

go/printer: don't strip parens around *ast.Ellipsis in return field list

Change-Id: Ic6a6d87202f9b531a591dc0ab2ef5fb050251198
This commit is contained in:
Mateusz Poliwczak 2024-11-09 12:12:11 +01:00
parent 5123f38e05
commit f1f04d35e9
2 changed files with 25 additions and 3 deletions

View File

@ -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)
}

View File

@ -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)
}
}