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

Closed
mdempsky opened this issue Dec 4, 2015 · 3 comments
Closed
Milestone

Comments

@mdempsky
Copy link
Member

mdempsky commented Dec 4, 2015

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

@griesemer
Copy link
Contributor

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
@griesemer
Copy link
Contributor

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.

@gopherbot
Copy link

CL https://golang.org/cl/17408 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.
Projects
None yet
Development

No branches or pull requests

3 participants