about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-09-26 17:55:15 +0200
committerGitHub <noreply@github.com>2019-09-26 17:55:15 +0200
commit95ea4a1a8a0c8786fdb0d6c63669ece927820ad3 (patch)
treed65d373ce0b6b6f59811d74261374c0e494df878
parent01303936f37ef494bfe11012645ff7ca37606fdf (diff)
parenta048447ff42c0e7c793aa33c92aa8daf61576b71 (diff)
downloadrust-95ea4a1a8a0c8786fdb0d6c63669ece927820ad3.tar.gz
rust-95ea4a1a8a0c8786fdb0d6c63669ece927820ad3.zip
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.
-rw-r--r--src/libsyntax/parse/unescape_error_reporting.rs8
-rw-r--r--src/test/ui/issues/issue-64732.rs9
-rw-r--r--src/test/ui/issues/issue-64732.stderr22
3 files changed, 38 insertions, 1 deletions
diff --git a/src/libsyntax/parse/unescape_error_reporting.rs b/src/libsyntax/parse/unescape_error_reporting.rs
index 7eee07e61a9..5565015179c 100644
--- a/src/libsyntax/parse/unescape_error_reporting.rs
+++ b/src/libsyntax/parse/unescape_error_reporting.rs
@@ -47,6 +47,12 @@ pub(crate) fn emit_unescape_error(
                 .emit();
         }
         EscapeError::MoreThanOneChar => {
+            let msg = if mode.is_bytes() {
+                "if you meant to write a byte string literal, use double quotes"
+            } else {
+                "if you meant to write a `str` literal, use double quotes"
+            };
+
             handler
                 .struct_span_err(
                     span_with_quotes,
@@ -54,7 +60,7 @@ pub(crate) fn emit_unescape_error(
                 )
                 .span_suggestion(
                     span_with_quotes,
-                    "if you meant to write a `str` literal, use double quotes",
+                    msg,
                     format!("\"{}\"", lit),
                     Applicability::MachineApplicable,
                 ).emit()
diff --git a/src/test/ui/issues/issue-64732.rs b/src/test/ui/issues/issue-64732.rs
new file mode 100644
index 00000000000..2db51ea6042
--- /dev/null
+++ b/src/test/ui/issues/issue-64732.rs
@@ -0,0 +1,9 @@
+#![allow(unused)]
+fn main() {
+    let _foo = b'hello\0';
+    //~^ ERROR character literal may only contain one codepoint
+    //~| HELP if you meant to write a byte string literal, use double quotes
+    let _bar = 'hello';
+    //~^ ERROR character literal may only contain one codepoint
+    //~| HELP if you meant to write a `str` literal, use double quotes
+}
diff --git a/src/test/ui/issues/issue-64732.stderr b/src/test/ui/issues/issue-64732.stderr
new file mode 100644
index 00000000000..fc0e8e3bdb2
--- /dev/null
+++ b/src/test/ui/issues/issue-64732.stderr
@@ -0,0 +1,22 @@
+error: character literal may only contain one codepoint
+  --> $DIR/issue-64732.rs:3:17
+   |
+LL |     let _foo = b'hello\0';
+   |                 ^^^^^^^^^
+help: if you meant to write a byte string literal, use double quotes
+   |
+LL |     let _foo = b"hello\0";
+   |                 ^^^^^^^^^
+
+error: character literal may only contain one codepoint
+  --> $DIR/issue-64732.rs:6:16
+   |
+LL |     let _bar = 'hello';
+   |                ^^^^^^^
+help: if you meant to write a `str` literal, use double quotes
+   |
+LL |     let _bar = "hello";
+   |                ^^^^^^^
+
+error: aborting due to 2 previous errors
+