mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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/+/169958 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
9da6530faa
commit
4145c5da1f
2 changed files with 34 additions and 4 deletions
|
|
@ -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
|
||||
|
||||
|
|
@ -514,7 +528,7 @@ func (p *parser) constDecl(group *Group) Decl {
|
|||
d.NameList = p.nameList(p.name())
|
||||
if p.tok != _EOF && p.tok != _Semi && p.tok != _Rparen {
|
||||
d.Type = p.typeOrNil()
|
||||
if p.got(_Assign) {
|
||||
if p.gotAssign() {
|
||||
d.Values = p.exprList()
|
||||
}
|
||||
}
|
||||
|
|
@ -533,7 +547,7 @@ func (p *parser) typeDecl(group *Group) Decl {
|
|||
d.pos = p.pos()
|
||||
|
||||
d.Name = p.name()
|
||||
d.Alias = p.got(_Assign)
|
||||
d.Alias = p.gotAssign()
|
||||
d.Type = p.typeOrNil()
|
||||
if d.Type == nil {
|
||||
d.Type = p.bad()
|
||||
|
|
@ -556,11 +570,11 @@ func (p *parser) varDecl(group *Group) Decl {
|
|||
d.pos = p.pos()
|
||||
|
||||
d.NameList = p.nameList(p.name())
|
||||
if p.got(_Assign) {
|
||||
if p.gotAssign() {
|
||||
d.Values = p.exprList()
|
||||
} else {
|
||||
d.Type = p.type_()
|
||||
if p.got(_Assign) {
|
||||
if p.gotAssign() {
|
||||
d.Values = p.exprList()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue