about summary refs log tree commit diff
diff options
context:
space:
mode:
authorÖmer Sinan Ağacan <omeragacan@gmail.com>2021-01-16 12:46:57 +0300
committerÖmer Sinan Ağacan <omeragacan@gmail.com>2021-01-17 17:40:58 +0300
commit9111e9dd01ac08b5a7e40ad0f348946816d6d140 (patch)
tree10ba859e7aa4ad2162f58d401974a1c8aa6f6c72
parentd51cf9601c714a8383aecade7c98559dd4831625 (diff)
rustc_parse_format: Fix character indices in find_skips
Fixes #81006
-rw-r--r--compiler/rustc_parse_format/src/lib.rs2
-rw-r--r--src/test/ui/macros/issue-81006.rs10
-rw-r--r--src/test/ui/macros/issue-81006.stderr14
3 files changed, 25 insertions, 1 deletions
diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs
index f7b16bd991b..f150f7a41ae 100644
--- a/compiler/rustc_parse_format/src/lib.rs
+++ b/compiler/rustc_parse_format/src/lib.rs
@@ -736,7 +736,7 @@ fn find_skips_from_snippet(
 
     fn find_skips(snippet: &str, is_raw: bool) -> Vec<usize> {
         let mut eat_ws = false;
-        let mut s = snippet.chars().enumerate().peekable();
+        let mut s = snippet.char_indices().peekable();
         let mut skips = vec![];
         while let Some((pos, c)) = s.next() {
             match (c, s.peek()) {
diff --git a/src/test/ui/macros/issue-81006.rs b/src/test/ui/macros/issue-81006.rs
new file mode 100644
index 00000000000..602eb597428
--- /dev/null
+++ b/src/test/ui/macros/issue-81006.rs
@@ -0,0 +1,10 @@
+// check-fail
+
+// First format below would cause a panic, second would generate error with incorrect span
+
+fn main() {
+    let _ = format!("→{}→\n");
+    //~^ ERROR 1 positional argument in format string, but no arguments were given
+    let _ = format!("→{} \n");
+    //~^ ERROR 1 positional argument in format string, but no arguments were given
+}
diff --git a/src/test/ui/macros/issue-81006.stderr b/src/test/ui/macros/issue-81006.stderr
new file mode 100644
index 00000000000..14a8cbe0155
--- /dev/null
+++ b/src/test/ui/macros/issue-81006.stderr
@@ -0,0 +1,14 @@
+error: 1 positional argument in format string, but no arguments were given
+  --> $DIR/issue-81006.rs:6:23
+   |
+LL |     let _ = format!("→{}→\n");
+   |                       ^^
+
+error: 1 positional argument in format string, but no arguments were given
+  --> $DIR/issue-81006.rs:8:23
+   |
+LL |     let _ = format!("→{} \n");
+   |                       ^^
+
+error: aborting due to 2 previous errors
+