Skip to content

Commit

Permalink
Check invalid Named member references before producing MemberUnraw
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 8, 2024
1 parent 2f6bff3 commit 1bb7e7a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,25 @@ impl Display<'_> {
if read.starts_with("r#") {
continue;
}
let ident = Ident::new(take_ident(&mut read), span);
MemberUnraw::Named(IdentUnraw::new(ident))
let repr = take_ident(&mut read);
if repr == "_" {
// Invalid. Let rustc produce the diagnostic.
out += repr;
continue;
}
let ident = IdentUnraw::new(Ident::new(repr, span));
if user_named_args.contains(&ident) {
// Refers to a named argument written by the user, not to field.
out += repr;
continue;
}
MemberUnraw::Named(ident)
}
_ => continue,
};
let mut formatvar = match &member {
MemberUnraw::Unnamed(index) => IdentUnraw::new(format_ident!("__field{}", index)),
MemberUnraw::Named(ident) => {
if user_named_args.contains(ident) || ident == "_" {
// Refers to a named argument written by the user, not to field.
out += &ident.to_string();
continue;
}
IdentUnraw::new(format_ident!("__field_{}", ident.to_string()))
}
};
Expand Down

0 comments on commit 1bb7e7a

Please sign in to comment.