From ed40d461590ab0101ccec7610b8d8faecc94f7ee Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 22 Oct 2022 02:37:15 +0100 Subject: [PATCH 1/2] Properly escape quotes when suggesting switching between char/string literals --- .../src/infer/error_reporting/mod.rs | 23 +++++++++++++++++-- .../src/lexer/unescape_error_reporting.rs | 19 +++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index ddeeaa9618e60..88ffbe0b539ac 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2272,6 +2272,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { struct_span_err!(self.tcx.sess, span, E0580, "{}", failure_str) } FailureCode::Error0308(failure_str) => { + fn escape_literal(s: &str) -> String { + let mut escaped = String::with_capacity(s.len()); + let mut chrs = s.chars().peekable(); + while let Some(first) = chrs.next() { + match (first, chrs.peek()) { + ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => { + escaped.push('\\'); + escaped.push(delim); + chrs.next(); + } + ('"' | '\'', _) => { + escaped.push('\\'); + escaped.push(first) + } + (c, _) => escaped.push(c), + }; + } + escaped + } let mut err = struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str); if let Some((expected, found)) = trace.values.ty() { match (expected.kind(), found.kind()) { @@ -2293,7 +2312,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { err.span_suggestion( span, "if you meant to write a `char` literal, use single quotes", - format!("'{}'", code), + format!("'{}'", escape_literal(code)), Applicability::MachineApplicable, ); } @@ -2308,7 +2327,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { err.span_suggestion( span, "if you meant to write a `str` literal, use double quotes", - format!("\"{}\"", code), + format!("\"{}\"", escape_literal(code)), Applicability::MachineApplicable, ); } diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 77c4fadab45ea..f075de7142676 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -113,11 +113,26 @@ pub(crate) fn emit_unescape_error( } else { ("", "if you meant to write a `str` literal, use double quotes") }; - + 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), + }; + } handler.span_suggestion( span_with_quotes, msg, - format!("{}\"{}\"", prefix, lit), + format!("{prefix}\"{escaped}\""), Applicability::MachineApplicable, ); } From 876248506c9dcb0f4f57db49021374bb0bfc1bd7 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 22 Oct 2022 02:37:20 +0100 Subject: [PATCH 2/2] Update UI tests --- .../ui/inference/char-as-str-single.fixed | 1 + src/test/ui/inference/char-as-str-single.rs | 1 + .../ui/inference/char-as-str-single.stderr | 15 +++++++++++- src/test/ui/inference/str-as-char.fixed | 4 +++- src/test/ui/inference/str-as-char.rs | 4 +++- src/test/ui/inference/str-as-char.stderr | 24 ++++++++++++++++++- 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/test/ui/inference/char-as-str-single.fixed b/src/test/ui/inference/char-as-str-single.fixed index e401492a830b4..bab1854dc5173 100644 --- a/src/test/ui/inference/char-as-str-single.fixed +++ b/src/test/ui/inference/char-as-str-single.fixed @@ -8,4 +8,5 @@ fn main() { let _: char = 'a'; //~ ERROR mismatched types let _: char = '人'; //~ ERROR mismatched types + let _: char = '\''; //~ ERROR mismatched types } diff --git a/src/test/ui/inference/char-as-str-single.rs b/src/test/ui/inference/char-as-str-single.rs index 4f23cea5354c3..736920643b2c4 100644 --- a/src/test/ui/inference/char-as-str-single.rs +++ b/src/test/ui/inference/char-as-str-single.rs @@ -8,4 +8,5 @@ fn main() { let _: char = "a"; //~ ERROR mismatched types let _: char = "人"; //~ ERROR mismatched types + let _: char = "'"; //~ ERROR mismatched types } diff --git a/src/test/ui/inference/char-as-str-single.stderr b/src/test/ui/inference/char-as-str-single.stderr index 29075c15414b2..3375ec6ac32ca 100644 --- a/src/test/ui/inference/char-as-str-single.stderr +++ b/src/test/ui/inference/char-as-str-single.stderr @@ -24,6 +24,19 @@ help: if you meant to write a `char` literal, use single quotes LL | let _: char = '人'; | ~~~~ -error: aborting due to 2 previous errors +error[E0308]: mismatched types + --> $DIR/char-as-str-single.rs:11:19 + | +LL | let _: char = "'"; + | ---- ^^^ expected `char`, found `&str` + | | + | expected due to this + | +help: if you meant to write a `char` literal, use single quotes + | +LL | let _: char = '\''; + | ~~~~ + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/inference/str-as-char.fixed b/src/test/ui/inference/str-as-char.fixed index 09f3dec5a1755..6aea809cbdb29 100644 --- a/src/test/ui/inference/str-as-char.fixed +++ b/src/test/ui/inference/str-as-char.fixed @@ -4,5 +4,7 @@ // run-rustfix fn main() { - let _: &str = "a"; //~ ERROR mismatched types + 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 } diff --git a/src/test/ui/inference/str-as-char.rs b/src/test/ui/inference/str-as-char.rs index 7092a61244255..eaa8d788c3467 100644 --- a/src/test/ui/inference/str-as-char.rs +++ b/src/test/ui/inference/str-as-char.rs @@ -4,5 +4,7 @@ // run-rustfix fn main() { - let _: &str = 'a'; //~ ERROR mismatched types + 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 } diff --git a/src/test/ui/inference/str-as-char.stderr b/src/test/ui/inference/str-as-char.stderr index ebbe7c80f7719..2c84dac8e0c15 100644 --- a/src/test/ui/inference/str-as-char.stderr +++ b/src/test/ui/inference/str-as-char.stderr @@ -1,3 +1,25 @@ +error: character literal may only contain one codepoint + --> $DIR/str-as-char.rs:8:19 + | +LL | let _: &str = '"""'; + | ^^^^^ + | +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:9: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 | @@ -11,6 +33,6 @@ help: if you meant to write a `str` literal, use double quotes LL | let _: &str = "a"; | ~~~ -error: aborting due to previous error +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0308`.