diff options
author | Robert Griesemer <gri@golang.org> | 2015-12-03 17:28:46 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2015-12-07 21:36:31 +0000 |
commit | 670642d389a3c1a90fad6016f91197125969376d (patch) | |
tree | c344c5658d0a942fac1d66843fca76bc4a46cc62 /src/go/parser/parser.go | |
parent | c5aa53c8c52ec895d7e7c18a8fab9c5786555c1a (diff) | |
download | go-670642d389a3c1a90fad6016f91197125969376d.tar.gz go-670642d389a3c1a90fad6016f91197125969376d.zip |
go/parser, go/types: report invalid else branch in if statements
- Only accept valid if statement syntax in go/parser.
- Check AST again in go/types since it may have been modified and the
AST doesn't preclude other statements in the else branch of an if
statement.
- Removed a test from gofmt which verified that old-style if statements
permitting any statement in the else branch were correctly reformatted.
It's been years since we switched to the current syntax; no need to
support this anymore.
- Added a comment to go/printer.
Fixes #13475.
Change-Id: Id2c8fbcc68b719cd511027d0412a37266cceed6b
Reviewed-on: https://go-review.googlesource.com/17408
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/go/parser/parser.go')
-rw-r--r-- | src/go/parser/parser.go | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 73edaa0ab3..f3a26032ee 100644 --- a/src/go/parser/parser.go +++ b/src/go/parser/parser.go @@ -1857,7 +1857,16 @@ func (p *parser) parseIfStmt() *ast.IfStmt { var else_ ast.Stmt if p.tok == token.ELSE { p.next() - else_ = p.parseStmt() + switch p.tok { + case token.IF: + else_ = p.parseIfStmt() + case token.LBRACE: + else_ = p.parseBlockStmt() + p.expectSemi() + default: + p.errorExpected(p.pos, "if statement or block") + else_ = &ast.BadStmt{From: p.pos, To: p.pos} + } } else { p.expectSemi() } |