Skip to content

Commit 92ba9b5

Browse files
Tim Coopergriesemer
Tim Cooper
authored andcommitted
go/printer: allow one-method interfaces to be printed on a single line
Previously, only the empty interface could be formatted to print on a single line. This behaviour made short one-method interfaces in function definitions and type assertions more verbose than they had to be. For example, the following type assertion: if c, ok := v.(interface { Close() error }); ok { } Can now be formatted as: if c, ok := v.(interface{ Close() error }); ok { } Fixes #21952 Change-Id: I896f796c5a30b9f4da2be3fe67cb6fea5871b835 Reviewed-on: https://go-review.googlesource.com/66130 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
1 parent e04ff3d commit 92ba9b5

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/go/printer/nodes.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,22 +398,33 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool)
398398
// no blank between keyword and {} in this case
399399
p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
400400
return
401-
} else if isStruct && p.isOneLineFieldList(list) { // for now ignore interfaces
401+
} else if p.isOneLineFieldList(list) {
402402
// small enough - print on one line
403403
// (don't use identList and ignore source line breaks)
404404
p.print(lbrace, token.LBRACE, blank)
405405
f := list[0]
406-
for i, x := range f.Names {
407-
if i > 0 {
408-
// no comments so no need for comma position
409-
p.print(token.COMMA, blank)
406+
if isStruct {
407+
for i, x := range f.Names {
408+
if i > 0 {
409+
// no comments so no need for comma position
410+
p.print(token.COMMA, blank)
411+
}
412+
p.expr(x)
413+
}
414+
if len(f.Names) > 0 {
415+
p.print(blank)
416+
}
417+
p.expr(f.Type)
418+
} else { // interface
419+
if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
420+
// method
421+
p.expr(f.Names[0])
422+
p.signature(ftyp.Params, ftyp.Results)
423+
} else {
424+
// embedded interface
425+
p.expr(f.Type)
410426
}
411-
p.expr(x)
412-
}
413-
if len(f.Names) > 0 {
414-
p.print(blank)
415427
}
416-
p.expr(f.Type)
417428
p.print(blank, rbrace, token.RBRACE)
418429
return
419430
}

src/go/printer/testdata/expressions.golden

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ func _() {
290290
_ = struct{ x, y, z int }{0, 1, 2}
291291
_ = struct{ int }{0}
292292
_ = struct{ s struct{ int } }{struct{ int }{0}}
293+
294+
_ = (interface{})(nil)
295+
_ = (interface{ String() string })(nil)
296+
_ = (interface {
297+
String() string
298+
})(nil)
299+
_ = (interface{ fmt.Stringer })(nil)
300+
_ = (interface {
301+
fmt.Stringer
302+
})(nil)
293303
}
294304

295305
func _() {

src/go/printer/testdata/expressions.input

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,17 @@ func _() {
295295
_ = struct{ x, y, z int }{0, 1, 2}
296296
_ = struct{ int }{0}
297297
_ = struct{ s struct { int } }{struct{ int}{0} }
298-
}
299298

299+
_ = (interface{})(nil)
300+
_ = (interface{String() string})(nil)
301+
_ = (interface{
302+
String() string
303+
})(nil)
304+
_ = (interface{fmt.Stringer})(nil)
305+
_ = (interface{
306+
fmt.Stringer
307+
})(nil)
308+
}
300309

301310
func _() {
302311
// do not modify literals

src/go/printer/testdata/expressions.raw

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ func _() {
290290
_ = struct{ x, y, z int }{0, 1, 2}
291291
_ = struct{ int }{0}
292292
_ = struct{ s struct{ int } }{struct{ int }{0}}
293+
294+
_ = (interface{})(nil)
295+
_ = (interface{ String() string })(nil)
296+
_ = (interface {
297+
String() string
298+
})(nil)
299+
_ = (interface{ fmt.Stringer })(nil)
300+
_ = (interface {
301+
fmt.Stringer
302+
})(nil)
293303
}
294304

295305
func _() {

0 commit comments

Comments
 (0)