Skip to content

Commit

Permalink
Remove incorrect detection of simple fmt cases
Browse files Browse the repository at this point in the history
This was intended to detect the simple cases "{var}" and "{var:?}" and emit them
as straightforward Display::fmt and Debug::fmt method calls rather than a write
macro, but this was only to keep the generated code simple and is not important.
It was broken by the changes to how we parse the fmt attr in 1.0.7.

Fixes #53.
  • Loading branch information
dtolnay committed Dec 4, 2019
1 parent d53be52 commit 20202db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
20 changes: 3 additions & 17 deletions impl/src/attr.rs
Expand Up @@ -20,7 +20,6 @@ pub struct Display<'a> {
pub original: &'a Attribute,
pub fmt: LitStr,
pub args: TokenStream,
pub was_shorthand: bool,
pub has_bonus_display: bool,
}

Expand Down Expand Up @@ -82,7 +81,6 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu
original: attr,
fmt: input.parse()?,
args: parse_token_expr(input, false)?,
was_shorthand: false,
has_bonus_display: false,
};
if attrs.display.is_some() {
Expand Down Expand Up @@ -153,20 +151,8 @@ impl ToTokens for Display<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let fmt = &self.fmt;
let args = &self.args;
if self.was_shorthand && fmt.value() == "{}" {
let arg = args.clone().into_iter().nth(1).unwrap();
tokens.extend(quote! {
std::fmt::Display::fmt(#arg, __formatter)
});
} else if self.was_shorthand && fmt.value() == "{:?}" {
let arg = args.clone().into_iter().nth(1).unwrap();
tokens.extend(quote! {
std::fmt::Debug::fmt(#arg, __formatter)
});
} else {
tokens.extend(quote! {
write!(__formatter, #fmt #args)
});
}
tokens.extend(quote! {
write!(__formatter, #fmt #args)
});
}
}
1 change: 0 additions & 1 deletion impl/src/fmt.rs
Expand Up @@ -88,7 +88,6 @@ impl Display<'_> {
out += read;
self.fmt = LitStr::new(&out, self.fmt.span());
self.args = args;
self.was_shorthand = true;
self.has_bonus_display = has_bonus_display;
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_display.rs
Expand Up @@ -165,3 +165,17 @@ fn test_trailing_comma() {

assert("error ?", Error('?'));
}

#[test]
fn test_field() {
#[derive(Debug)]
struct Inner {
data: usize,
}

#[derive(Error, Debug)]
#[error("{}", .0.data)]
struct Error(Inner);

assert("0", Error(Inner { data: 0 }));
}

0 comments on commit 20202db

Please sign in to comment.