From: Robert Griesemer Date: Fri, 26 Aug 2022 04:10:36 +0000 (-0700) Subject: go/parser: check that go/defer expressions are not parenthesized X-Git-Tag: go1.20rc1~1286 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a74d46d8ff44abe409282bf1a9a9ab1b0b2cdaa0;p=gostls13.git go/parser: check that go/defer expressions are not parenthesized 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 TryBot-Result: Gopher Robot Run-TryBot: Robert Griesemer Reviewed-by: Alan Donovan Reviewed-by: Robert Griesemer --- diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 3d4d83c4a4..26ba7b2892 100644 --- a/src/go/parser/parser.go +++ b/src/go/parser/parser.go @@ -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 } diff --git a/src/go/parser/short_test.go b/src/go/parser/short_test.go index 2d9016aadd..ea8b087bae 100644 --- a/src/go/parser/short_test.go +++ b/src/go/parser/short_test.go @@ -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) _()`, diff --git a/src/go/types/testdata/check/stmt0.go b/src/go/types/testdata/check/stmt0.go index 0caebcf544..14a37c1ed9 100644 --- a/src/go/types/testdata/check/stmt0.go +++ b/src/go/types/testdata/check/stmt0.go @@ -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)