Skip to content

Commit 95ea4a1

Browse files
authored
Rollup merge of #64783 - onehr:onehrxn, r=varkor
Fix issue #64732 Based on issue #64732, when creating a byte literal with single quotes, the suggestion message would indicate that you meant to write a `str` literal, but we actually meant to write a byte string literal. So I changed the unescape_error_reporting.rs to decide whether to print out "if you meant to write a `str` literal, use double quotes", or "if you meant to write a byte string literal, use double quotes". Fixes #64732.
2 parents 0130393 + a048447 commit 95ea4a1

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/libsyntax/parse/unescape_error_reporting.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ pub(crate) fn emit_unescape_error(
4747
.emit();
4848
}
4949
EscapeError::MoreThanOneChar => {
50+
let msg = if mode.is_bytes() {
51+
"if you meant to write a byte string literal, use double quotes"
52+
} else {
53+
"if you meant to write a `str` literal, use double quotes"
54+
};
55+
5056
handler
5157
.struct_span_err(
5258
span_with_quotes,
5359
"character literal may only contain one codepoint",
5460
)
5561
.span_suggestion(
5662
span_with_quotes,
57-
"if you meant to write a `str` literal, use double quotes",
63+
msg,
5864
format!("\"{}\"", lit),
5965
Applicability::MachineApplicable,
6066
).emit()

src/test/ui/issues/issue-64732.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(unused)]
2+
fn main() {
3+
let _foo = b'hello\0';
4+
//~^ ERROR character literal may only contain one codepoint
5+
//~| HELP if you meant to write a byte string literal, use double quotes
6+
let _bar = 'hello';
7+
//~^ ERROR character literal may only contain one codepoint
8+
//~| HELP if you meant to write a `str` literal, use double quotes
9+
}

src/test/ui/issues/issue-64732.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: character literal may only contain one codepoint
2+
--> $DIR/issue-64732.rs:3:17
3+
|
4+
LL | let _foo = b'hello\0';
5+
| ^^^^^^^^^
6+
help: if you meant to write a byte string literal, use double quotes
7+
|
8+
LL | let _foo = b"hello\0";
9+
| ^^^^^^^^^
10+
11+
error: character literal may only contain one codepoint
12+
--> $DIR/issue-64732.rs:6:16
13+
|
14+
LL | let _bar = 'hello';
15+
| ^^^^^^^
16+
help: if you meant to write a `str` literal, use double quotes
17+
|
18+
LL | let _bar = "hello";
19+
| ^^^^^^^
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)