]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: check that go/defer expressions are not parenthesized
authorRobert Griesemer <gri@golang.org>
Fri, 26 Aug 2022 04:10:36 +0000 (21:10 -0700)
committerRobert Griesemer <gri@google.com>
Thu, 1 Sep 2022 23:18:01 +0000 (23:18 +0000)
Logic matches the code in the syntax package.
This error was missing from go/parser and go/types.
Added some tests.

For #54511.

Change-Id: I418de4bd4c7169457b424366caae70227a92a761
Reviewed-on: https://go-review.googlesource.com/c/go/+/425795
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/go/parser/parser.go
src/go/parser/short_test.go
src/go/types/testdata/check/stmt0.go

index 3d4d83c4a41d9bb894a3c9eb5883812b6047ebea..26ba7b2892239562ce20e0c22f0d2a4540cf5848 100644 (file)
@@ -1928,6 +1928,10 @@ func (p *parser) checkAssignStmt(as *ast.AssignStmt) {
 
 func (p *parser) parseCallExpr(callType string) *ast.CallExpr {
        x := p.parseRhs() // could be a conversion: (some type)(x)
+       if t := unparen(x); t != x {
+               p.error(x.Pos(), fmt.Sprintf("expression in %s must not be parenthesized", callType))
+               x = t
+       }
        if call, isCall := x.(*ast.CallExpr); isCall {
                return call
        }
index 2d9016aaddc7dd90bb6d559b510fbd8655d95bad..ea8b087bae12b5c146607d2cdc66a02bb990cec9 100644 (file)
@@ -159,7 +159,9 @@ var invalids = []string{
        `package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`,
        `package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
        `package p; func f() { go f /* ERROR HERE "must be function call" */ }`,
+       `package p; func f() { go ( /* ERROR "must not be parenthesized" */ f()) }`,
        `package p; func f() { defer func() {} /* ERROR HERE "must be function call" */ }`,
+       `package p; func f() { defer ( /* ERROR "must not be parenthesized" */ f()) }`,
        `package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`,
        `package p; func _() (type /* ERROR "found 'type'" */ T)(T)`,
        `package p; func (type /* ERROR "found 'type'" */ T)(T) _()`,
index 0caebcf544fced402fc5aecec5c2cf719b042b25..14a37c1ed933ad39bcad2ece181ab29ed4e6e54c 100644 (file)
@@ -231,6 +231,7 @@ func selects() {
 func gos() {
        go 1; /* ERROR "must be function call" */
        go int /* ERROR "go requires function call, not conversion" */ (0)
+       go ( /* ERROR expression in go must not be parenthesized */ gos())
        go gos()
        var c chan int
        go close(c)
@@ -240,6 +241,7 @@ func gos() {
 func defers() {
        defer 1; /* ERROR "must be function call" */
        defer int /* ERROR "defer requires function call, not conversion" */ (0)
+       defer ( /* ERROR expression in defer must not be parenthesized */ defers())
        defer defers()
        var c chan int
        defer close(c)