diff options
| author | Robert Griesemer <gri@golang.org> | 2024-10-31 11:19:28 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-11-14 02:14:13 +0000 |
| commit | 2eac154b1c8b51d05fa5b110ae065d3610a61e06 (patch) | |
| tree | afecc69592011a077c1eef9e8539c8ea96826557 /src/cmd/compile/internal/syntax/parser.go | |
| parent | 3730814f2f2bf24550920c39a16841583de2dac1 (diff) | |
| download | go-2eac154b1c8b51d05fa5b110ae065d3610a61e06.tar.xz | |
cmd/compile: better error message when offending/missing token is a keyword
Prefix keywords (type, default, case, etc.) with "keyword" in error
messages to make them less ambiguous.
Fixes #68589.
Change-Id: I1eb92d1382f621b934167b3a4c335045da26be9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/623819
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tim King <taking@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax/parser.go')
| -rw-r--r-- | src/cmd/compile/internal/syntax/parser.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go index 77abdda867..14a737c414 100644 --- a/src/cmd/compile/internal/syntax/parser.go +++ b/src/cmd/compile/internal/syntax/parser.go @@ -300,7 +300,11 @@ func tokstring(tok token) string { case _Semi: return "semicolon or newline" } - return tok.String() + s := tok.String() + if _Break <= tok && tok <= _Var { + return "keyword " + s + } + return s } // Convenience methods using the current token position. @@ -2337,7 +2341,7 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS if p.tok != _Semi { // accept potential varDecl but complain if p.got(_Var) { - p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", tokstring(keyword))) + p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String())) } init = p.simpleStmt(nil, keyword) // If we have a range clause, we are done (can only happen for keyword == _For). |
