Skip to content

Commit 2eac154

Browse files
griesemergopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent 3730814 commit 2eac154

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

src/cmd/compile/internal/syntax/parser.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,11 @@ func tokstring(tok token) string {
300300
case _Semi:
301301
return "semicolon or newline"
302302
}
303-
return tok.String()
303+
s := tok.String()
304+
if _Break <= tok && tok <= _Var {
305+
return "keyword " + s
306+
}
307+
return s
304308
}
305309

306310
// Convenience methods using the current token position.
@@ -2337,7 +2341,7 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
23372341
if p.tok != _Semi {
23382342
// accept potential varDecl but complain
23392343
if p.got(_Var) {
2340-
p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", tokstring(keyword)))
2344+
p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String()))
23412345
}
23422346
init = p.simpleStmt(nil, keyword)
23432347
// If we have a range clause, we are done (can only happen for keyword == _For).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
var _ (/* ERROR unexpected keyword type */ type T)

test/fixedbugs/issue11610.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
package a
1111
var? // ERROR "invalid character U\+003F '\?'|invalid character 0x3f in input file"
1212

13-
var x int // ERROR "unexpected var|expected identifier|expected type"
13+
var x int // ERROR "unexpected keyword var|expected identifier|expected type"
1414

1515
func main() {
1616
}

test/switch2.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ func f() {
2525

2626
switch {
2727
case 0: f(); case 0:
28-
case 0: f() case 0: // ERROR "unexpected case at end of statement"
28+
case 0: f() case 0: // ERROR "unexpected keyword case at end of statement"
2929
}
3030

3131
switch {
3232
case 0: f(); default:
33-
case 0: f() default: // ERROR "unexpected default at end of statement"
33+
case 0: f() default: // ERROR "unexpected keyword default at end of statement"
3434
}
3535

3636
switch {

test/syntax/semi7.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package main
88

99
func main() {
1010
if x { } // GCCGO_ERROR "undefined"
11-
else { } // ERROR "unexpected semicolon or newline before .?else.?|unexpected else"
11+
else { } // ERROR "unexpected semicolon or newline before .?else.?|unexpected keyword else"
1212
}
1313

1414

0 commit comments

Comments
 (0)