1
0
mirror of https://github.com/golang/go synced 2024-11-16 21:04:45 -07:00

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 <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>
This commit is contained in:
Robert Griesemer 2022-08-25 21:10:36 -07:00 committed by Robert Griesemer
parent 489f508ccf
commit a74d46d8ff
3 changed files with 8 additions and 0 deletions

View 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
}

View 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) _()`,

View 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)