1
0
mirror of https://github.com/golang/go synced 2024-11-22 21:10:03 -07:00

go/printer: parenthesize literal function types in conversions

Also: gofmt -w src misc

R=r
CC=golang-dev, iant
https://golang.org/cl/6591071
This commit is contained in:
Robert Griesemer 2012-10-04 21:03:50 -07:00
parent 6c740e769f
commit 1065c6f65a
5 changed files with 54 additions and 2 deletions

View File

@ -791,7 +791,14 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if len(x.Args) > 1 { if len(x.Args) > 1 {
depth++ depth++
} }
p.expr1(x.Fun, token.HighestPrec, depth) if _, ok := x.Fun.(*ast.FuncType); ok {
// conversions to literal function types require parentheses around the type
p.print(token.LPAREN)
p.expr1(x.Fun, token.HighestPrec, depth)
p.print(token.RPAREN)
} else {
p.expr1(x.Fun, token.HighestPrec, depth)
}
p.print(x.Lparen, token.LPAREN) p.print(x.Lparen, token.LPAREN)
if x.Ellipsis.IsValid() { if x.Ellipsis.IsValid() {
p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis) p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis)

View File

@ -647,3 +647,18 @@ func _() {
a..., a...,
) )
} }
// Literal function types in conversions must be parenthesized;
// for now go/parser accepts the unparenthesized form where it
// is non-ambiguous.
func _() {
// these conversions should be rewritten to look
// the same as the parenthesized conversions below
_ = (func())(nil)
_ = (func(x int) float)(nil)
_ = (func() func() func())(nil)
_ = (func())(nil)
_ = (func(x int) float)(nil)
_ = (func() func() func())(nil)
}

View File

@ -676,3 +676,18 @@ func _() {
a..., a...,
) )
} }
// Literal function types in conversions must be parenthesized;
// for now go/parser accepts the unparenthesized form where it
// is non-ambiguous.
func _() {
// these conversions should be rewritten to look
// the same as the parenthesized conversions below
_ = func()()(nil)
_ = func(x int)(float)(nil)
_ = func() func() func()()(nil)
_ = (func()())(nil)
_ = (func(x int)(float))(nil)
_ = (func() func() func()())(nil)
}

View File

@ -647,3 +647,18 @@ func _() {
a..., a...,
) )
} }
// Literal function types in conversions must be parenthesized;
// for now go/parser accepts the unparenthesized form where it
// is non-ambiguous.
func _() {
// these conversions should be rewritten to look
// the same as the parenthesized conversions below
_ = (func())(nil)
_ = (func(x int) float)(nil)
_ = (func() func() func())(nil)
_ = (func())(nil)
_ = (func(x int) float)(nil)
_ = (func() func() func())(nil)
}

View File

@ -1494,7 +1494,7 @@ func TestMethod(t *testing.T) {
} }
// Curried method of value. // Curried method of value.
tfunc := TypeOf(func(int) int(nil)) tfunc := TypeOf((func(int) int)(nil))
v := ValueOf(p).Method(1) v := ValueOf(p).Method(1)
if tt := v.Type(); tt != tfunc { if tt := v.Type(); tt != tfunc {
t.Errorf("Value Method Type is %s; want %s", tt, tfunc) t.Errorf("Value Method Type is %s; want %s", tt, tfunc)