]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use quotes to wrap user-supplied token
authorZxilly <zhouxinyu1001@gmail.com>
Thu, 22 Feb 2024 21:09:55 +0000 (21:09 +0000)
committerRobert Griesemer <gri@google.com>
Tue, 27 Feb 2024 16:22:24 +0000 (16:22 +0000)
Use quotes to wrap user-supplied token in the syntax error message.
Updates #65790

Change-Id: I631a63df4a6bb8615b7850a324d812190bc15f30
GitHub-Last-Rev: f291e1d5a6adee558d21bb7e0a3a17471bad7eb6
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>

src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/testdata/issue20789.go
src/cmd/compile/internal/syntax/testdata/issue47704.go
src/cmd/compile/internal/syntax/testdata/issue49205.go
src/cmd/compile/internal/syntax/testdata/issue52391.go
src/cmd/compile/internal/syntax/testdata/issue65970.go [new file with mode: 0644]
test/fixedbugs/issue20789.go
test/fixedbugs/issue23664.go

index 1569b5e9872e1eab970c56ff81cd3675898319dc..f75f86587a658d42af3e96a6085e44304a19549a 100644 (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
index 0d5988b9a608c21316be48ba481f5f81a4994985..8a6db6d2ee0b81a08f11c85c64f5c31ef7e9a8c4 100644 (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
\ No newline at end of file
+func([<-chan<-[func /* ERROR unexpected `u' */ u){go
\ No newline at end of file
index e4cdad148f62c01c8767b8c3c96cd0851f3ac6c4..aab37905607c40aae3e6dc3cdc5a960c7d33568a 100644 (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
index bbcc950c5c790548e9991f303690f58e0e4eb360..9b6c76970380c0f8aa2871256075ac852c0f6282 100644 (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)
 }
index f2098ceadb2c467f21afcfbbebf6af1c849ac700..42b71cc83a315a9bc02c31711fad968b42a0affa 100644 (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 \) */ )
 }
diff --git a/src/cmd/compile/internal/syntax/testdata/issue65970.go b/src/cmd/compile/internal/syntax/testdata/issue65970.go
new file mode 100644 (file)
index 0000000..07ffd12
--- /dev/null
@@ -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)
+}
index 4e4eed42a7ff4bdcdf25f3118b5217b4d7547ed5..cba129095773ca27cc8b5339b59a2b0f8f4ba01f 100644 (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"
\ No newline at end of file
+func([<-chan<-[func u){go // ERROR "unexpected `u'"
\ No newline at end of file
index 715654be70deb59ae48009dd3d8d9b8b29d1808d..fe171c27d0497ea4d70fd6a74f8182c6ee0bfeed 100644 (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 {"
        }
 }