]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: allow one-method interfaces to be printed on a single line
authorTim Cooper <tim.cooper@layeh.com>
Tue, 26 Sep 2017 01:34:23 +0000 (22:34 -0300)
committerRobert Griesemer <gri@golang.org>
Tue, 3 Oct 2017 16:41:54 +0000 (16:41 +0000)
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>
src/go/printer/nodes.go
src/go/printer/testdata/expressions.golden
src/go/printer/testdata/expressions.input
src/go/printer/testdata/expressions.raw

index e9b110fe79f1f7499f84d532a4cabc304ab36a1e..37b5873b6817ceae17fbd32118e581f457056aaf 100644 (file)
@@ -398,22 +398,33 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool)
                        // no blank between keyword and {} in this case
                        p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
                        return
-               } else if isStruct && p.isOneLineFieldList(list) { // for now ignore interfaces
+               } else if p.isOneLineFieldList(list) {
                        // small enough - print on one line
                        // (don't use identList and ignore source line breaks)
                        p.print(lbrace, token.LBRACE, blank)
                        f := list[0]
-                       for i, x := range f.Names {
-                               if i > 0 {
-                                       // no comments so no need for comma position
-                                       p.print(token.COMMA, blank)
+                       if isStruct {
+                               for i, x := range f.Names {
+                                       if i > 0 {
+                                               // no comments so no need for comma position
+                                               p.print(token.COMMA, blank)
+                                       }
+                                       p.expr(x)
+                               }
+                               if len(f.Names) > 0 {
+                                       p.print(blank)
+                               }
+                               p.expr(f.Type)
+                       } else { // interface
+                               if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
+                                       // method
+                                       p.expr(f.Names[0])
+                                       p.signature(ftyp.Params, ftyp.Results)
+                               } else {
+                                       // embedded interface
+                                       p.expr(f.Type)
                                }
-                               p.expr(x)
-                       }
-                       if len(f.Names) > 0 {
-                               p.print(blank)
                        }
-                       p.expr(f.Type)
                        p.print(blank, rbrace, token.RBRACE)
                        return
                }
index 4c08a423dbb7fae6cccf525226922b91c19c4d7d..16a68c7bf7450ad7ca2b269fae6e8261e653a3f7 100644 (file)
@@ -290,6 +290,16 @@ func _() {
        _ = struct{ x, y, z int }{0, 1, 2}
        _ = struct{ int }{0}
        _ = struct{ s struct{ int } }{struct{ int }{0}}
+
+       _ = (interface{})(nil)
+       _ = (interface{ String() string })(nil)
+       _ = (interface {
+               String() string
+       })(nil)
+       _ = (interface{ fmt.Stringer })(nil)
+       _ = (interface {
+               fmt.Stringer
+       })(nil)
 }
 
 func _() {
index b3b8c2bdc64789dd264f47c196b5d3a8328f2d79..8c523b60221c9d6e5b39b993fd71ee3208d5fb14 100644 (file)
@@ -295,8 +295,17 @@ func _() {
        _ = struct{ x, y, z int }{0, 1, 2}
        _ = struct{ int }{0}
        _ = struct{ s struct { int } }{struct{ int}{0} }
-}
 
+       _ = (interface{})(nil)
+       _ = (interface{String() string})(nil)
+       _ = (interface{
+               String()    string
+       })(nil)
+       _ = (interface{fmt.Stringer})(nil)
+       _ = (interface{
+                   fmt.Stringer
+       })(nil)
+}
 
 func _() {
        // do not modify literals
index f121115e9df3c872063c16cd9688dd9a5ff8cc72..058fded4474ed3e8847d75182ffe783bdb4bbbd9 100644 (file)
@@ -290,6 +290,16 @@ func _() {
        _ = struct{ x, y, z int }{0, 1, 2}
        _ = struct{ int }{0}
        _ = struct{ s struct{ int } }{struct{ int }{0}}
+
+       _ = (interface{})(nil)
+       _ = (interface{ String() string })(nil)
+       _ = (interface {
+               String() string
+       })(nil)
+       _ = (interface{ fmt.Stringer })(nil)
+       _ = (interface {
+               fmt.Stringer
+       })(nil)
 }
 
 func _() {