From 7f4a744d51a40fc0ecb970f73c3b79cbe9543619 Mon Sep 17 00:00:00 2001 From: Nicole Mazzuca Date: Sun, 26 Apr 2020 15:54:42 -0700 Subject: [PATCH 1/4] Try setting as error for a crater run --- src/libfmt_macros/lib.rs | 83 +++++++++++----------------------------- 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index e138503b508d5..785d3bb9c1e6a 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -197,7 +197,7 @@ impl<'a> Iterator for Parser<'a> { Some(String(self.string(pos + 1))) } else { let arg = self.argument(); - if let Some(end) = self.must_consume('}') { + if let Some(end) = self.consume_close_brace() { let start = self.to_span_index(pos); let end = self.to_span_index(end + 1); self.arg_places.push(start.to(end)); @@ -250,24 +250,6 @@ impl<'a> Parser<'a> { } } - /// Notifies of an error. The message doesn't actually need to be of type - /// String, but I think it does when this eventually uses conditions so it - /// might as well start using it now. - fn err, S2: Into>( - &mut self, - description: S1, - label: S2, - span: InnerSpan, - ) { - self.errors.push(ParseError { - description: description.into(), - note: None, - label: label.into(), - span, - secondary_label: None, - }); - } - /// Notifies of an error. The message doesn't actually need to be of type /// String, but I think it does when this eventually uses conditions so it /// might as well start using it now. @@ -329,30 +311,22 @@ impl<'a> Parser<'a> { InnerOffset(raw + pos + 1) } - /// Forces consumption of the specified character. If the character is not - /// found, an error is emitted. - fn must_consume(&mut self, c: char) -> Option { - self.ws(); - + /// Consume the final `}` in a format specifier. If it is not found, an error is emitted. + fn consume_close_brace(&mut self) -> Option { if let Some(&(pos, maybe)) = self.cur.peek() { - if c == maybe { + if maybe == '}' { self.cur.next(); Some(pos) } else { let pos = self.to_span_index(pos); let description = format!("expected `'}}'`, found `{:?}`", maybe); let label = "expected `}`".to_owned(); - let (note, secondary_label) = if c == '}' { - ( - Some( - "if you intended to print `{`, you can escape it using `{{`".to_owned(), - ), - self.last_opening_brace - .map(|sp| ("because of this opening brace".to_owned(), sp)), - ) - } else { - (None, None) - }; + let note = Some( + "if you intended to print `{`, you can escape it using `{{`".to_owned(), + ); + let secondary_label = + self.last_opening_brace + .map(|sp| ("because of this opening brace".to_owned(), sp)); self.errors.push(ParseError { description, note, @@ -363,33 +337,22 @@ impl<'a> Parser<'a> { None } } else { - let description = format!("expected `{:?}` but string was terminated", c); + let description = "expected `}` but string was terminated".to_owned(); // point at closing `"` let pos = self.input.len() - if self.append_newline { 1 } else { 0 }; let pos = self.to_span_index(pos); - if c == '}' { - let label = format!("expected `{:?}`", c); - let (note, secondary_label) = if c == '}' { - ( - Some( - "if you intended to print `{`, you can escape it using `{{`".to_owned(), - ), - self.last_opening_brace - .map(|sp| ("because of this opening brace".to_owned(), sp)), - ) - } else { - (None, None) - }; - self.errors.push(ParseError { - description, - note, - label, - span: pos.to(pos), - secondary_label, - }); - } else { - self.err(description, format!("expected `{:?}`", c), pos.to(pos)); - } + let label = "expected `}`".to_owned(); + let note = + Some("if you intended to print `{`, you can escape it using `{{`".to_owned()); + let secondary_label = + self.last_opening_brace.map(|sp| ("because of this opening brace".to_owned(), sp)); + self.errors.push(ParseError { + description, + note, + label, + span: pos.to(pos), + secondary_label, + }); None } } From 0b1dad75a314d6c6b678871e3f56beeaa1801e2b Mon Sep 17 00:00:00 2001 From: Nicole Mazzuca Date: Mon, 27 Apr 2020 13:18:39 -0700 Subject: [PATCH 2/4] remove ws() --- src/libfmt_macros/lib.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index 785d3bb9c1e6a..de483ea1eb677 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -357,17 +357,6 @@ impl<'a> Parser<'a> { } } - /// Consumes all whitespace characters until the first non-whitespace character - fn ws(&mut self) { - while let Some(&(_, c)) = self.cur.peek() { - if c.is_whitespace() { - self.cur.next(); - } else { - break; - } - } - } - /// Parses all of a string which is to be considered a "raw literal" in a /// format string. This is everything outside of the braces. fn string(&mut self, start: usize) -> &'a str { From 9a7b12eed903c572dfc7a9200f3c97e4ed01bb14 Mon Sep 17 00:00:00 2001 From: Nicole Mazzuca Date: Mon, 27 Apr 2020 14:33:29 -0700 Subject: [PATCH 3/4] format --- src/libfmt_macros/lib.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index de483ea1eb677..14c289c2926e4 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -321,12 +321,11 @@ impl<'a> Parser<'a> { let pos = self.to_span_index(pos); let description = format!("expected `'}}'`, found `{:?}`", maybe); let label = "expected `}`".to_owned(); - let note = Some( - "if you intended to print `{`, you can escape it using `{{`".to_owned(), - ); - let secondary_label = - self.last_opening_brace - .map(|sp| ("because of this opening brace".to_owned(), sp)); + let note = + Some("if you intended to print `{`, you can escape it using `{{`".to_owned()); + let secondary_label = self + .last_opening_brace + .map(|sp| ("because of this opening brace".to_owned(), sp)); self.errors.push(ParseError { description, note, From db7c3d949fe5f60162ba9e515010c979890e0eb8 Mon Sep 17 00:00:00 2001 From: Nicole Mazzuca Date: Tue, 28 Apr 2020 00:08:38 -0700 Subject: [PATCH 4/4] fix stderr output? --- src/libfmt_macros/lib.rs | 2 +- src/test/ui/issues/issue-51848.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index 14c289c2926e4..d8b47b0aa2008 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -319,7 +319,7 @@ impl<'a> Parser<'a> { Some(pos) } else { let pos = self.to_span_index(pos); - let description = format!("expected `'}}'`, found `{:?}`", maybe); + let description = format!("expected `}}`, found `{:?}`", maybe); let label = "expected `}`".to_owned(); let note = Some("if you intended to print `{`, you can escape it using `{{`".to_owned()); diff --git a/src/test/ui/issues/issue-51848.stderr b/src/test/ui/issues/issue-51848.stderr index 051c4d7f42793..316b7b7e47768 100644 --- a/src/test/ui/issues/issue-51848.stderr +++ b/src/test/ui/issues/issue-51848.stderr @@ -1,8 +1,8 @@ -error: invalid format string: expected `'}'` but string was terminated +error: invalid format string: expected `}` but string was terminated --> $DIR/issue-51848.rs:6:20 | LL | println!("{"); - | -^ expected `'}'` in format string + | -^ expected `}` in format string | | | because of this opening brace ...