@@ -5,7 +5,8 @@ use crate::diagnostics::error::{
5
5
SessionDiagnosticDeriveError ,
6
6
} ;
7
7
use crate :: diagnostics:: utils:: {
8
- option_inner_ty, report_error_if_not_applied_to_span, type_matches_path, FieldInfo , HasFieldMap ,
8
+ option_inner_ty, report_error_if_not_applied_to_span, type_matches_path, FieldInfo ,
9
+ HasFieldMap , SetOnce ,
9
10
} ;
10
11
use proc_macro2:: TokenStream ;
11
12
use quote:: { format_ident, quote} ;
@@ -240,7 +241,7 @@ struct SessionDiagnosticDeriveBuilder {
240
241
slug : Option < ( String , proc_macro:: Span ) > ,
241
242
/// Error codes are a optional part of the struct attribute - this is only set to detect
242
243
/// multiple specifications.
243
- code : Option < proc_macro:: Span > ,
244
+ code : Option < ( String , proc_macro:: Span ) > ,
244
245
}
245
246
246
247
impl HasFieldMap for SessionDiagnosticDeriveBuilder {
@@ -306,7 +307,7 @@ impl SessionDiagnosticDeriveBuilder {
306
307
diag. help( "only `error` and `warning` are valid attributes" )
307
308
} ) ,
308
309
} ;
309
- self . set_kind_once ( kind, span) ? ;
310
+ self . kind . set_once ( ( kind, span) ) ;
310
311
311
312
let mut tokens = Vec :: new ( ) ;
312
313
for nested_attr in nested {
@@ -321,12 +322,17 @@ impl SessionDiagnosticDeriveBuilder {
321
322
// Struct attributes are only allowed to be applied once, and the diagnostic
322
323
// changes will be set in the initialisation code.
323
324
Meta :: NameValue ( MetaNameValue { lit : syn:: Lit :: Str ( s) , .. } ) => {
325
+ let span = s. span ( ) . unwrap ( ) ;
324
326
match nested_name. as_str ( ) {
325
327
"slug" => {
326
- self . set_slug_once ( s. value ( ) , s . span ( ) . unwrap ( ) ) ;
328
+ self . slug . set_once ( ( s. value ( ) , span) ) ;
327
329
}
328
330
"code" => {
329
- tokens. push ( self . set_code_once ( s. value ( ) , s. span ( ) . unwrap ( ) ) ) ;
331
+ self . code . set_once ( ( s. value ( ) , span) ) ;
332
+ let ( diag, code) = ( & self . diag , & self . code . as_ref ( ) . map ( |( v, _) | v) ) ;
333
+ tokens. push ( quote ! {
334
+ #diag. code( rustc_errors:: DiagnosticId :: Error ( #code. to_string( ) ) ) ;
335
+ } ) ;
330
336
}
331
337
_ => invalid_nested_attr ( attr, & nested_attr)
332
338
. help ( "only `slug` and `code` are valid nested attributes" )
@@ -340,61 +346,6 @@ impl SessionDiagnosticDeriveBuilder {
340
346
Ok ( tokens. drain ( ..) . collect ( ) )
341
347
}
342
348
343
- #[ must_use]
344
- fn set_kind_once (
345
- & mut self ,
346
- kind : SessionDiagnosticKind ,
347
- span : proc_macro:: Span ,
348
- ) -> Result < ( ) , SessionDiagnosticDeriveError > {
349
- match self . kind {
350
- None => {
351
- self . kind = Some ( ( kind, span) ) ;
352
- Ok ( ( ) )
353
- }
354
- Some ( ( prev_kind, prev_span) ) => {
355
- let existing = prev_kind. descr ( ) ;
356
- let current = kind. descr ( ) ;
357
-
358
- let msg = if current == existing {
359
- format ! ( "`{}` specified multiple times" , existing)
360
- } else {
361
- format ! ( "`{}` specified when `{}` was already specified" , current, existing)
362
- } ;
363
- throw_span_err ! ( span, & msg, |diag| diag
364
- . span_note( prev_span, "previously specified here" ) ) ;
365
- }
366
- }
367
- }
368
-
369
- fn set_code_once ( & mut self , code : String , span : proc_macro:: Span ) -> TokenStream {
370
- match self . code {
371
- None => {
372
- self . code = Some ( span) ;
373
- }
374
- Some ( prev_span) => {
375
- span_err ( span, "`code` specified multiple times" )
376
- . span_note ( prev_span, "previously specified here" )
377
- . emit ( ) ;
378
- }
379
- }
380
-
381
- let diag = & self . diag ;
382
- quote ! { #diag. code( rustc_errors:: DiagnosticId :: Error ( #code. to_string( ) ) ) ; }
383
- }
384
-
385
- fn set_slug_once ( & mut self , slug : String , span : proc_macro:: Span ) {
386
- match self . slug {
387
- None => {
388
- self . slug = Some ( ( slug, span) ) ;
389
- }
390
- Some ( ( _, prev_span) ) => {
391
- span_err ( span, "`slug` specified multiple times" )
392
- . span_note ( prev_span, "previously specified here" )
393
- . emit ( ) ;
394
- }
395
- }
396
- }
397
-
398
349
fn generate_field_attr_code (
399
350
& mut self ,
400
351
attr : & syn:: Attribute ,
0 commit comments