This is because go/parser.(*parser).parseIfStmt simply calls go/parser.(*parser).parseStmt after consuming the ELSE token, without checking that the next token is an IF or { token. go/parser's package documentation does warn that go/parser accepts a superset of the Go grammar, so arguably this is okay for go/parser, but then I think go/types or at least go/types/gotype.go should reject it.
(Notably, cmd/gofmt inserts the necessary braces to make the code syntactically valid.)
Definitively a bug. This should not be accepted by the parser. Also, the type checker needs to double-check since the AST may have been manipulated manually and the else_ branch can contain any statement.
(This is ironic given the fact that I was fighting hard to only permit else { and else if in this case... - the original language permitted a statement. I am surprised I didn't fix this.)
PS: I think I did the parser change on purpose to permit better error recovery (but then didn't add the necessary check to go/types). It's probably better to enforce syntax here since it's easy, and the syntax is very clear and unambiguous. Fix forthcoming.
http://play.golang.org/p/CbXFobPaOa
This is because go/parser.(*parser).parseIfStmt simply calls go/parser.(*parser).parseStmt after consuming the ELSE token, without checking that the next token is an IF or { token. go/parser's package documentation does warn that go/parser accepts a superset of the Go grammar, so arguably this is okay for go/parser, but then I think go/types or at least go/types/gotype.go should reject it.
(Notably, cmd/gofmt inserts the necessary braces to make the code syntactically valid.)
CC @griesemer
The text was updated successfully, but these errors were encountered: