From 9111e9dd01ac08b5a7e40ad0f348946816d6d140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Sat, 16 Jan 2021 12:46:57 +0300 Subject: [PATCH] rustc_parse_format: Fix character indices in find_skips Fixes #81006 --- compiler/rustc_parse_format/src/lib.rs | 2 +- src/test/ui/macros/issue-81006.rs | 10 ++++++++++ src/test/ui/macros/issue-81006.stderr | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/macros/issue-81006.rs create mode 100644 src/test/ui/macros/issue-81006.stderr diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index f7b16bd991bfd..f150f7a41ae80 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 { 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 0000000000000..602eb59742879 --- /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 0000000000000..14a8cbe0155f9 --- /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 +