From 8323cef77cb4bc9c905aca7cd8b66655c8e3b3a2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 12 Mar 2013 13:07:15 -0700 Subject: [PATCH] go/printer, gofmt: avoid extra final comma in multi-line signatures 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 | 4 +-- .../go/printer/testdata/declarations.golden | 25 +++++++++++++++++++ .../go/printer/testdata/declarations.input | 25 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index ee0bbf1eda..7cd068e22e 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -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 { diff --git a/src/pkg/go/printer/testdata/declarations.golden b/src/pkg/go/printer/testdata/declarations.golden index 0ad72d349e..0331615e51 100644 --- a/src/pkg/go/printer/testdata/declarations.golden +++ b/src/pkg/go/printer/testdata/declarations.golden @@ -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 ) diff --git a/src/pkg/go/printer/testdata/declarations.input b/src/pkg/go/printer/testdata/declarations.input index 455c0c6c19..dbdbdfe742 100644 --- a/src/pkg/go/printer/testdata/declarations.input +++ b/src/pkg/go/printer/testdata/declarations.input @@ -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 ) -- 2.48.1