@@ -4,17 +4,16 @@ use crate::diagnostics::error::{
4
4
invalid_attr, span_err, throw_invalid_attr, throw_span_err, DiagnosticDeriveError ,
5
5
} ;
6
6
use crate :: diagnostics:: utils:: {
7
- build_field_mapping, is_doc_comment, new_code_ident,
8
- report_error_if_not_applied_to_applicability, report_error_if_not_applied_to_span, FieldInfo ,
9
- FieldInnerTy , FieldMap , HasFieldMap , SetOnce , SpannedOption , SubdiagnosticKind ,
7
+ build_field_mapping, build_suggestion_code, is_doc_comment, new_code_ident,
8
+ report_error_if_not_applied_to_applicability, report_error_if_not_applied_to_span,
9
+ should_generate_set_arg, AllowMultipleAlternatives , FieldInfo , FieldInnerTy , FieldMap ,
10
+ HasFieldMap , SetOnce , SpannedOption , SubdiagnosticKind ,
10
11
} ;
11
12
use proc_macro2:: TokenStream ;
12
13
use quote:: { format_ident, quote} ;
13
14
use syn:: { spanned:: Spanned , Attribute , Meta , MetaList , Path } ;
14
15
use synstructure:: { BindingInfo , Structure , VariantInfo } ;
15
16
16
- use super :: utils:: { build_suggestion_code, AllowMultipleAlternatives } ;
17
-
18
17
/// The central struct for constructing the `add_to_diagnostic` method from an annotated struct.
19
18
pub ( crate ) struct SubdiagnosticDeriveBuilder {
20
19
diag : syn:: Ident ,
@@ -210,19 +209,20 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
210
209
}
211
210
212
211
/// Generates the code for a field with no attributes.
213
- fn generate_field_set_arg ( & mut self , binding : & BindingInfo < ' _ > ) -> TokenStream {
214
- let ast = binding. ast ( ) ;
215
- assert_eq ! ( ast. attrs. len( ) , 0 , "field with attribute used as diagnostic arg" ) ;
216
-
212
+ fn generate_field_set_arg ( & mut self , binding_info : & BindingInfo < ' _ > ) -> TokenStream {
217
213
let diag = & self . parent . diag ;
218
- let ident = ast. ident . as_ref ( ) . unwrap ( ) ;
219
- // strip `r#` prefix, if present
220
- let ident = format_ident ! ( "{}" , ident) ;
214
+
215
+ let field = binding_info. ast ( ) ;
216
+ let mut field_binding = binding_info. binding . clone ( ) ;
217
+ field_binding. set_span ( field. ty . span ( ) ) ;
218
+
219
+ let ident = field. ident . as_ref ( ) . unwrap ( ) ;
220
+ let ident = format_ident ! ( "{}" , ident) ; // strip `r#` prefix, if present
221
221
222
222
quote ! {
223
223
#diag. set_arg(
224
224
stringify!( #ident) ,
225
- #binding
225
+ #field_binding
226
226
) ;
227
227
}
228
228
}
@@ -399,7 +399,8 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
399
399
clone_suggestion_code : bool ,
400
400
) -> Result < TokenStream , DiagnosticDeriveError > {
401
401
let span = attr. span ( ) . unwrap ( ) ;
402
- let ident = & list. path . segments . last ( ) . unwrap ( ) . ident ;
402
+ let mut ident = list. path . segments . last ( ) . unwrap ( ) . ident . clone ( ) ;
403
+ ident. set_span ( info. ty . span ( ) ) ;
403
404
let name = ident. to_string ( ) ;
404
405
let name = name. as_str ( ) ;
405
406
@@ -498,7 +499,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
498
499
. variant
499
500
. bindings ( )
500
501
. iter ( )
501
- . filter ( |binding| !binding. ast ( ) . attrs . is_empty ( ) )
502
+ . filter ( |binding| !should_generate_set_arg ( binding. ast ( ) ) )
502
503
. map ( |binding| self . generate_field_attr_code ( binding, kind_stats) )
503
504
. collect ( ) ;
504
505
@@ -580,7 +581,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
580
581
. variant
581
582
. bindings ( )
582
583
. iter ( )
583
- . filter ( |binding| binding. ast ( ) . attrs . is_empty ( ) )
584
+ . filter ( |binding| should_generate_set_arg ( binding. ast ( ) ) )
584
585
. map ( |binding| self . generate_field_set_arg ( binding) )
585
586
. collect ( ) ;
586
587
0 commit comments