Skip to content

Commit

Permalink
Only apply inferred bounds if interpolation refers to field, not form…
Browse files Browse the repository at this point in the history
…at var
  • Loading branch information
dtolnay committed Nov 4, 2024
1 parent 67faae4 commit 5933179
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ impl Display<'_> {
}
_ => continue,
};
let formatvar = match &member {
MemberUnraw::Unnamed(index) => IdentUnraw::new(format_ident!("_{}", index)),
MemberUnraw::Named(ident) => ident.clone(),
};
out += &formatvar.to_string();
if !named_args.insert(formatvar.clone()) {
// Already specified in the format argument list.
continue;
}
if !has_trailing_comma {
args.extend(quote_spanned!(span=> ,));
}
let local = formatvar.to_local();
args.extend(quote_spanned!(span=> #formatvar = #local));
if let Some(&field) = member_index.get(&member) {
let end_spec = match read.find('}') {
Some(end_spec) => end_spec,
Expand All @@ -83,28 +97,15 @@ impl Display<'_> {
Some('b') => Trait::Binary,
Some('e') => Trait::LowerExp,
Some('E') => Trait::UpperExp,
Some(_) | None => Trait::Display,
Some(_) => Trait::Display,
None => {
has_bonus_display = true;
args.extend(quote_spanned!(span=> .as_display()));
Trait::Display
}
};
implied_bounds.insert((field, bound));
}
let formatvar = match &member {
MemberUnraw::Unnamed(index) => IdentUnraw::new(format_ident!("_{}", index)),
MemberUnraw::Named(ident) => ident.clone(),
};
out += &formatvar.to_string();
if !named_args.insert(formatvar.clone()) {
// Already specified in the format argument list.
continue;
}
if !has_trailing_comma {
args.extend(quote_spanned!(span=> ,));
}
let local = formatvar.to_local();
args.extend(quote_spanned!(span=> #formatvar = #local));
if read.starts_with('}') && member_index.contains_key(&member) {
has_bonus_display = true;
args.extend(quote_spanned!(span=> .as_display()));
}
has_trailing_comma = false;
}

Expand Down

0 comments on commit 5933179

Please sign in to comment.