Fix spans on macro-generated bindings and format variables

This commit is contained in:
David Tolnay 2024-12-17 18:49:14 -08:00
parent 6a07345135
commit f1243a0ceb
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
2 changed files with 15 additions and 7 deletions

View file

@ -107,12 +107,14 @@ impl Display<'_> {
}
};
infinite_recursive |= member == *"self" && bound == Trait::Display;
if let Some(&field) = member_index.get(&member) {
implied_bounds.insert((field, bound));
} else {
out += &member.to_string();
continue;
}
let field = match member_index.get(&member) {
Some(&field) => field,
None => {
out += &member.to_string();
continue;
}
};
implied_bounds.insert((field, bound));
let formatvar_prefix = if bonus_display {
"__display"
} else if bound == Trait::Pointer {
@ -129,15 +131,17 @@ impl Display<'_> {
while user_named_args.contains(&formatvar) {
formatvar = IdentUnraw::new(format_ident!("_{}", formatvar.to_string()));
}
formatvar.set_span(span);
out += &formatvar.to_string();
if !macro_named_args.insert(formatvar.clone()) {
// Already added to bindings by a previous use.
continue;
}
let binding_value = match &member {
let mut binding_value = match &member {
MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
MemberUnraw::Named(ident) => ident.to_local(),
};
binding_value.set_span(span.resolved_at(fields[field].member.span()));
let wrapped_binding_value = if bonus_display {
quote_spanned!(span=> #binding_value.as_display())
} else if bound == Trait::Pointer {

View file

@ -28,6 +28,10 @@ impl IdentUnraw {
}
unraw
}
pub fn set_span(&mut self, span: Span) {
self.0.set_span(span);
}
}
impl Display for IdentUnraw {