Skip to content

Commit f3daf63

Browse files
lolbinarycatLFS6502
authored andcommitted
Hint on unknown escape of Unicode quotation marks in string literal
Fixes #128858 I opted not to produce a suggestion, since it's not obvious what the user meant to do.
1 parent f77247a commit f3daf63

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ pub(crate) fn emit_unescape_error(
158158
"this is an isolated carriage return; consider checking your editor and \
159159
version control settings",
160160
);
161+
} else if looks_like_quote(c) {
162+
diag.help(
163+
format!("{ec} is not an ascii quote, \
164+
but may look like one in some fonts.\n\
165+
consider writing it in its \
166+
escaped form for clarity."));
161167
} else {
162168
if mode == Mode::Str || mode == Mode::Char {
163169
diag.span_suggestion(
@@ -295,3 +301,23 @@ pub(crate) fn escaped_char(c: char) -> String {
295301
_ => c.escape_default().to_string(),
296302
}
297303
}
304+
305+
/// Returns true if `c` may look identical to `"` in some fonts.
306+
fn looks_like_quote(c: char) -> bool {
307+
// list of homoglyphs generated using the following wikidata query:
308+
// SELECT ?u WHERE {
309+
// wd:Q87495536 wdt:P2444+ ?c.
310+
// ?c wdt:P4213 ?u.
311+
// }
312+
match c {
313+
'\u{2033}' |
314+
'\u{02BA}' |
315+
'\u{02DD}' |
316+
'\u{030B}' |
317+
'\u{030E}' |
318+
'\u{05F4}' |
319+
'\u{201C}' |
320+
'\u{201D}' => true,
321+
_ => false,
322+
}
323+
}

tests/ui/unicode-quote.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
dbg!("since when is \“THIS\” not allowed in a string literal");
3+
}

tests/ui/unicode-quote.stderr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: unknown character escape: `\u{201c}`
2+
--> $DIR/unicode-quote.rs:2:26
3+
|
4+
LL | dbg!("since when is \“THIS\” not allowed in a string literal");
5+
| ^ unknown character escape
6+
|
7+
= help: \u{201c} is not an ascii quote, but may look like one in some fonts.
8+
consider writing it in its escaped form for clarity.
9+
10+
error: unknown character escape: `\u{201d}`
11+
--> $DIR/unicode-quote.rs:2:32
12+
|
13+
LL | dbg!("since when is \“THIS\” not allowed in a string literal");
14+
| ^ unknown character escape
15+
|
16+
= help: \u{201d} is not an ascii quote, but may look like one in some fonts.
17+
consider writing it in its escaped form for clarity.
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)