mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-19 11:07:32 +00:00
[rustc_builtin_macros] add indices to format_foreign::printf::Substitution::Escape
This commit is contained in:
parent
8ed935e92d
commit
a661671ae3
@ -7,28 +7,29 @@ pub(crate) mod printf {
|
|||||||
pub enum Substitution<'a> {
|
pub enum Substitution<'a> {
|
||||||
/// A formatted output substitution with its internal byte offset.
|
/// A formatted output substitution with its internal byte offset.
|
||||||
Format(Format<'a>),
|
Format(Format<'a>),
|
||||||
/// A literal `%%` escape.
|
/// A literal `%%` escape, with its start and end indices.
|
||||||
Escape,
|
Escape((usize, usize)),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Substitution<'a> {
|
impl<'a> Substitution<'a> {
|
||||||
pub fn as_str(&self) -> &str {
|
pub fn as_str(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
Substitution::Format(ref fmt) => fmt.span,
|
Substitution::Format(ref fmt) => fmt.span,
|
||||||
Substitution::Escape => "%%",
|
Substitution::Escape(_) => "%%",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn position(&self) -> Option<InnerSpan> {
|
pub fn position(&self) -> Option<InnerSpan> {
|
||||||
match *self {
|
match *self {
|
||||||
Substitution::Format(ref fmt) => Some(fmt.position),
|
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) {
|
pub fn set_position(&mut self, start: usize, end: usize) {
|
||||||
if let Substitution::Format(ref mut fmt) = self {
|
match self {
|
||||||
fmt.position = InnerSpan::new(start, end);
|
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<String, Option<String>> {
|
pub fn translate(&self) -> Result<String, Option<String>> {
|
||||||
match *self {
|
match *self {
|
||||||
Substitution::Format(ref fmt) => fmt.translate(),
|
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<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let (mut sub, tail) = parse_next_substitution(self.s)?;
|
let (mut sub, tail) = parse_next_substitution(self.s)?;
|
||||||
self.s = tail;
|
self.s = tail;
|
||||||
match sub {
|
if let Some(InnerSpan { start, end }) = sub.position() {
|
||||||
Substitution::Format(_) => {
|
sub.set_position(start + self.pos, end + self.pos);
|
||||||
if let Some(inner_span) = sub.position() {
|
self.pos += end;
|
||||||
sub.set_position(inner_span.start + self.pos, inner_span.end + self.pos);
|
|
||||||
self.pos += inner_span.end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Substitution::Escape => self.pos += 2,
|
|
||||||
}
|
}
|
||||||
Some(sub)
|
Some(sub)
|
||||||
}
|
}
|
||||||
@ -340,7 +336,7 @@ pub(crate) mod printf {
|
|||||||
let at = {
|
let at = {
|
||||||
let start = s.find('%')?;
|
let start = s.find('%')?;
|
||||||
if let '%' = s[start + 1..].chars().next()? {
|
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)
|
Cur::new_at(s, start)
|
||||||
|
3
src/test/ui/macros/issue-92267.rs
Normal file
3
src/test/ui/macros/issue-92267.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// check-fail
|
||||||
|
|
||||||
|
pub fn main() { println!("🦀%%%", 0) } //~ ERROR argument never used
|
16
src/test/ui/macros/issue-92267.stderr
Normal file
16
src/test/ui/macros/issue-92267.stderr
Normal file
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user