about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2018-12-26 20:05:56 -0800
committerEsteban Küber <esteban@kuber.com.ar>2018-12-26 20:36:02 -0800
commit5e75001c596991d63729b41769164197db6fc5c8 (patch)
tree2f5339c9d148bfbd43497a3a81414032de2dd74d /src
parenta1bad57fa59a8069a6ebb05cd6a2ae73c88b2e98 (diff)
downloadrust-5e75001c596991d63729b41769164197db6fc5c8.tar.gz
rust-5e75001c596991d63729b41769164197db6fc5c8.zip
Point at correct span for arguments in format strings
When a format string has escaped whitespace characters format
arguments were shifted by one per each escaped character. Account
for these escaped characters when synthesizing the spans.

Fix #55155.
Diffstat (limited to 'src')
-rw-r--r--src/libfmt_macros/lib.rs7
-rw-r--r--src/libsyntax_ext/format.rs4
-rw-r--r--src/test/ui/fmt/format-string-error-2.rs2
-rw-r--r--src/test/ui/fmt/format-string-error-2.stderr8
4 files changed, 15 insertions, 6 deletions
diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs
index fc8325e5915..32ae878909f 100644
--- a/src/libfmt_macros/lib.rs
+++ b/src/libfmt_macros/lib.rs
@@ -144,7 +144,7 @@ pub struct Parser<'a> {
     /// `Some(raw count)` when the string is "raw", used to position spans correctly
     style: Option<usize>,
     /// Start and end byte offset of every successfully parsed argument
-    pub arg_places: Vec<(usize, usize)>,
+    pub arg_places: Vec<(SpanIndex, SpanIndex)>,
     /// Characters that need to be shifted
     skips: Vec<usize>,
     /// Span offset of the last opening brace seen, used for error reporting
@@ -154,7 +154,7 @@ pub struct Parser<'a> {
 }
 
 #[derive(Clone, Copy, Debug)]
-pub struct SpanIndex(usize);
+pub struct SpanIndex(pub usize);
 
 impl SpanIndex {
     pub fn unwrap(self) -> usize {
@@ -166,7 +166,6 @@ impl<'a> Iterator for Parser<'a> {
     type Item = Piece<'a>;
 
     fn next(&mut self) -> Option<Piece<'a>> {
-        let raw = self.raw();
         if let Some(&(pos, c)) = self.cur.peek() {
             match c {
                 '{' => {
@@ -180,7 +179,7 @@ impl<'a> Iterator for Parser<'a> {
                     } else {
                         let arg = self.argument();
                         if let Some(arg_pos) = self.must_consume('}').map(|end| {
-                            (pos + raw + 1, end + raw + 2)
+                            (self.to_span_index(pos), self.to_span_index(end + 1))
                         }) {
                             self.arg_places.push(arg_pos);
                         }
diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs
index 220765fd8c7..17e692d1d32 100644
--- a/src/libsyntax_ext/format.rs
+++ b/src/libsyntax_ext/format.rs
@@ -861,7 +861,9 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
     }
 
     let arg_spans = parser.arg_places.iter()
-        .map(|&(start, end)| fmt.span.from_inner_byte_pos(start, end))
+        .map(|&(parse::SpanIndex(start), parse::SpanIndex(end))| {
+            fmt.span.from_inner_byte_pos(start, end)
+        })
         .collect();
 
     let mut cx = Context {
diff --git a/src/test/ui/fmt/format-string-error-2.rs b/src/test/ui/fmt/format-string-error-2.rs
index fd6e41ec6fc..3c6c15c06bb 100644
--- a/src/test/ui/fmt/format-string-error-2.rs
+++ b/src/test/ui/fmt/format-string-error-2.rs
@@ -67,4 +67,6 @@ raw  { \n
     asdf}
     ", asdf=1);
     //~^^ ERROR invalid format string
+    println!("\t{}");
+    //~^ ERROR 1 positional argument in format string
 }
diff --git a/src/test/ui/fmt/format-string-error-2.stderr b/src/test/ui/fmt/format-string-error-2.stderr
index 0b3f08c1fbe..face0bc0f5f 100644
--- a/src/test/ui/fmt/format-string-error-2.stderr
+++ b/src/test/ui/fmt/format-string-error-2.stderr
@@ -133,5 +133,11 @@ LL |     asdf}
    |
    = note: if you intended to print `{`, you can escape it using `{{`
 
-error: aborting due to 13 previous errors
+error: 1 positional argument in format string, but no arguments were given
+  --> $DIR/format-string-error-2.rs:70:17
+   |
+LL |     println!("/t{}");
+   |                 ^^
+
+error: aborting due to 14 previous errors