1
0
mirror of https://github.com/golang/go synced 2024-11-19 01:44:40 -07:00

go/scanner: give specific error for curvy “abc” quotes

Code examples sometimes mistakenly use curvy quotes,
leading to hard-to-spot invalid token errors.
This change makes the error message explicit.

(An alternative change would be to accept them in place
of "abc" and emit an error, but the extra check would
likely add an unacceptable dynamic cost to string scanning.)

Fixes #61450

Change-Id: Ie2b18c958c6f8f71a56ac193a94a8d16eea839db
Reviewed-on: https://go-review.googlesource.com/c/go/+/512855
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Alan Donovan 2023-07-25 12:47:05 +01:00
parent 7e7dd4dcd9
commit faf564644d
2 changed files with 8 additions and 1 deletions

View File

@ -943,7 +943,13 @@ scanAgain:
default: default:
// next reports unexpected BOMs - don't repeat // next reports unexpected BOMs - don't repeat
if ch != bom { if ch != bom {
s.errorf(s.file.Offset(pos), "illegal character %#U", ch) // Report an informative error for U+201[CD] quotation
// marks, which are easily introduced via copy and paste.
if ch == '“' || ch == '”' {
s.errorf(s.file.Offset(pos), "curly quotation mark %q (use neutral %q)", ch, '"')
} else {
s.errorf(s.file.Offset(pos), "illegal character %#U", ch)
}
} }
insertSemi = s.insertSemi // preserve insertSemi info insertSemi = s.insertSemi // preserve insertSemi info
tok = token.ILLEGAL tok = token.ILLEGAL

View File

@ -813,6 +813,7 @@ var errors = []struct {
{`"` + "abc\ufeffdef" + `"`, token.STRING, 4, `"` + "abc\ufeffdef" + `"`, "illegal byte order mark"}, // only first BOM is ignored {`"` + "abc\ufeffdef" + `"`, token.STRING, 4, `"` + "abc\ufeffdef" + `"`, "illegal byte order mark"}, // only first BOM is ignored
{"abc\x00def", token.IDENT, 3, "abc", "illegal character NUL"}, {"abc\x00def", token.IDENT, 3, "abc", "illegal character NUL"},
{"abc\x00", token.IDENT, 3, "abc", "illegal character NUL"}, {"abc\x00", token.IDENT, 3, "abc", "illegal character NUL"},
{"“abc”", token.ILLEGAL, 0, "abc", `curly quotation mark '“' (use neutral '"')`},
} }
func TestScanErrors(t *testing.T) { func TestScanErrors(t *testing.T) {