Skip to content

Commit

Permalink
Fix collision of numbered field references with user-written named arg
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 5, 2024
1 parent 51ccdf1 commit f8fd1bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
18 changes: 9 additions & 9 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 as Set, HashMap as Map};
use std::collections::{BTreeSet, HashMap as Map, HashSet};
use std::iter;
use syn::ext::IdentExt;
use syn::parse::discouraged::Speculative;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Display<'_> {
let mut out = String::new();
let mut args = self.args.clone();
let mut has_bonus_display = false;
let mut implied_bounds = Set::new();
let mut implied_bounds = BTreeSet::new();

let mut has_trailing_comma = false;
if let Some(TokenTree::Punct(punct)) = args.clone().into_iter().last() {
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Display<'_> {
MemberUnraw::Named(ident) => ident.clone(),
};
out += &formatvar.to_string();
if !named_args.insert(formatvar.clone()) {
if !named_args.insert(member.clone()) {
// Already specified in the format argument list.
continue;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Display<'_> {
}

struct FmtArguments {
named: Set<IdentUnraw>,
named: HashSet<MemberUnraw>,
first_unnamed: Option<TokenStream>,
}

Expand All @@ -150,15 +150,15 @@ fn explicit_named_args(input: ParseStream) -> Result<FmtArguments> {

input.parse::<TokenStream>().unwrap();
Ok(FmtArguments {
named: Set::new(),
named: HashSet::new(),
first_unnamed: None,
})
}

fn try_explicit_named_args(input: ParseStream) -> Result<FmtArguments> {
let mut syn_full = None;
let mut args = FmtArguments {
named: Set::new(),
named: HashSet::new(),
first_unnamed: None,
};

Expand All @@ -172,7 +172,7 @@ fn try_explicit_named_args(input: ParseStream) -> Result<FmtArguments> {
if input.peek(Ident::peek_any) && input.peek2(Token![=]) && !input.peek2(Token![==]) {
let ident: IdentUnraw = input.parse()?;
input.parse::<Token![=]>()?;
args.named.insert(ident);
args.named.insert(MemberUnraw::Named(ident));
} else {
begin_unnamed = Some(input.fork());
}
Expand All @@ -196,7 +196,7 @@ fn try_explicit_named_args(input: ParseStream) -> Result<FmtArguments> {

fn fallback_explicit_named_args(input: ParseStream) -> Result<FmtArguments> {
let mut args = FmtArguments {
named: Set::new(),
named: HashSet::new(),
first_unnamed: None,
};

Expand All @@ -209,7 +209,7 @@ fn fallback_explicit_named_args(input: ParseStream) -> Result<FmtArguments> {
input.parse::<Token![,]>()?;
let ident: IdentUnraw = input.parse()?;
input.parse::<Token![=]>()?;
args.named.insert(ident);
args.named.insert(MemberUnraw::Named(ident));
}
}

Expand Down
1 change: 1 addition & 0 deletions impl/src/unraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl ToTokens for IdentUnraw {
}
}

#[derive(Clone)]
pub enum MemberUnraw {
Named(IdentUnraw),
Unnamed(Index),
Expand Down

0 comments on commit f8fd1bf

Please sign in to comment.