]> Cypherpunks repositories - gostls13.git/commitdiff
go/scanner: give specific error for curvy “abc” quotes
authorAlan Donovan <adonovan@google.com>
Tue, 25 Jul 2023 11:47:05 +0000 (12:47 +0100)
committerAlan Donovan <adonovan@google.com>
Mon, 28 Aug 2023 15:38:31 +0000 (15:38 +0000)
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>

src/go/scanner/scanner.go
src/go/scanner/scanner_test.go

index 75f835d3105d67848fc20d40ea7d626721bd4abd..8742c29906520ced468435e2f59f837950b4b0ff 100644 (file)
@@ -943,7 +943,13 @@ scanAgain:
                default:
                        // next reports unexpected BOMs - don't repeat
                        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
                        tok = token.ILLEGAL
index 9046148ac2c4a05127bbdeb079c9900826d34612..916a40a8744ed77513bd112b2ea044bff7fb2b3c 100644 (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\x00def", 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) {