go/printer: update comments and simplify test (cleanup)

Follow-up on CL 752220.

Change-Id: I848f9e721b0a209c70903bfb23a38f3d716c65a3
Reviewed-on: https://go-review.googlesource.com/c/go/+/779921
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Neal Patel <nealpatel@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2026-05-19 11:30:32 -07:00 committed by Gopher Robot
parent c07a0f09b8
commit 5563d58a15
2 changed files with 21 additions and 25 deletions

View file

@ -1312,8 +1312,7 @@ func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, po
// isCompositeLitLike reports whether x is a composite literal or an expression
// whose core is a composite literal (e.g. &T{...}), ignoring parentheses.
func isCompositeLitLike(x ast.Expr) bool {
x = stripParensAlways(x)
switch x := x.(type) {
switch x := stripParensAlways(x).(type) {
case *ast.CompositeLit:
return true
case *ast.UnaryExpr:
@ -1326,10 +1325,13 @@ func isCompositeLitLike(x ast.Expr) bool {
// indentList reports whether an expression list would look better if it
// were indented wholesale (starting with the very first element, rather
// than starting at the first line break).
// Currently this function is only used to improve formatting of return
// statements.
func (p *printer) indentList(list []ast.Expr) bool {
// Heuristic: indentList reports whether there are more than one multi-
// line element in the list, or if there is any element that is not
// starting on the same line as the previous one ends.
// line element (such as a complex expression, but excluding composite
// literals) in the list, or if there is any element that is not starting
// on the same line as the previous one ends.
if len(list) >= 2 {
var b = p.lineFor(list[0].Pos())
var e = p.lineFor(list[len(list)-1].End())
@ -1345,11 +1347,11 @@ func (p *printer) indentList(list []ast.Expr) bool {
// line as the previous one ended
return true
}
if xb < xe {
// x is a multi-line element.
if !isCompositeLitLike(x) {
n++
}
if xb < xe && !isCompositeLitLike(x) {
// x is a multi-line element but not a composite literal
// (composite literals have their own field indentation
// already, see go.dev/issue/7195)
n++
}
line = xe
}

View file

@ -907,40 +907,34 @@ func TestEmptyDecl(t *testing.T) { // issue 63566
// when printing a return statement with multiple multi-line composite literals.
func TestIssue7195(t *testing.T) {
const src = `package p
type T struct{ x int }
func f() (*T, *T) {
return &T{
func _() (T, *T) {
return T{
x: 1,
}, &T{
x: 2,
}
}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
if err := Fprint(&buf, fset, file); err != nil {
t.Fatal(err)
}
const want = `package p
type T struct{ x int }
func f() (*T, *T) {
return &T{
func _() (T, *T) {
return T{
x: 1,
}, &T{
x: 2,
}
}
`
if got := buf.String(); got != want {
got, err := format([]byte(src), 0)
if err != nil {
t.Fatal(err)
}
if got := string(got); got != want {
t.Fatalf("got:\n%s\nwant:\n%s\n", got, want)
}
}