Skip to content

Commit ab958a7

Browse files
authored
Rollup merge of #90861 - 5225225:nonprinting-char, r=davidtwco
Print escaped string if char literal has multiple characters, but only one printable character Fixes #90857 I'm not sure about the error message here, it could get rather long and *maybe* using the names of characters would be better? That wouldn't help the length any, though.
2 parents 904dba5 + eee29b0 commit ab958a7

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+27
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,33 @@ pub(crate) fn emit_unescape_error(
8282
Applicability::MachineApplicable,
8383
);
8484
}
85+
} else {
86+
let printable: Vec<char> = lit
87+
.chars()
88+
.filter(|&x| {
89+
unicode_width::UnicodeWidthChar::width(x).unwrap_or(0) != 0
90+
&& !x.is_whitespace()
91+
})
92+
.collect();
93+
94+
if let [ch] = printable.as_slice() {
95+
has_help = true;
96+
97+
handler.span_note(
98+
span,
99+
&format!(
100+
"there are non-printing characters, the full sequence is `{}`",
101+
lit.escape_default(),
102+
),
103+
);
104+
105+
handler.span_suggestion(
106+
span,
107+
"consider removing the non-printing characters",
108+
ch.to_string(),
109+
Applicability::MaybeIncorrect,
110+
);
111+
}
85112
}
86113

87114
if !has_help {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
let _hair_space_around = ' x​';
6+
//~^ ERROR: character literal may only contain one codepoint
7+
//~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
8+
//~| HELP: consider removing the non-printing characters
9+
//~| SUGGESTION: x
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: character literal may only contain one codepoint
2+
--> $DIR/whitespace-character-literal.rs:5:30
3+
|
4+
LL | let _hair_space_around = ' x​';
5+
| ^--^
6+
| |
7+
| help: consider removing the non-printing characters: `x`
8+
|
9+
note: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
10+
--> $DIR/whitespace-character-literal.rs:5:31
11+
|
12+
LL | let _hair_space_around = ' x​';
13+
| ^^
14+
15+
error: aborting due to previous error
16+

src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ENTRY_LIMIT: usize = 1000;
99
// FIXME: The following limits should be reduced eventually.
1010
const ROOT_ENTRY_LIMIT: usize = 1102;
1111
const ISSUES_ENTRY_LIMIT: usize = 2310;
12-
const PARSER_LIMIT: usize = 1004;
12+
const PARSER_LIMIT: usize = 1005;
1313

1414
fn check_entries(path: &Path, bad: &mut bool) {
1515
let dirs = walkdir::WalkDir::new(&path.join("test/ui"))

0 commit comments

Comments
 (0)