Skip to content

Commit 856355a

Browse files
ZxillyRobert Griesemer
authored and
Robert Griesemer
committed
cmd/compile: use quotes to wrap user-supplied token
Use quotes to wrap user-supplied token in the syntax error message. Updates #65790 Change-Id: I631a63df4a6bb8615b7850a324d812190bc15f30 GitHub-Last-Rev: f291e1d GitHub-Pull-Request: #65840 Reviewed-on: https://go-review.googlesource.com/c/go/+/565518 Reviewed-by: Carlos Amedee <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f326b3e commit 856355a

File tree

8 files changed

+27
-11
lines changed

8 files changed

+27
-11
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ func (p *parser) syntaxErrorAt(pos Pos, msg string) {
267267
// determine token string
268268
var tok string
269269
switch p.tok {
270-
case _Name, _Semi:
270+
case _Name:
271+
tok = "`" + p.lit + "'"
272+
case _Semi:
271273
tok = p.lit
272274
case _Literal:
273275
tok = "literal " + p.lit

src/cmd/compile/internal/syntax/testdata/issue20789.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
// Line 9 must end in EOF for this test (no newline).
77

88
package e
9-
func([<-chan<-[func /* ERROR unexpected u */ u){go
9+
func([<-chan<-[func /* ERROR unexpected `u' */ u){go

src/cmd/compile/internal/syntax/testdata/issue47704.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package p
77
func _() {
88
_ = m[] // ERROR expected operand
99
_ = m[x,]
10-
_ = m[x /* ERROR unexpected a */ a b c d]
10+
_ = m[x /* ERROR unexpected `a' */ a b c d]
1111
}
1212

1313
// test case from the issue

src/cmd/compile/internal/syntax/testdata/issue49205.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package p
77
// test case from issue
88

99
type _ interface{
10-
m /* ERROR unexpected int in interface type; possibly missing semicolon or newline or } */ int
10+
m /* ERROR unexpected `int' in interface type; possibly missing semicolon or newline or } */ int
1111
}
1212

1313
// other cases where the fix for this issue affects the error message
@@ -16,12 +16,12 @@ const (
1616
x int = 10 /* ERROR unexpected literal "foo" in grouped declaration; possibly missing semicolon or newline or \) */ "foo"
1717
)
1818

19-
var _ = []int{1, 2, 3 /* ERROR unexpected int in composite literal; possibly missing comma or } */ int }
19+
var _ = []int{1, 2, 3 /* ERROR unexpected `int' in composite literal; possibly missing comma or } */ int }
2020

2121
type _ struct {
2222
x y /* ERROR syntax error: unexpected comma in struct type; possibly missing semicolon or newline or } */ ,
2323
}
2424

25-
func f(a, b c /* ERROR unexpected d in parameter list; possibly missing comma or \) */ d) {
26-
f(a, b, c /* ERROR unexpected d in argument list; possibly missing comma or \) */ d)
25+
func f(a, b c /* ERROR unexpected `d' in parameter list; possibly missing comma or \) */ d) {
26+
f(a, b, c /* ERROR unexpected `d' in argument list; possibly missing comma or \) */ d)
2727
}

src/cmd/compile/internal/syntax/testdata/issue52391.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ type _ interface {
1313
(int) | (string)
1414
(int) | ~(string)
1515
(/* ERROR unexpected ~ */ ~int)
16-
(int /* ERROR unexpected \| */ | /* ERROR unexpected string */ string /* ERROR unexpected \) */ )
16+
(int /* ERROR unexpected \| */ | /* ERROR unexpected `string' */ string /* ERROR unexpected \) */ )
1717
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2023 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+
import (
8+
"fmt"
9+
)
10+
11+
func f() {
12+
int status // ERROR syntax error: unexpected `status' at end of statement
13+
fmt.Println(status)
14+
}

test/fixedbugs/issue20789.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
// there yet, so put it here for now. See also #20800.)
1111

1212
package e
13-
func([<-chan<-[func u){go // ERROR "unexpected u"
13+
func([<-chan<-[func u){go // ERROR "unexpected `u'"

test/fixedbugs/issue23664.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
package p
1010

1111
func f() {
12-
if f() true { // ERROR "unexpected true, expected {"
12+
if f() true { // ERROR "unexpected `true', expected {"
1313
}
1414

15-
switch f() true { // ERROR "unexpected true, expected {"
15+
switch f() true { // ERROR "unexpected `true', expected {"
1616
}
1717
}

0 commit comments

Comments
 (0)