1
0
mirror of https://github.com/golang/go synced 2024-09-29 10:14:29 -06:00

go/printer: if comment gets formatted away, don't squash

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>
This commit is contained in:
Ian Lance Taylor 2022-05-24 19:51:37 -07:00
parent 757f3c802a
commit 557244cef7
3 changed files with 21 additions and 0 deletions

View File

@ -747,6 +747,18 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
// a top-level doc comment. // a top-level doc comment.
list = formatDocComment(list) list = formatDocComment(list)
changed = true 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 { for _, c := range list {
p.writeCommentPrefix(p.posFor(c.Pos()), next, last, tok) p.writeCommentPrefix(p.posFor(c.Pos()), next, last, tok)

View File

@ -767,4 +767,8 @@ var _ = []T{ // lone comments
// in composite lit // 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 */ /* This comment is the last entry in this file. It must be printed and should be followed by a newline */

View File

@ -765,4 +765,9 @@ var _ = []T{// lone comments
// in composite lit // 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 */ /* This comment is the last entry in this file. It must be printed and should be followed by a newline */