From a661671ae37eece708d894eb25d58ec7dd2fd9a7 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Fri, 31 Dec 2021 12:26:10 -0500 Subject: [PATCH 1/2] [rustc_builtin_macros] add indices to format_foreign::printf::Substitution::Escape --- .../src/format_foreign.rs | 28 ++++++++----------- src/test/ui/macros/issue-92267.rs | 3 ++ src/test/ui/macros/issue-92267.stderr | 16 +++++++++++ 3 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/macros/issue-92267.rs create mode 100644 src/test/ui/macros/issue-92267.stderr diff --git a/compiler/rustc_builtin_macros/src/format_foreign.rs b/compiler/rustc_builtin_macros/src/format_foreign.rs index bfddd7073ff..ecd16736e7c 100644 --- a/compiler/rustc_builtin_macros/src/format_foreign.rs +++ b/compiler/rustc_builtin_macros/src/format_foreign.rs @@ -7,28 +7,29 @@ pub(crate) mod printf { pub enum Substitution<'a> { /// A formatted output substitution with its internal byte offset. Format(Format<'a>), - /// A literal `%%` escape. - Escape, + /// A literal `%%` escape, with its start and end indices. + Escape((usize, usize)), } impl<'a> Substitution<'a> { pub fn as_str(&self) -> &str { match *self { Substitution::Format(ref fmt) => fmt.span, - Substitution::Escape => "%%", + Substitution::Escape(_) => "%%", } } pub fn position(&self) -> Option { match *self { Substitution::Format(ref fmt) => Some(fmt.position), - _ => None, + Substitution::Escape((start, end)) => Some(InnerSpan::new(start, end)), } } pub fn set_position(&mut self, start: usize, end: usize) { - if let Substitution::Format(ref mut fmt) = self { - fmt.position = InnerSpan::new(start, end); + match self { + Substitution::Format(ref mut fmt) => fmt.position = InnerSpan::new(start, end), + Substitution::Escape(ref mut pos) => *pos = (start, end), } } @@ -39,7 +40,7 @@ pub(crate) mod printf { pub fn translate(&self) -> Result> { match *self { Substitution::Format(ref fmt) => fmt.translate(), - Substitution::Escape => Err(None), + Substitution::Escape(_) => Err(None), } } } @@ -304,14 +305,9 @@ pub(crate) mod printf { fn next(&mut self) -> Option { let (mut sub, tail) = parse_next_substitution(self.s)?; self.s = tail; - match sub { - Substitution::Format(_) => { - if let Some(inner_span) = sub.position() { - sub.set_position(inner_span.start + self.pos, inner_span.end + self.pos); - self.pos += inner_span.end; - } - } - Substitution::Escape => self.pos += 2, + if let Some(InnerSpan { start, end }) = sub.position() { + sub.set_position(start + self.pos, end + self.pos); + self.pos += end; } Some(sub) } @@ -340,7 +336,7 @@ pub(crate) mod printf { let at = { let start = s.find('%')?; if let '%' = s[start + 1..].chars().next()? { - return Some((Substitution::Escape, &s[start + 2..])); + return Some((Substitution::Escape((start, start + 2)), &s[start + 2..])); } Cur::new_at(s, start) diff --git a/src/test/ui/macros/issue-92267.rs b/src/test/ui/macros/issue-92267.rs new file mode 100644 index 00000000000..f1daaeb743e --- /dev/null +++ b/src/test/ui/macros/issue-92267.rs @@ -0,0 +1,3 @@ +// check-fail + +pub fn main() { println!("🦀%%%", 0) } //~ ERROR argument never used diff --git a/src/test/ui/macros/issue-92267.stderr b/src/test/ui/macros/issue-92267.stderr new file mode 100644 index 00000000000..d2d66c81198 --- /dev/null +++ b/src/test/ui/macros/issue-92267.stderr @@ -0,0 +1,16 @@ +error: argument never used + --> $DIR/issue-92267.rs:3:34 + | +LL | pub fn main() { println!("🦀%%%", 0) } + | ^ argument never used + | +note: format specifiers use curly braces, and the conversion specifier ` + ` is unknown or unsupported + --> $DIR/issue-92267.rs:3:30 + | +LL | pub fn main() { println!("🦀%%%", 0) } + | ^^ + = note: printf formatting not supported; see the documentation for `std::fmt` + +error: aborting due to previous error + From 4a7f276cb67673ad2550ee7ef91caa0706565679 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Fri, 31 Dec 2021 12:51:27 -0500 Subject: [PATCH 2/2] update tests --- .../rustc_builtin_macros/src/format_foreign/printf/tests.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/format_foreign/printf/tests.rs b/compiler/rustc_builtin_macros/src/format_foreign/printf/tests.rs index 1336aab7316..fc7442470ac 100644 --- a/compiler/rustc_builtin_macros/src/format_foreign/printf/tests.rs +++ b/compiler/rustc_builtin_macros/src/format_foreign/printf/tests.rs @@ -13,9 +13,9 @@ macro_rules! assert_eq_pnsat { fn test_escape() { assert_eq!(pns("has no escapes"), None); assert_eq!(pns("has no escapes, either %"), None); - assert_eq!(pns("*so* has a %% escape"), Some((S::Escape, " escape"))); - assert_eq!(pns("%% leading escape"), Some((S::Escape, " leading escape"))); - assert_eq!(pns("trailing escape %%"), Some((S::Escape, ""))); + assert_eq!(pns("*so* has a %% escape"), Some((S::Escape((11, 13)), " escape"))); + assert_eq!(pns("%% leading escape"), Some((S::Escape((0, 2)), " leading escape"))); + assert_eq!(pns("trailing escape %%"), Some((S::Escape((16, 18)), ""))); } #[test]