Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/parser, go/types: accepts invalid non-if/block statements after "else" #13475

mdempsky opened this issue Dec 4, 2015 · 3 comments


Copy link

mdempsky commented Dec 4, 2015

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

Copy link

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

@griesemer griesemer self-assigned this Dec 4, 2015
@griesemer griesemer added this to the Go1.6 milestone Dec 4, 2015
Copy link

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.

Copy link

CL mentions this issue.

@golang golang locked and limited conversation to collaborators Dec 14, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

3 participants