]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: if comment gets formatted away, don't squash
authorIan Lance Taylor <iant@golang.org>
Wed, 25 May 2022 02:51:37 +0000 (19:51 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 25 May 2022 23:11:14 +0000 (23:11 +0000)
We were dropping the preceding whitespace.

Fixes #53059

Change-Id: Ide0d6a76c463a7481ec4883a8568f7f7b5e87e9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/408394
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/go/printer/printer.go
src/go/printer/testdata/comments.golden
src/go/printer/testdata/comments.input

index 25eec6bd751a2d82ad03eef06ef5819035d127d4..244a19b2a7787ffc5370e81b5859c6000eb80b84 100644 (file)
@@ -747,6 +747,18 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
                        // a top-level doc comment.
                        list = formatDocComment(list)
                        changed = true
+
+                       if len(p.comment.List) > 0 && len(list) == 0 {
+                               // The doc comment was removed entirely.
+                               // Keep preceding whitespace.
+                               p.writeCommentPrefix(p.posFor(p.comment.Pos()), next, last, tok)
+                               // Change print state to continue at next.
+                               p.pos = next
+                               p.last = next
+                               // There can't be any more comments.
+                               p.nextComment()
+                               return p.writeCommentSuffix(false)
+                       }
                }
                for _, c := range list {
                        p.writeCommentPrefix(p.posFor(c.Pos()), next, last, tok)
index 62f37ea091069fbecb7c818de90df90eaf2ff665..1e5d17b4e1398d4f29304fc81295343ed37f46dd 100644 (file)
@@ -767,4 +767,8 @@ var _ = []T{        // lone comments
        // in composite lit
 }
 
+func _()       {}
+
+func _()       {}
+
 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */
index 4bdafc3781de31793c0295b68ad95a9ea3dcda83..40aa55be068ae25b807f069313ae13c97418ec85 100644 (file)
@@ -765,4 +765,9 @@ var _ = []T{// lone comments
 // in composite lit
 }
 
+func _() {}
+
+//
+func _() {}
+
 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */