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: f291e1d5a6
GitHub-Pull-Request: golang/go#65840
Reviewed-on: https://go-review.googlesource.com/c/go/+/565518
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Zxilly 2024-02-22 21:09:55 +00:00 committed by Robert Griesemer
parent f326b3e2b3
commit 856355a913
8 changed files with 27 additions and 11 deletions

View File

@ -267,7 +267,9 @@ func (p *parser) syntaxErrorAt(pos Pos, msg string) {
// determine token string
var tok string
switch p.tok {
case _Name, _Semi:
case _Name:
tok = "`" + p.lit + "'"
case _Semi:
tok = p.lit
case _Literal:
tok = "literal " + p.lit

View File

@ -6,4 +6,4 @@
// Line 9 must end in EOF for this test (no newline).
package e
func([<-chan<-[func /* ERROR unexpected u */ u){go
func([<-chan<-[func /* ERROR unexpected `u' */ u){go

View File

@ -7,7 +7,7 @@ package p
func _() {
_ = m[] // ERROR expected operand
_ = m[x,]
_ = m[x /* ERROR unexpected a */ a b c d]
_ = m[x /* ERROR unexpected `a' */ a b c d]
}
// test case from the issue

View File

@ -7,7 +7,7 @@ package p
// test case from issue
type _ interface{
m /* ERROR unexpected int in interface type; possibly missing semicolon or newline or } */ int
m /* ERROR unexpected `int' in interface type; possibly missing semicolon or newline or } */ int
}
// other cases where the fix for this issue affects the error message
@ -16,12 +16,12 @@ const (
x int = 10 /* ERROR unexpected literal "foo" in grouped declaration; possibly missing semicolon or newline or \) */ "foo"
)
var _ = []int{1, 2, 3 /* ERROR unexpected int in composite literal; possibly missing comma or } */ int }
var _ = []int{1, 2, 3 /* ERROR unexpected `int' in composite literal; possibly missing comma or } */ int }
type _ struct {
x y /* ERROR syntax error: unexpected comma in struct type; possibly missing semicolon or newline or } */ ,
}
func f(a, b c /* ERROR unexpected d in parameter list; possibly missing comma or \) */ d) {
f(a, b, c /* ERROR unexpected d in argument list; possibly missing comma or \) */ d)
func f(a, b c /* ERROR unexpected `d' in parameter list; possibly missing comma or \) */ d) {
f(a, b, c /* ERROR unexpected `d' in argument list; possibly missing comma or \) */ d)
}

View File

@ -13,5 +13,5 @@ type _ interface {
(int) | (string)
(int) | ~(string)
(/* ERROR unexpected ~ */ ~int)
(int /* ERROR unexpected \| */ | /* ERROR unexpected string */ string /* ERROR unexpected \) */ )
(int /* ERROR unexpected \| */ | /* ERROR unexpected `string' */ string /* ERROR unexpected \) */ )
}

View File

@ -0,0 +1,14 @@
// Copyright 2023 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
import (
"fmt"
)
func f() {
int status // ERROR syntax error: unexpected `status' at end of statement
fmt.Println(status)
}

View File

@ -10,4 +10,4 @@
// there yet, so put it here for now. See also #20800.)
package e
func([<-chan<-[func u){go // ERROR "unexpected u"
func([<-chan<-[func u){go // ERROR "unexpected `u'"

View File

@ -9,9 +9,9 @@
package p
func f() {
if f() true { // ERROR "unexpected true, expected {"
if f() true { // ERROR "unexpected `true', expected {"
}
switch f() true { // ERROR "unexpected true, expected {"
switch f() true { // ERROR "unexpected `true', expected {"
}
}