Commit a265f2e9 authored by Daniel Martí's avatar Daniel Martí Committed by Robert Griesemer

go/printer: indent lone comments in composite lits

If a composite literal contains any comments on their own lines without
any elements, the printer would unindent the comments.

The comments in this edge case are written when the closing '}' is
written. Indent and outdent first so that the indentation is
interspersed before the comment is written.

Also note that the go/printer golden tests don't show the exact same
behaviour that gofmt does. Added a TODO to figure this out in a separate
CL.

While at it, ensure that the tree conforms to gofmt. The changes are
unrelated to this indentation fix, however.

Fixes #22355.

Change-Id: I5ac25ac6de95a236f1e123479127cc4dd71e93fe
Reviewed-on: https://go-review.googlesource.com/74232
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent 89b7a08a
......@@ -865,7 +865,9 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if len(x.Elts) > 0 {
mode |= noExtraBlank
}
p.print(mode, x.Rbrace, token.RBRACE, mode)
// need the initial indent to print lone comments with
// the proper level of indentation
p.print(indent, unindent, mode, x.Rbrace, token.RBRACE, mode)
p.level--
case *ast.Ellipsis:
......
......@@ -831,4 +831,32 @@ func _() {
*/
}
var _ = []T{ /* lone comment */ }
var _ = []T{
/* lone comment */
}
var _ = []T{
// lone comments
// in composite lit
}
var _ = [][]T{
{
// lone comments
// in composite lit
},
}
// TODO: gofmt doesn't add these tabs; make it so that these golden
// tests run the printer in a way that it's exactly like gofmt.
var _ = []T{ // lone comment
}
var _ = []T{ // lone comments
// in composite lit
}
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */
......@@ -832,4 +832,32 @@ func _() {
*/
}
var _ = []T{/* lone comment */}
var _ = []T{
/* lone comment */
}
var _ = []T{
// lone comments
// in composite lit
}
var _ = [][]T{
{
// lone comments
// in composite lit
},
}
// TODO: gofmt doesn't add these tabs; make it so that these golden
// tests run the printer in a way that it's exactly like gofmt.
var _ = []T{// lone comment
}
var _ = []T{// lone comments
// in composite lit
}
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */
......@@ -533,7 +533,6 @@ func (z *Int) GCD(x, y, a, b *Int) *Int {
// See Jebelean, "Improving the multiprecision Euclidean algorithm",
// Design and Implementation of Symbolic Computation Systems, pp 45-58.
func (z *Int) lehmerGCD(a, b *Int) *Int {
// ensure a >= b
if a.abs.cmp(b.abs) < 0 {
a, b = b, a
......@@ -551,7 +550,6 @@ func (z *Int) lehmerGCD(a, b *Int) *Int {
// loop invariant A >= B
for len(B.abs) > 1 {
// initialize the digits
var a1, a2, u0, u1, u2, v0, v1, v2 Word
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment