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

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

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

Comments

@mdempsky
Copy link
Member

@mdempsky 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

@griesemer griesemer commented Dec 4, 2015

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

@griesemer griesemer commented Dec 4, 2015

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

@gopherbot gopherbot commented Dec 4, 2015

CL https://golang.org/cl/17408 mentions this issue.

@griesemer griesemer closed this in 670642d Dec 7, 2015
@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
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.