Skip to content

Commit

Permalink
Support Display and Debug of same path in error message
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 10, 2024
1 parent dc0359e commit 6388293
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::scan_expr::scan_expr;
use crate::unraw::{IdentUnraw, MemberUnraw};
use proc_macro2::{Delimiter, TokenStream, TokenTree};
use quote::{format_ident, quote, quote_spanned};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::collections::{BTreeSet, HashMap};
use std::iter;
use syn::ext::IdentExt;
use syn::parse::discouraged::Speculative;
Expand Down Expand Up @@ -34,7 +34,7 @@ impl Display<'_> {
let mut infinite_recursive = false;
let mut implied_bounds = BTreeSet::new();
let mut bindings = Vec::new();
let mut macro_named_args = HashSet::new();
let mut macro_named_args = BTreeSet::new();

self.requires_fmt_machinery = self.requires_fmt_machinery || fmt.contains('}');

Expand Down Expand Up @@ -112,15 +112,22 @@ impl Display<'_> {
out += &member.to_string();
continue;
}
let formatvar_prefix = if bonus_display {
"__display"
} else {
"__field"
};
let mut formatvar = IdentUnraw::new(match &member {
MemberUnraw::Unnamed(index) => format_ident!("__field{}", index),
MemberUnraw::Named(ident) => format_ident!("__field_{}", ident.to_string()),
MemberUnraw::Unnamed(index) => format_ident!("{}{}", formatvar_prefix, index),
MemberUnraw::Named(ident) => {
format_ident!("{}_{}", formatvar_prefix, ident.to_string())
}
});
while user_named_args.contains(&formatvar) {
formatvar = IdentUnraw::new(format_ident!("_{}", formatvar.to_string()));
}
out += &formatvar.to_string();
if !macro_named_args.insert(member.clone()) {
if !macro_named_args.insert(formatvar.clone()) {
// Already added to bindings by a previous use.
continue;
}
Expand Down

0 comments on commit 6388293

Please sign in to comment.