cmd/compile/internal/gc: better error message for parenthesized go/defer exprs

Change-Id: Ie24d56422ae2196198a6c306716fa867c1442d6e
Reviewed-on: https://go-review.googlesource.com/17043
Reviewed-by: Chris Manghane <cmang@golang.org>
This commit is contained in:
Robert Griesemer 2015-11-18 11:24:48 -08:00
parent 5500d46914
commit fe762b6466
2 changed files with 17 additions and 8 deletions

View file

@ -1391,13 +1391,18 @@ func (p *parser) pseudocall() *Node {
defer p.trace("pseudocall")()
}
// The expression in go/defer must not be parenthesized;
// don't drop ()'s so we can report an error.
x := p.pexpr(true /* keep_parens */)
if x.Op != OCALL {
Yyerror("argument to go/defer must be function call")
x := p.pexpr(true) // keep_parens so we can report error below
switch x.Op {
case OCALL:
return x
case OPAREN:
Yyerror("expression in go/defer must not be parenthesized")
// already progressed, no need to advance
default:
Yyerror("expression in go/defer must be function call")
// already progressed, no need to advance
}
return x
return nil
}
// go.y:pexpr (partial)

View file

@ -19,8 +19,12 @@ type S struct {
}
func F() {
go (F()) // ERROR "must be function call"
defer (F()) // ERROR "must be function call"
go F // ERROR "must be function call"
defer F // ERROR "must be function call"
go (F) // ERROR "must be function call|must not be parenthesized"
defer (F) // ERROR "must be function call|must not be parenthesized"
go (F()) // ERROR "must be function call|must not be parenthesized"
defer (F()) // ERROR "must be function call|must not be parenthesized"
var s S
(&s.t).F()
go (&s.t).F()