@@ -17,28 +17,18 @@ use synstructure::{BindingInfo, Structure, VariantInfo};
1717use super :: utils:: SubdiagnosticVariant ;
1818
1919/// What kind of diagnostic is being derived - a fatal/error/warning or a lint?
20- #[ derive( Clone , PartialEq , Eq ) ]
20+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
2121pub ( crate ) enum DiagnosticDeriveKind {
22- Diagnostic { dcx : syn :: Ident } ,
22+ Diagnostic ,
2323 LintDiagnostic ,
2424}
2525
26- /// Tracks persistent information required for the entire type when building up individual calls to
27- /// diagnostic methods for generated diagnostic derives - both `Diagnostic` for
28- /// fatal/errors/warnings and `LintDiagnostic` for lints.
29- pub ( crate ) struct DiagnosticDeriveBuilder {
30- /// The identifier to use for the generated `DiagnosticBuilder` instance.
31- pub diag : syn:: Ident ,
32- /// Kind of diagnostic that should be derived.
33- pub kind : DiagnosticDeriveKind ,
34- }
35-
3626/// Tracks persistent information required for a specific variant when building up individual calls
3727/// to diagnostic methods for generated diagnostic derives - both `Diagnostic` for
3828/// fatal/errors/warnings and `LintDiagnostic` for lints.
39- pub ( crate ) struct DiagnosticDeriveVariantBuilder < ' parent > {
40- /// The parent builder for the entire type.
41- pub parent : & ' parent DiagnosticDeriveBuilder ,
29+ pub ( crate ) struct DiagnosticDeriveVariantBuilder {
30+ /// The kind for the entire type.
31+ pub kind : DiagnosticDeriveKind ,
4232
4333 /// Initialization of format strings for code suggestions.
4434 pub formatting_init : TokenStream ,
@@ -59,19 +49,19 @@ pub(crate) struct DiagnosticDeriveVariantBuilder<'parent> {
5949 pub code : SpannedOption < ( ) > ,
6050}
6151
62- impl < ' a > HasFieldMap for DiagnosticDeriveVariantBuilder < ' a > {
52+ impl HasFieldMap for DiagnosticDeriveVariantBuilder {
6353 fn get_field_binding ( & self , field : & String ) -> Option < & TokenStream > {
6454 self . field_map . get ( field)
6555 }
6656}
6757
68- impl DiagnosticDeriveBuilder {
58+ impl DiagnosticDeriveKind {
6959 /// Call `f` for the struct or for each variant of the enum, returning a `TokenStream` with the
7060 /// tokens from `f` wrapped in an `match` expression. Emits errors for use of derive on unions
7161 /// or attributes on the type itself when input is an enum.
72- pub ( crate ) fn each_variant < ' s , F > ( & mut self , structure : & mut Structure < ' s > , f : F ) -> TokenStream
62+ pub ( crate ) fn each_variant < ' s , F > ( self , structure : & mut Structure < ' s > , f : F ) -> TokenStream
7363 where
74- F : for <' a , ' v > Fn ( DiagnosticDeriveVariantBuilder < ' a > , & VariantInfo < ' v > ) -> TokenStream ,
64+ F : for < ' v > Fn ( DiagnosticDeriveVariantBuilder , & VariantInfo < ' v > ) -> TokenStream ,
7565 {
7666 let ast = structure. ast ( ) ;
7767 let span = ast. span ( ) . unwrap ( ) ;
@@ -101,7 +91,7 @@ impl DiagnosticDeriveBuilder {
10191 _ => variant. ast ( ) . ident . span ( ) . unwrap ( ) ,
10292 } ;
10393 let builder = DiagnosticDeriveVariantBuilder {
104- parent : self ,
94+ kind : self ,
10595 span,
10696 field_map : build_field_mapping ( variant) ,
10797 formatting_init : TokenStream :: new ( ) ,
@@ -119,7 +109,7 @@ impl DiagnosticDeriveBuilder {
119109 }
120110}
121111
122- impl < ' a > DiagnosticDeriveVariantBuilder < ' a > {
112+ impl DiagnosticDeriveVariantBuilder {
123113 /// Generates calls to `code` and similar functions based on the attributes on the type or
124114 /// variant.
125115 pub ( crate ) fn preamble ( & mut self , variant : & VariantInfo < ' _ > ) -> TokenStream {
@@ -184,8 +174,6 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
184174 & mut self ,
185175 attr : & Attribute ,
186176 ) -> Result < TokenStream , DiagnosticDeriveError > {
187- let diag = & self . parent . diag ;
188-
189177 // Always allow documentation comments.
190178 if is_doc_comment ( attr) {
191179 return Ok ( quote ! { } ) ;
@@ -223,7 +211,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
223211
224212 let code = nested. parse :: < syn:: LitStr > ( ) ?;
225213 tokens. extend ( quote ! {
226- # diag. code( rustc_errors:: DiagnosticId :: Error ( #code. to_string( ) ) ) ;
214+ diag. code( rustc_errors:: DiagnosticId :: Error ( #code. to_string( ) ) ) ;
227215 } ) ;
228216 } else {
229217 span_err ( path. span ( ) . unwrap ( ) , "unknown argument" )
@@ -257,8 +245,6 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
257245 }
258246
259247 fn generate_field_code ( & mut self , binding_info : & BindingInfo < ' _ > ) -> TokenStream {
260- let diag = & self . parent . diag ;
261-
262248 let field = binding_info. ast ( ) ;
263249 let mut field_binding = binding_info. binding . clone ( ) ;
264250 field_binding. set_span ( field. ty . span ( ) ) ;
@@ -267,7 +253,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
267253 let ident = format_ident ! ( "{}" , ident) ; // strip `r#` prefix, if present
268254
269255 quote ! {
270- # diag. set_arg(
256+ diag. set_arg(
271257 stringify!( #ident) ,
272258 #field_binding
273259 ) ;
@@ -322,21 +308,19 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
322308 info : FieldInfo < ' _ > ,
323309 binding : TokenStream ,
324310 ) -> Result < TokenStream , DiagnosticDeriveError > {
325- let diag = & self . parent . diag ;
326-
327311 let ident = & attr. path ( ) . segments . last ( ) . unwrap ( ) . ident ;
328312 let name = ident. to_string ( ) ;
329313 match ( & attr. meta , name. as_str ( ) ) {
330314 // Don't need to do anything - by virtue of the attribute existing, the
331315 // `set_arg` call will not be generated.
332316 ( Meta :: Path ( _) , "skip_arg" ) => return Ok ( quote ! { } ) ,
333317 ( Meta :: Path ( _) , "primary_span" ) => {
334- match self . parent . kind {
335- DiagnosticDeriveKind :: Diagnostic { .. } => {
318+ match self . kind {
319+ DiagnosticDeriveKind :: Diagnostic => {
336320 report_error_if_not_applied_to_span ( attr, & info) ?;
337321
338322 return Ok ( quote ! {
339- # diag. set_span( #binding) ;
323+ diag. set_span( #binding) ;
340324 } ) ;
341325 }
342326 DiagnosticDeriveKind :: LintDiagnostic => {
@@ -348,13 +332,13 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
348332 }
349333 ( Meta :: Path ( _) , "subdiagnostic" ) => {
350334 if FieldInnerTy :: from_type ( & info. binding . ast ( ) . ty ) . will_iterate ( ) {
351- let DiagnosticDeriveKind :: Diagnostic { dcx } = & self . parent . kind else {
335+ let DiagnosticDeriveKind :: Diagnostic = self . kind else {
352336 // No eager translation for lints.
353- return Ok ( quote ! { # diag. subdiagnostic( #binding) ; } ) ;
337+ return Ok ( quote ! { diag. subdiagnostic( #binding) ; } ) ;
354338 } ;
355- return Ok ( quote ! { # diag. eager_subdiagnostic( # dcx, #binding) ; } ) ;
339+ return Ok ( quote ! { diag. eager_subdiagnostic( dcx, #binding) ; } ) ;
356340 } else {
357- return Ok ( quote ! { # diag. subdiagnostic( #binding) ; } ) ;
341+ return Ok ( quote ! { diag. subdiagnostic( #binding) ; } ) ;
358342 }
359343 }
360344 ( Meta :: List ( meta_list) , "subdiagnostic" ) => {
@@ -376,15 +360,15 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
376360 return Ok ( quote ! { } ) ;
377361 }
378362
379- let dcx = match & self . parent . kind {
380- DiagnosticDeriveKind :: Diagnostic { dcx } => dcx ,
363+ match & self . kind {
364+ DiagnosticDeriveKind :: Diagnostic => { }
381365 DiagnosticDeriveKind :: LintDiagnostic => {
382366 throw_invalid_attr ! ( attr, |diag| {
383367 diag. help( "eager subdiagnostics are not supported on lints" )
384368 } )
385369 }
386370 } ;
387- return Ok ( quote ! { # diag. eager_subdiagnostic( # dcx, #binding) ; } ) ;
371+ return Ok ( quote ! { diag. eager_subdiagnostic( dcx, #binding) ; } ) ;
388372 }
389373 _ => ( ) ,
390374 }
@@ -442,7 +426,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
442426
443427 self . formatting_init . extend ( code_init) ;
444428 Ok ( quote ! {
445- # diag. span_suggestions_with_style(
429+ diag. span_suggestions_with_style(
446430 #span_field,
447431 crate :: fluent_generated:: #slug,
448432 #code_field,
@@ -463,10 +447,9 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
463447 kind : & Ident ,
464448 fluent_attr_identifier : Path ,
465449 ) -> TokenStream {
466- let diag = & self . parent . diag ;
467450 let fn_name = format_ident ! ( "span_{}" , kind) ;
468451 quote ! {
469- # diag. #fn_name(
452+ diag. #fn_name(
470453 #field_binding,
471454 crate :: fluent_generated:: #fluent_attr_identifier
472455 ) ;
@@ -476,9 +459,8 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
476459 /// Adds a subdiagnostic by generating a `diag.span_$kind` call with the current slug
477460 /// and `fluent_attr_identifier`.
478461 fn add_subdiagnostic ( & self , kind : & Ident , fluent_attr_identifier : Path ) -> TokenStream {
479- let diag = & self . parent . diag ;
480462 quote ! {
481- # diag. #kind( crate :: fluent_generated:: #fluent_attr_identifier) ;
463+ diag. #kind( crate :: fluent_generated:: #fluent_attr_identifier) ;
482464 }
483465 }
484466
0 commit comments