Skip to content

Commit

Permalink
Rollup merge of #92460 - dwrensha:fix-92267, r=petrochenkov
Browse files Browse the repository at this point in the history
[rustc_builtin_macros] add indices to format_foreign::printf::Substitution::Escape

Fixes #92267.

The problem was that the escape string "%%" does not need to appear at the very beginning of the format string, but
the iterator implementation assumed that it did.

The solution follows the pattern used by `format_foregin::shell::Subtitution::Escape`: https://github.com/rust-lang/rust/blob/8ed935e92dfb09ae388344b12284bf5110cf9265/compiler/rustc_builtin_macros/src/format_foreign.rs#L629
  • Loading branch information
matthiaskrgr authored Jan 1, 2022
2 parents a76128b + 4a7f276 commit 913bc86
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
28 changes: 12 additions & 16 deletions compiler/rustc_builtin_macros/src/format_foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InnerSpan> {
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),
}
}

Expand All @@ -39,7 +40,7 @@ pub(crate) mod printf {
pub fn translate(&self) -> Result<String, Option<String>> {
match *self {
Substitution::Format(ref fmt) => fmt.translate(),
Substitution::Escape => Err(None),
Substitution::Escape(_) => Err(None),
}
}
}
Expand Down Expand Up @@ -304,14 +305,9 @@ pub(crate) mod printf {
fn next(&mut self) -> Option<Self::Item> {
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)
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/macros/issue-92267.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// check-fail

pub fn main() { println!("🦀%%%", 0) } //~ ERROR argument never used
16 changes: 16 additions & 0 deletions src/test/ui/macros/issue-92267.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 913bc86

Please sign in to comment.