Skip to content

Commit 8e5fd99

Browse files
Rollup merge of #143060 - xizheyin:simplify-diag, r=oli-obk
Only args in main diag are saved and restored without removing the newly added ones cc #142724 Here's a more simplified approach, since we'll be storing and restoring the main diagnostic's arg, removing args newly added isn't needed in the derive subdiagnostic implementation. `remove_arg` is helpful only for manual implementation of subdiagnostic. r? ``@oli-obk``
2 parents 8256a75 + d1f3136 commit 8e5fd99

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
220220
}
221221

222222
/// Generates the code for a field with no attributes.
223-
fn generate_field_arg(&mut self, binding_info: &BindingInfo<'_>) -> (TokenStream, TokenStream) {
223+
fn generate_field_arg(&mut self, binding_info: &BindingInfo<'_>) -> TokenStream {
224224
let diag = &self.parent.diag;
225225

226226
let field = binding_info.ast();
@@ -230,16 +230,12 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
230230
let ident = field.ident.as_ref().unwrap();
231231
let ident = format_ident!("{}", ident); // strip `r#` prefix, if present
232232

233-
let args = quote! {
233+
quote! {
234234
#diag.arg(
235235
stringify!(#ident),
236236
#field_binding
237237
);
238-
};
239-
let remove_args = quote! {
240-
#diag.remove_arg(stringify!(#ident));
241-
};
242-
(args, remove_args)
238+
}
243239
}
244240

245241
/// Generates the necessary code for all attributes on a field.
@@ -610,7 +606,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
610606
let restore_args = quote! {
611607
#diag.restore_args();
612608
};
613-
let (plain_args, remove_args): (TokenStream, TokenStream) = self
609+
let plain_args: TokenStream = self
614610
.variant
615611
.bindings()
616612
.iter()
@@ -623,9 +619,8 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
623619
// For #[derive(Subdiagnostic)]
624620
//
625621
// - Store args of the main diagnostic for later restore.
626-
// - add args of subdiagnostic.
622+
// - Add args of subdiagnostic.
627623
// - Generate the calls, such as note, label, etc.
628-
// - Remove the arguments for allowing Vec<Subdiagnostic> to be used.
629624
// - Restore the arguments for allowing main and subdiagnostic share the same fields.
630625
Ok(quote! {
631626
#init
@@ -634,7 +629,6 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
634629
#store_args
635630
#plain_args
636631
#calls
637-
#remove_args
638632
#restore_args
639633
})
640634
}

0 commit comments

Comments
 (0)