Skip to content

Commit da7dd4f

Browse files
committed
Print full char literal on error if any are non-printing
1 parent 7594067 commit da7dd4f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ pub(crate) fn emit_unescape_error(
8282
Applicability::MachineApplicable,
8383
);
8484
}
85+
} else {
86+
if lit.chars().filter(|x| x.is_whitespace() || x.is_control()).count() >= 1 {
87+
handler.span_note(
88+
span,
89+
&format!(
90+
"there are non-printing characters, the full sequence is `{}`",
91+
lit.escape_default(),
92+
),
93+
);
94+
}
8595
}
8696

8797
if !has_help {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This tests that the error generated when a character literal has multiple
2+
// characters in it contains a note about non-printing characters.
3+
4+
fn main() {
5+
// <hair space>x<zero width space>
6+
let _hair_space_around = ' x​';
7+
//~^ ERROR: character literal may only contain one codepoint
8+
//~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: character literal may only contain one codepoint
2+
--> $DIR/whitespace-character-literal.rs:6:30
3+
|
4+
LL | let _hair_space_around = ' x​';
5+
| ^^^^
6+
|
7+
note: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
8+
--> $DIR/whitespace-character-literal.rs:6:31
9+
|
10+
LL | let _hair_space_around = ' x​';
11+
| ^^
12+
help: if you meant to write a `str` literal, use double quotes
13+
|
14+
LL | let _hair_space_around = " x​";
15+
| ~~~~
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)