Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/cmd/compile/internal/syntax/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,10 +886,12 @@ func (p *printer) printSignature(sig *FuncType) {
if list := sig.ResultList; list != nil {
p.print(blank)
if len(list) == 1 && list[0].Name == nil {
p.printNode(list[0].Type)
} else {
p.printParameterList(list, 0)
if _, ok := list[0].Type.(*DotsType); !ok {
p.printNode(list[0].Type)
return
}
}
p.printParameterList(list, 0)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/cmd/compile/internal/syntax/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ var stringTests = [][2]string{
{"package p; type _[P ((C)),] int", "package p; type _[P C] int"},
{"package p; type _[P, Q ((C))] int", "package p; type _[P, Q C] int"},

dup("package A; func A() (...A)"),

// TODO(gri) expand
}

Expand Down
10 changes: 7 additions & 3 deletions src/go/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 18 additions & 0 deletions src/go/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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)
}
}