mirror of
https://github.com/golang/go
synced 2024-11-24 23:57:57 -07:00
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>
This commit is contained in:
parent
3730814f2f
commit
2eac154b1c
@ -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).
|
||||
|
7
src/cmd/compile/internal/syntax/testdata/issue68589.go
vendored
Normal file
7
src/cmd/compile/internal/syntax/testdata/issue68589.go
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
// Copyright 2024 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.
|
||||
|
||||
package p
|
||||
|
||||
var _ (/* ERROR unexpected keyword type */ type T)
|
@ -10,7 +10,7 @@
|
||||
package a
|
||||
var? // ERROR "invalid character U\+003F '\?'|invalid character 0x3f in input file"
|
||||
|
||||
var x int // ERROR "unexpected var|expected identifier|expected type"
|
||||
var x int // ERROR "unexpected keyword var|expected identifier|expected type"
|
||||
|
||||
func main() {
|
||||
}
|
||||
|
@ -25,12 +25,12 @@ func f() {
|
||||
|
||||
switch {
|
||||
case 0: f(); case 0:
|
||||
case 0: f() case 0: // ERROR "unexpected case at end of statement"
|
||||
case 0: f() case 0: // ERROR "unexpected keyword case at end of statement"
|
||||
}
|
||||
|
||||
switch {
|
||||
case 0: f(); default:
|
||||
case 0: f() default: // ERROR "unexpected default at end of statement"
|
||||
case 0: f() default: // ERROR "unexpected keyword default at end of statement"
|
||||
}
|
||||
|
||||
switch {
|
||||
|
@ -8,7 +8,7 @@ package main
|
||||
|
||||
func main() {
|
||||
if x { } // GCCGO_ERROR "undefined"
|
||||
else { } // ERROR "unexpected semicolon or newline before .?else.?|unexpected else"
|
||||
else { } // ERROR "unexpected semicolon or newline before .?else.?|unexpected keyword else"
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user