]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer, gofmt: avoid extra final comma in multi-line signatures
authorRobert Griesemer <gri@golang.org>
Tue, 12 Mar 2013 20:07:15 +0000 (13:07 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 12 Mar 2013 20:07:15 +0000 (13:07 -0700)
The parameter list layout function was incorrectly computing the
end of the previous line in cases where a parameter type spanned
multiple lines. As a result, an extra (valid, but not needed)
comma was introduced before the paremeter list's closing parenthesis.

Fixes #4533.

R=rsc
CC=golang-dev
https://golang.org/cl/7674044

src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/declarations.golden
src/pkg/go/printer/testdata/declarations.input

index ee0bbf1eda9aba40d6c12cf30afda3ca02638d1e..7cd068e22ef45f754a9bec7a31c154b9715a248a 100644 (file)
@@ -271,12 +271,12 @@ func (p *printer) parameters(fields *ast.FieldList) {
                        // if there are multiple parameter names for this par
                        // or the type is on a separate line)
                        var parLineBeg int
-                       var parLineEnd = p.lineFor(par.Type.Pos())
                        if len(par.Names) > 0 {
                                parLineBeg = p.lineFor(par.Names[0].Pos())
                        } else {
-                               parLineBeg = parLineEnd
+                               parLineBeg = p.lineFor(par.Type.Pos())
                        }
+                       var parLineEnd = p.lineFor(par.Type.End())
                        // separating "," if needed
                        needsLinebreak := 0 < prevLine && prevLine < parLineBeg
                        if i > 0 {
index 0ad72d349ee440bcb1aa2753929054328350c2fa..0331615e5195bc99e7dd50d2fd6d9f9acab90157 100644 (file)
@@ -912,3 +912,28 @@ func _(x chan (<-chan int))
 func _(x chan<- (chan int))
 func _(x chan<- (chan int))
 func _(x chan<- (chan int))
+
+// don't introduce comma after last parameter if the closing ) is on the same line
+// even if the parameter type itself is multi-line (test cases from issue 4533)
+func _(...interface{})
+func _(...interface {
+       m()
+       n()
+})     // no extra comma between } and )
+
+func (t *T) _(...interface{})
+func (t *T) _(...interface {
+       m()
+       n()
+})     // no extra comma between } and )
+
+func _(interface{})
+func _(interface {
+       m()
+})     // no extra comma between } and )
+
+func _(struct{})
+func _(struct {
+       x       int
+       y       int
+})     // no extra comma between } and )
index 455c0c6c19f1e84f5d5ac8db580537fd5276a6e0..dbdbdfe7422db109a99c991a19e25ea2db256eba 100644 (file)
@@ -921,3 +921,28 @@ func _(x ((((chan(<-chan int))))))
 func _(x chan<-(chan int))
 func _(x (chan<-(chan int)))
 func _(x ((((chan<-(chan int))))))
+
+// don't introduce comma after last parameter if the closing ) is on the same line
+// even if the parameter type itself is multi-line (test cases from issue 4533)
+func _(...interface{})
+func _(...interface {
+       m()
+       n()
+}) // no extra comma between } and )
+
+func (t *T) _(...interface{})
+func (t *T) _(...interface {
+       m()
+       n()
+}) // no extra comma between } and )
+
+func _(interface{})
+func _(interface {
+       m()
+}) // no extra comma between } and )
+
+func _(struct{})
+func _(struct {
+       x int
+       y int
+}) // no extra comma between } and )