]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: parenthesize literal function types in conversions
authorRobert Griesemer <gri@golang.org>
Fri, 5 Oct 2012 04:03:50 +0000 (21:03 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 5 Oct 2012 04:03:50 +0000 (21:03 -0700)
Also: gofmt -w src misc

R=r
CC=golang-dev, iant
https://golang.org/cl/6591071

src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/expressions.golden
src/pkg/go/printer/testdata/expressions.input
src/pkg/go/printer/testdata/expressions.raw
src/pkg/reflect/all_test.go

index 01a7473b8350e3c0bad115018e820a6f1776a900..6eee9a9245c19ffed4de0c209dece5a0ba1c007c 100644 (file)
@@ -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)
index 45fa4d97a4a5ca89ba58cae0cc796ba0f6d4662a..4291c557ce2aa6e8c8e21779a817bd4164cd1a32 100644 (file)
@@ -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)
+}
index f545c66057ca93d00cc8b6b07f2476f62056017e..1ec12a0504970ad2e01081ca27e3c1dd07998701 100644 (file)
@@ -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)
+}
index 87a4b00836d168d3169734ca0ba5415e082bffcb..062900e072476501dfc0660dc43af727703d0123 100644 (file)
@@ -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)
+}
index 5dad071b3cc1ad3766c2d32daf21a50b7a3db4e6..5ddd6bef18c1d559ec547f59e1b6adbb02fc0792 100644 (file)
@@ -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)