Commit 1065c6f6 authored by Robert Griesemer's avatar Robert Griesemer

go/printer: parenthesize literal function types in conversions

Also: gofmt -w src misc

R=r
CC=golang-dev, iant
https://golang.org/cl/6591071
parent 6c740e76
......@@ -791,7 +791,14 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if len(x.Args) > 1 {
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)
if x.Ellipsis.IsValid() {
p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis)
......
......@@ -647,3 +647,18 @@ func _() {
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)
}
......@@ -676,3 +676,18 @@ func _() {
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)
}
......@@ -647,3 +647,18 @@ func _() {
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)
}
......@@ -1494,7 +1494,7 @@ func TestMethod(t *testing.T) {
}
// Curried method of value.
tfunc := TypeOf(func(int) int(nil))
tfunc := TypeOf((func(int) int)(nil))
v := ValueOf(p).Method(1)
if tt := v.Type(); tt != tfunc {
t.Errorf("Value Method Type is %s; want %s", tt, tfunc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment