Skip to content

Fix suggestion for attempting to define a string with single quotes #114913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,14 @@ pub(crate) fn emit_unescape_error(
let sugg = sugg.unwrap_or_else(|| {
let prefix = mode.prefix_noraw();
let mut escaped = String::with_capacity(lit.len());
let mut chrs = lit.chars().peekable();
while let Some(first) = chrs.next() {
match (first, chrs.peek()) {
('\\', Some('"')) => {
escaped.push('\\');
escaped.push('"');
chrs.next();
}
('"', _) => {
escaped.push('\\');
escaped.push('"')
}
(c, _) => escaped.push(c),
};
let mut in_escape = false;
for c in lit.chars() {
match c {
'\\' => in_escape = !in_escape,
'"' if !in_escape => escaped.push('\\'),
_ => in_escape = false,
}
escaped.push(c);
}
let sugg = format!("{prefix}\"{escaped}\"");
MoreThanOneCharSugg::Quotes {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/inference/str-as-char.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ fn main() {
let _: &str = "a"; //~ ERROR mismatched types
let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint
let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint
let _: &str = "\"\"\\\"\\\"\\\\\""; //~ ERROR character literal may only contain one codepoint
}
1 change: 1 addition & 0 deletions tests/ui/inference/str-as-char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ fn main() {
let _: &str = 'a'; //~ ERROR mismatched types
let _: &str = '"""'; //~ ERROR character literal may only contain one codepoint
let _: &str = '\"\"\"'; //~ ERROR character literal may only contain one codepoint
let _: &str = '"\"\\"\\\"\\\\"'; //~ ERROR character literal may only contain one codepoint
}
13 changes: 12 additions & 1 deletion tests/ui/inference/str-as-char.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ help: if you meant to write a `str` literal, use double quotes
LL | let _: &str = "\"\"\"";
| ~~~~~~~~

error: character literal may only contain one codepoint
--> $DIR/str-as-char.rs:10:19
|
LL | let _: &str = '"\"\"\\"\\"';
| ^^^^^^^^^^^^^^^^^
|
help: if you meant to write a `str` literal, use double quotes
|
LL | let _: &str = "\"\"\\"\\"\\\"";
| ~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/str-as-char.rs:7:19
|
Expand All @@ -33,6 +44,6 @@ help: if you meant to write a `str` literal, use double quotes
LL | let _: &str = "a";
| ~~~

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.