]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: better error message when offending/missing token is a keyword
authorRobert Griesemer <gri@golang.org>
Thu, 31 Oct 2024 18:19:28 +0000 (11:19 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 14 Nov 2024 02:14:13 +0000 (02:14 +0000)
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>
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/testdata/issue68589.go [new file with mode: 0644]
test/fixedbugs/issue11610.go
test/switch2.go
test/syntax/semi7.go

index 77abdda867ba8f83d044b2b6e06b1b7626c84902..14a737c414988eaae89929e31eb8d4343eceba8a 100644 (file)
@@ -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).
diff --git a/src/cmd/compile/internal/syntax/testdata/issue68589.go b/src/cmd/compile/internal/syntax/testdata/issue68589.go
new file mode 100644 (file)
index 0000000..701815a
--- /dev/null
@@ -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)
index 8d68c98f2d437d543d2294baf34edcf361459455..ec4d8e84c14b0919e89d705542344970b8309c0d 100644 (file)
@@ -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() {
 }
index 66e89fda19e2c3079b5b453d1ed5d2df7a25bca5..113d81be854fc6436632bc71181882dd37b8cdf9 100644 (file)
@@ -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 {
index a1948b0f7d38b6ed93aef91ccc1cc1fd60bed20b..acd2f83597391ab708814f2a0629926ccbb31dd3 100644 (file)
@@ -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"
 }