Skip to content

Commit 08f19bb

Browse files
committed
go/printer: forbid empty line before first comment in block
To improve readability when exported fields are removed, forbid the printer from emitting an empty line before the first comment in a const, var, or type block. Also, when printing the "Has filtered or unexported fields." message, add an empty line before it to separate the message from the struct or interfact contents. Before the change: <<< type NamedArg struct { // Name is the name of the parameter placeholder. // // If empty, the ordinal position in the argument list will be // used. // // Name must omit any symbol prefix. Name string // Value is the value of the parameter. // It may be assigned the same value types as the query // arguments. Value interface{} // contains filtered or unexported fields } >>> After the change: <<< type NamedArg struct { // Name is the name of the parameter placeholder. // // If empty, the ordinal position in the argument list will be // used. // // Name must omit any symbol prefix. Name string // Value is the value of the parameter. // It may be assigned the same value types as the query // arguments. Value interface{} // contains filtered or unexported fields } >>> Fixes #18264 Change-Id: I9fe17ca39cf92fcdfea55064bd2eaa784ce48c88 Reviewed-on: https://go-review.googlesource.com/71990 Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 8585f9f commit 08f19bb

File tree

30 files changed

+293
-26
lines changed

30 files changed

+293
-26
lines changed

misc/cgo/test/buildid_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func testBuildID(t *testing.T) {
3939
}
4040

4141
for len(d) > 0 {
42-
4342
// ELF standards differ as to the sizes in
4443
// note sections. Both the GNU linker and
4544
// gold always generate 32-bit sizes, so that

src/cmd/compile/internal/ssa/gen/rulegen.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ func extract(val string) (op string, typ string, auxint string, aux string, args
659659
// It returns the op and unparsed strings for typ, auxint, and aux restrictions and for all args.
660660
// oparch is the architecture that op is located in, or "" for generic.
661661
func parseValue(val string, arch arch, loc string) (op opData, oparch string, typ string, auxint string, aux string, args []string) {
662-
663662
// Resolve the op.
664663
var s string
665664
s, typ, auxint, aux, args = extract(val)

src/cmd/internal/obj/ppc64/asm9.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2468,7 +2468,6 @@ func (c *ctxt9) asmout(p *obj.Prog, o *Optab, out []uint32) {
24682468
d := c.vregoff(p.GetFrom3())
24692469
var a int
24702470
switch p.As {
2471-
24722471
// These opcodes expect a mask operand that has to be converted into the
24732472
// appropriate operand. The way these were defined, not all valid masks are possible.
24742473
// Left here for compatibility in case they were used or generated.

src/cmd/internal/obj/x86/asm6.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,6 @@ func span6(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
19961996
c = 0
19971997
for p := s.Func.Text; p != nil; p = p.Link {
19981998
if ctxt.Headtype == objabi.Hnacl && p.Isize > 0 {
1999-
20001999
// pad everything to avoid crossing 32-byte boundary
20012000
if c>>5 != (c+int32(p.Isize)-1)>>5 {
20022001
c = naclpad(ctxt, s, c, -c&31)

src/cmd/link/internal/ld/data.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,6 @@ func assignAddress(ctxt *Link, sect *sym.Section, n int, s *sym.Symbol, va uint6
18571857
// Only break at outermost syms.
18581858

18591859
if ctxt.Arch.InFamily(sys.PPC64) && s.Outer == nil && ctxt.IsELF && ctxt.LinkMode == LinkExternal && va-sect.Vaddr+funcsize+maxSizeTrampolinesPPC64(s, isTramp) > 0x1c00000 {
1860-
18611860
// Set the length for the previous text section
18621861
sect.Length = va - sect.Vaddr
18631862

src/cmd/link/internal/ppc64/asm.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ func archrelocaddr(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, val *int64) bool
523523

524524
// resolve direct jump relocation r in s, and add trampoline if necessary
525525
func trampoline(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol) {
526-
527526
// Trampolines are created if the branch offset is too large and the linker cannot insert a call stub to handle it.
528527
// For internal linking, trampolines are always created for long calls.
529528
// For external linking, the linker can insert a call stub to handle a long call, but depends on having the TOC address in
@@ -542,7 +541,6 @@ func trampoline(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol) {
542541
if (ctxt.LinkMode == ld.LinkExternal && s.Sect != r.Sym.Sect) || (ctxt.LinkMode == ld.LinkInternal && int64(int32(t<<6)>>6) != t) || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) {
543542
var tramp *sym.Symbol
544543
for i := 0; ; i++ {
545-
546544
// Using r.Add as part of the name is significant in functions like duffzero where the call
547545
// target is at some offset within the function. Calls to duff+8 and duff+256 must appear as
548546
// distinct trampolines.

src/encoding/base32/base32.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
290290
dlen := 8
291291

292292
for j := 0; j < 8; {
293-
294293
// We have reached the end and are missing padding
295294
if len(src) == 0 && enc.padChar != NoPadding {
296295
return n, false, CorruptInputError(olen - len(src) - j)

src/go/doc/testdata/blank.0.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ TYPES
4848
H uint32
4949

5050
A uint8
51+
5152
// contains filtered or unexported fields
5253
}
5354

src/go/doc/testdata/blank.2.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ TYPES
4848
H uint32
4949

5050
A uint8
51+
5152
// contains filtered or unexported fields
5253
}
5354

src/go/doc/testdata/testing.0.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ TYPES
4242
// B is a type passed to Benchmark functions to manage benchmark ...
4343
type B struct {
4444
N int
45+
4546
// contains filtered or unexported fields
4647
}
4748

src/go/doc/testdata/testing.2.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ TYPES
4242
// B is a type passed to Benchmark functions to manage benchmark ...
4343
type B struct {
4444
N int
45+
4546
// contains filtered or unexported fields
4647
}
4748

src/go/printer/nodes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool)
479479
if isIncomplete {
480480
if len(list) > 0 {
481481
p.print(formfeed)
482+
p.print(newline)
482483
}
483484
p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
484485
p.setLineComment("// contains filtered or unexported fields")
@@ -506,6 +507,7 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool)
506507
if isIncomplete {
507508
if len(list) > 0 {
508509
p.print(formfeed)
510+
p.print(newline)
509511
}
510512
p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
511513
p.setLineComment("// contains filtered or unexported methods")

src/go/printer/printer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type printer struct {
6262
mode pmode // current printer mode
6363
impliedSemi bool // if set, a linebreak implies a semicolon
6464
lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace)
65-
prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL
65+
prevOpen token.Token // previous "open" token: (, [, {, or token.ILLEGAL
6666
wsbuf []whiteSpace // delayed white space
6767

6868
// Positions
@@ -426,6 +426,11 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment
426426
n := 0
427427
if pos.IsValid() && p.last.IsValid() {
428428
n = pos.Line - p.last.Line
429+
if n > 1 && p.prevOpen != token.ILLEGAL && prev == nil {
430+
// Forbid multiple empty lines from appearing immediately
431+
// following some type of open paren, bracket, or brace.
432+
n = 1
433+
}
429434
if n < 0 { // should never happen
430435
n = 0
431436
}
@@ -879,7 +884,7 @@ func (p *printer) print(args ...interface{}) {
879884
switch p.lastTok {
880885
case token.ILLEGAL:
881886
// ignore (white space)
882-
case token.LPAREN, token.LBRACK:
887+
case token.LPAREN, token.LBRACK, token.LBRACE:
883888
p.prevOpen = p.lastTok
884889
default:
885890
// other tokens followed any opening token

src/go/printer/testdata/comments.golden

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,109 @@ type S3 struct {
9797
f3 int // f3 is not exported
9898
}
9999

100+
type T struct { // foo
101+
102+
// bar
103+
x int
104+
X int
105+
}
106+
107+
type T struct { /* foo */
108+
109+
// bar
110+
x int
111+
}
112+
113+
type T struct { // foo
114+
115+
x int
116+
117+
X int
118+
}
119+
120+
type T struct { /* foo */
121+
122+
x int
123+
}
124+
125+
type T struct {
126+
x int
127+
}
128+
129+
type T struct {
130+
x int
131+
}
132+
133+
type T struct{ x int }
134+
135+
type T struct {
136+
x int
137+
}
138+
139+
type T struct {
140+
x int
141+
X int
142+
x int
143+
}
144+
145+
type T struct {
146+
// Comment
147+
x int
148+
// X.
149+
X int
150+
// Comment
151+
x int
152+
}
153+
154+
const ( // foo
155+
156+
// bar
157+
x int = 0
158+
X int = 0
159+
)
160+
161+
const ( /* foo */
162+
163+
// bar
164+
x int = 0
165+
)
166+
167+
const ( // foo
168+
169+
x int = 0
170+
171+
X int = 0
172+
)
173+
174+
const ( /* foo */
175+
176+
x int = 0
177+
)
178+
179+
const (
180+
x int = 0
181+
)
182+
183+
const (
184+
x int = 0
185+
X int = 0
186+
)
187+
188+
const (
189+
x int = 0
190+
X int = 0
191+
x int = 0
192+
)
193+
194+
const (
195+
// Comment
196+
x int = 0
197+
// X.
198+
X int = 0
199+
// Comment
200+
x int = 0
201+
)
202+
100203
// This comment group should be separated
101204
// with a newline from the next comment
102205
// group.

src/go/printer/testdata/comments.input

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,113 @@ type S3 struct {
9797
f3 int // f3 is not exported
9898
}
9999

100+
type T struct { // foo
101+
102+
// bar
103+
x int
104+
X int
105+
}
106+
107+
type T struct { /* foo */
108+
109+
// bar
110+
x int
111+
}
112+
113+
type T struct { // foo
114+
115+
x int
116+
117+
X int
118+
}
119+
120+
type T struct { /* foo */
121+
122+
123+
124+
x int
125+
}
126+
127+
type T struct {x int
128+
}
129+
130+
type T struct {
131+
x int}
132+
133+
type T struct {x int}
134+
135+
type T struct {
136+
137+
x int
138+
}
139+
140+
type T struct {
141+
x int
142+
X int
143+
x int
144+
}
145+
146+
type T struct {
147+
// Comment
148+
x int
149+
// X.
150+
X int
151+
// Comment
152+
x int
153+
}
154+
155+
const ( // foo
156+
157+
// bar
158+
x int = 0
159+
X int = 0
160+
)
161+
162+
const ( /* foo */
163+
164+
// bar
165+
x int = 0
166+
)
167+
168+
const ( // foo
169+
170+
x int = 0
171+
172+
X int = 0
173+
)
174+
175+
const ( /* foo */
176+
177+
x int = 0
178+
)
179+
180+
const (
181+
182+
x int = 0
183+
)
184+
185+
const (
186+
187+
x int = 0
188+
X int = 0
189+
190+
)
191+
192+
const (
193+
x int = 0
194+
X int = 0
195+
x int = 0
196+
)
197+
198+
const (
199+
// Comment
200+
x int = 0
201+
// X.
202+
X int = 0
203+
// Comment
204+
x int = 0
205+
)
206+
100207
// This comment group should be separated
101208
// with a newline from the next comment
102209
// group.

0 commit comments

Comments
 (0)