Skip to content

Commit a661671

Browse files
committed
[rustc_builtin_macros] add indices to format_foreign::printf::Substitution::Escape
1 parent 8ed935e commit a661671

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

compiler/rustc_builtin_macros/src/format_foreign.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@ pub(crate) mod printf {
77
pub enum Substitution<'a> {
88
/// A formatted output substitution with its internal byte offset.
99
Format(Format<'a>),
10-
/// A literal `%%` escape.
11-
Escape,
10+
/// A literal `%%` escape, with its start and end indices.
11+
Escape((usize, usize)),
1212
}
1313

1414
impl<'a> Substitution<'a> {
1515
pub fn as_str(&self) -> &str {
1616
match *self {
1717
Substitution::Format(ref fmt) => fmt.span,
18-
Substitution::Escape => "%%",
18+
Substitution::Escape(_) => "%%",
1919
}
2020
}
2121

2222
pub fn position(&self) -> Option<InnerSpan> {
2323
match *self {
2424
Substitution::Format(ref fmt) => Some(fmt.position),
25-
_ => None,
25+
Substitution::Escape((start, end)) => Some(InnerSpan::new(start, end)),
2626
}
2727
}
2828

2929
pub fn set_position(&mut self, start: usize, end: usize) {
30-
if let Substitution::Format(ref mut fmt) = self {
31-
fmt.position = InnerSpan::new(start, end);
30+
match self {
31+
Substitution::Format(ref mut fmt) => fmt.position = InnerSpan::new(start, end),
32+
Substitution::Escape(ref mut pos) => *pos = (start, end),
3233
}
3334
}
3435

@@ -39,7 +40,7 @@ pub(crate) mod printf {
3940
pub fn translate(&self) -> Result<String, Option<String>> {
4041
match *self {
4142
Substitution::Format(ref fmt) => fmt.translate(),
42-
Substitution::Escape => Err(None),
43+
Substitution::Escape(_) => Err(None),
4344
}
4445
}
4546
}
@@ -304,14 +305,9 @@ pub(crate) mod printf {
304305
fn next(&mut self) -> Option<Self::Item> {
305306
let (mut sub, tail) = parse_next_substitution(self.s)?;
306307
self.s = tail;
307-
match sub {
308-
Substitution::Format(_) => {
309-
if let Some(inner_span) = sub.position() {
310-
sub.set_position(inner_span.start + self.pos, inner_span.end + self.pos);
311-
self.pos += inner_span.end;
312-
}
313-
}
314-
Substitution::Escape => self.pos += 2,
308+
if let Some(InnerSpan { start, end }) = sub.position() {
309+
sub.set_position(start + self.pos, end + self.pos);
310+
self.pos += end;
315311
}
316312
Some(sub)
317313
}
@@ -340,7 +336,7 @@ pub(crate) mod printf {
340336
let at = {
341337
let start = s.find('%')?;
342338
if let '%' = s[start + 1..].chars().next()? {
343-
return Some((Substitution::Escape, &s[start + 2..]));
339+
return Some((Substitution::Escape((start, start + 2)), &s[start + 2..]));
344340
}
345341

346342
Cur::new_at(s, start)

src/test/ui/macros/issue-92267.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// check-fail
2+
3+
pub fn main() { println!("🦀%%%", 0) } //~ ERROR argument never used

src/test/ui/macros/issue-92267.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: argument never used
2+
--> $DIR/issue-92267.rs:3:34
3+
|
4+
LL | pub fn main() { println!("🦀%%%", 0) }
5+
| ^ argument never used
6+
|
7+
note: format specifiers use curly braces, and the conversion specifier `
8+
` is unknown or unsupported
9+
--> $DIR/issue-92267.rs:3:30
10+
|
11+
LL | pub fn main() { println!("🦀%%%", 0) }
12+
| ^^
13+
= note: printf formatting not supported; see the documentation for `std::fmt`
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)