Commit 4145c5da authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: better recovery after := (rather than =) in declarations

Before this fix, a mistaken := in a (const/type/var) declaration
ended that declaration with an error from which the parser didn't
recover well. This low-cost change will provide a better error
message and lets the parser recover perfectly.

Fixes #31092.

Change-Id: Ic4f94dc5e29dd00b7ef6d53a80dded638e3cea80
Reviewed-on: https://go-review.googlesource.com/c/go/+/169958Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent 9da6530f
...@@ -170,6 +170,20 @@ func (p *parser) want(tok token) { ...@@ -170,6 +170,20 @@ func (p *parser) want(tok token) {
} }
} }
// gotAssign is like got(_Assign) but it also accepts ":="
// (and reports an error) for better parser error recovery.
func (p *parser) gotAssign() bool {
switch p.tok {
case _Define:
p.syntaxError("expecting =")
fallthrough
case _Assign:
p.next()
return true
}
return false
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Error handling // Error handling
...@@ -514,7 +528,7 @@ func (p *parser) constDecl(group *Group) Decl { ...@@ -514,7 +528,7 @@ func (p *parser) constDecl(group *Group) Decl {
d.NameList = p.nameList(p.name()) d.NameList = p.nameList(p.name())
if p.tok != _EOF && p.tok != _Semi && p.tok != _Rparen { if p.tok != _EOF && p.tok != _Semi && p.tok != _Rparen {
d.Type = p.typeOrNil() d.Type = p.typeOrNil()
if p.got(_Assign) { if p.gotAssign() {
d.Values = p.exprList() d.Values = p.exprList()
} }
} }
...@@ -533,7 +547,7 @@ func (p *parser) typeDecl(group *Group) Decl { ...@@ -533,7 +547,7 @@ func (p *parser) typeDecl(group *Group) Decl {
d.pos = p.pos() d.pos = p.pos()
d.Name = p.name() d.Name = p.name()
d.Alias = p.got(_Assign) d.Alias = p.gotAssign()
d.Type = p.typeOrNil() d.Type = p.typeOrNil()
if d.Type == nil { if d.Type == nil {
d.Type = p.bad() d.Type = p.bad()
...@@ -556,11 +570,11 @@ func (p *parser) varDecl(group *Group) Decl { ...@@ -556,11 +570,11 @@ func (p *parser) varDecl(group *Group) Decl {
d.pos = p.pos() d.pos = p.pos()
d.NameList = p.nameList(p.name()) d.NameList = p.nameList(p.name())
if p.got(_Assign) { if p.gotAssign() {
d.Values = p.exprList() d.Values = p.exprList()
} else { } else {
d.Type = p.type_() d.Type = p.type_()
if p.got(_Assign) { if p.gotAssign() {
d.Values = p.exprList() d.Values = p.exprList()
} }
} }
......
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test cases for issue 31092: Better synchronization of
// parser after seeing an := rather than an = in a const,
// type, or variable declaration.
package p
const _ /* ERROR unexpected := */ := 0
type _ /* ERROR unexpected := */ := int
var _ /* ERROR unexpected := */ := 0
const _ int /* ERROR unexpected := */ := 0
var _ int /* ERROR unexpected := */ := 0
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment