Skip to content

Commit 2f37939

Browse files
committed
go/parser: improve error recovery from invalid selector exprs
Before this CL, the parser consumed the next token following an invalid selector expr no matter what it was. This leads to poor error recovery when this next token is a closing delimiter or other reasonable element of a stop set. As a side-effect, x/tools tests broke when parser logic for type parameters was introduced, as they threw off the parser synchronization to the point where the x/tools test bailed out. This CL introduces a targeted fix that allows the x/tools tests to pass. More general improvement for parser error recovery should be done for go1.17. Change-Id: I44d73d34b6063e62d16a23d24ab7cbce6500239d Reviewed-on: https://go-review.googlesource.com/c/go/+/293792 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 8654db4 commit 2f37939

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/go/parser/parser.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,14 @@ func (p *parser) parsePrimaryExpr(lhs bool) (x ast.Expr) {
17541754
default:
17551755
pos := p.pos
17561756
p.errorExpected(pos, "selector or type assertion")
1757-
p.next() // make progress
1757+
// TODO(rFindley) The check for token.RBRACE below is a targeted fix
1758+
// to error recovery sufficient to make the x/tools tests to
1759+
// pass with the new parsing logic introduced for type
1760+
// parameters. Remove this once error recovery has been
1761+
// more generally reconsidered.
1762+
if p.tok != token.RBRACE {
1763+
p.next() // make progress
1764+
}
17581765
sel := &ast.Ident{NamePos: pos, Name: "_"}
17591766
x = &ast.SelectorExpr{X: x, Sel: sel}
17601767
}

0 commit comments

Comments
 (0)