From a265f2e90eb928ab773ecabf48aefb11b188296d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 29 Oct 2017 21:04:01 +0000 Subject: [PATCH] go/printer: indent lone comments in composite lits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Reviewed-by: Robert Griesemer --- src/go/printer/nodes.go | 4 +++- src/go/printer/testdata/comments.golden | 28 +++++++++++++++++++++++++ src/go/printer/testdata/comments.input | 28 +++++++++++++++++++++++++ src/math/big/int.go | 2 -- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/go/printer/nodes.go b/src/go/printer/nodes.go index 83c17fef5e..5b5b6063fb 100644 --- a/src/go/printer/nodes.go +++ b/src/go/printer/nodes.go @@ -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: diff --git a/src/go/printer/testdata/comments.golden b/src/go/printer/testdata/comments.golden index d14d8d6cbb..3fc29ccf91 100644 --- a/src/go/printer/testdata/comments.golden +++ b/src/go/printer/testdata/comments.golden @@ -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 */ diff --git a/src/go/printer/testdata/comments.input b/src/go/printer/testdata/comments.input index df98056a65..985e334795 100644 --- a/src/go/printer/testdata/comments.input +++ b/src/go/printer/testdata/comments.input @@ -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 */ diff --git a/src/math/big/int.go b/src/math/big/int.go index 135ebd083f..0eda9cd4e1 100644 --- a/src/math/big/int.go +++ b/src/math/big/int.go @@ -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