Skip to content

Commit

Permalink
[dev.go2go] go/printer: don't write "type" keyword in type parameter …
Browse files Browse the repository at this point in the history
…lists if brackets are used

When the printer mode UseBrackets is set, use the new unified parameter
list syntax: use []'s for type parameters without the "type" keyword.
This will also change gofmt behavior accordingly.

Fixes #41053.

Change-Id: Ibaf490ea9ed178684bea34da5b57afa823a6829f
Reviewed-on: https://go-review.googlesource.com/c/go/+/250998
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
griesemer committed Aug 27, 2020
1 parent 27c9d6e commit 3314879
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/go/printer/nodes.go
Expand Up @@ -325,7 +325,7 @@ func (p *printer) parameters(isTypeParam bool, fields *ast.FieldList) {
openTok, closeTok = token.LBRACK, token.RBRACK
}
p.print(fields.Opening, openTok)
if isTypeParam {
if isTypeParam && p.Mode&UseBrackets == 0 {
p.print(token.TYPE)
}
if len(fields.List) > 0 {
Expand All @@ -352,7 +352,7 @@ func (p *printer) parameters(isTypeParam bool, fields *ast.FieldList) {
if needsLinebreak && p.linebreak(parLineBeg, 0, ws, true) > 0 {
// break line if the opening "(" or previous parameter ended on a different line
ws = ignore
} else if isTypeParam && len(par.Names) > 0 || i > 0 {
} else if isTypeParam && len(par.Names) > 0 && p.Mode&UseBrackets == 0 || i > 0 {
p.print(blank)
}
// parameter names
Expand Down
2 changes: 1 addition & 1 deletion src/go/printer/printer.go
Expand Up @@ -1277,7 +1277,7 @@ const (
UseSpaces // use spaces instead of tabs for alignment
SourcePos // emit //line directives to preserve original source positions
StdFormat // apply standard formatting changes (exact byte output may change between versions of Go)
UseBrackets // use square brackets instead of parentheses for type parameters
UseBrackets // use square brackets instead of parentheses for type parameters (implies unified parameter syntax)
)

// A Config node controls the output of Fprint.
Expand Down
25 changes: 11 additions & 14 deletions src/go/printer/testdata/genericsB.golden
Expand Up @@ -4,27 +4,24 @@

package generics

type T[type] struct{}
type T[type P] struct{}
type T[type P1, P2, P3] struct{}
type T[P any] struct{}
type T[P1, P2, P3 any] struct{}

type T[type P C] struct{}
type T[type P1, P2, P3, C] struct{}
type T[P C] struct{}
type T[P1, P2, P3, C] struct{}

type T[type P C[P]] struct{}
type T[type P1, P2, P3 C[P1, P2, P3]] struct{}
type T[P C[P]] struct{}
type T[P1, P2, P3 C[P1, P2, P3]] struct{}

func f[type]()
func f[type P](x P)
func f[type P1, P2, P3](x1 P1, x2 P2, x3 P3) struct{}
func f[P any](x P)
func f[P1, P2, P3 any](x1 P1, x2 P2, x3 P3) struct{}

func f[type]()
func f[type P interface{}](x P)
func f[type P1, P2, P3 interface {
func f[P interface{}](x P)
func f[P1, P2, P3 interface {
m1(P1)
type P2, P3
}](x1 P1, x2 P2, x3 P3) struct{}
func f[type P](T1[P], T2[P]) T3[P]
func f[P any](T1[P], T2[P]) T3[P]

func (x T[P]) m()
func (T[P]) m(x T[P]) P
Expand Down
25 changes: 11 additions & 14 deletions src/go/printer/testdata/genericsB.input
Expand Up @@ -4,24 +4,21 @@

package generics

type T[type] struct{}
type T[type P] struct{}
type T[type P1, P2, P3] struct{}
type T[P any] struct{}
type T[P1, P2, P3 any] struct{}

type T[type P C] struct{}
type T[type P1, P2, P3, C] struct{}
type T[P C] struct{}
type T[P1, P2, P3, C] struct{}

type T[type P C[P]] struct{}
type T[type P1, P2, P3 C[P1, P2, P3]] struct{}
type T[P C[P]] struct{}
type T[P1, P2, P3 C[P1, P2, P3]] struct{}

func f[type]()
func f[type P](x P)
func f[type P1, P2, P3](x1 P1, x2 P2, x3 P3) struct{}
func f[P any](x P)
func f[P1, P2, P3 any](x1 P1, x2 P2, x3 P3) struct{}

func f[type]()
func f[type P interface{}](x P)
func f[type P1, P2, P3 interface{ m1(P1); type P2, P3 }](x1 P1, x2 P2, x3 P3) struct{}
func f[type P](T1[P], T2[P]) T3[P]
func f[P interface{}](x P)
func f[P1, P2, P3 interface{ m1(P1); type P2, P3 }](x1 P1, x2 P2, x3 P3) struct{}
func f[P any](T1[P], T2[P]) T3[P]

func (x T[P]) m()
func ((T[P])) m(x T[P]) P
Expand Down

0 comments on commit 3314879

Please sign in to comment.