@@ -34,6 +34,27 @@ thread_local! {
34
34
static USED_ATTRS : RefCell <Vec <u64 >> = RefCell :: new( Vec :: new( ) )
35
35
}
36
36
37
+ enum AttrError {
38
+ MultipleItem ( InternedString ) ,
39
+ UnknownMetaItem ( InternedString ) ,
40
+ MissingSince ,
41
+ MissingFeature ,
42
+ MultipleStabilityLevels ,
43
+ }
44
+
45
+ fn handle_errors ( diag : & Handler , span : Span , error : AttrError ) {
46
+ match error {
47
+ AttrError :: MultipleItem ( item) => span_err ! ( diag, span, E0538 ,
48
+ "multiple '{}' items" , item) ,
49
+ AttrError :: UnknownMetaItem ( item) => span_err ! ( diag, span, E0541 ,
50
+ "unknown meta item '{}'" , item) ,
51
+ AttrError :: MissingSince => span_err ! ( diag, span, E0542 , "missing 'since'" ) ,
52
+ AttrError :: MissingFeature => span_err ! ( diag, span, E0546 , "missing 'feature'" ) ,
53
+ AttrError :: MultipleStabilityLevels => span_err ! ( diag, span, E0544 ,
54
+ "multiple stability levels" ) ,
55
+ }
56
+ }
57
+
37
58
pub fn mark_used ( attr : & Attribute ) {
38
59
let AttrId ( id) = attr. node . id ;
39
60
USED_ATTRS . with ( |slot| {
@@ -303,10 +324,10 @@ pub fn find_export_name_attr(diag: &Handler, attrs: &[Attribute]) -> Option<Inte
303
324
if let s@Some ( _) = attr. value_str ( ) {
304
325
s
305
326
} else {
306
- diag . struct_span_err ( attr. span ,
307
- "export_name attribute has invalid format" )
308
- . help ( "use #[export_name=\" *\" ]" )
309
- . emit ( ) ;
327
+ struct_span_err ! ( diag , attr. span, E0533 ,
328
+ "export_name attribute has invalid format" )
329
+ . help ( "use #[export_name=\" *\" ]" )
330
+ . emit ( ) ;
310
331
None
311
332
}
312
333
} else {
@@ -339,14 +360,16 @@ pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> In
339
360
MetaItemKind :: List ( ref n, ref items) if n == "inline" => {
340
361
mark_used ( attr) ;
341
362
if items. len ( ) != 1 {
342
- diagnostic. map ( |d|{ d . span_err ( attr. span , "expected one argument" ) ; } ) ;
363
+ diagnostic. map ( |d|{ span_err ! ( d , attr. span, E0534 , "expected one argument" ) ; } ) ;
343
364
InlineAttr :: None
344
365
} else if contains_name ( & items[ ..] , "always" ) {
345
366
InlineAttr :: Always
346
367
} else if contains_name ( & items[ ..] , "never" ) {
347
368
InlineAttr :: Never
348
369
} else {
349
- diagnostic. map ( |d|{ d. span_err ( ( * items[ 0 ] ) . span , "invalid argument" ) ; } ) ;
370
+ diagnostic. map ( |d| {
371
+ span_err ! ( d, ( * items[ 0 ] ) . span, E0535 , "invalid argument" ) ;
372
+ } ) ;
350
373
InlineAttr :: None
351
374
}
352
375
}
@@ -374,13 +397,13 @@ pub fn cfg_matches(cfgs: &[P<MetaItem>], cfg: &ast::MetaItem,
374
397
mis. iter ( ) . all ( |mi| cfg_matches ( cfgs, & mi, sess, features) ) ,
375
398
ast:: MetaItemKind :: List ( ref pred, ref mis) if & pred[ ..] == "not" => {
376
399
if mis. len ( ) != 1 {
377
- sess. span_diagnostic . span_err ( cfg. span , "expected 1 cfg-pattern" ) ;
400
+ span_err ! ( sess. span_diagnostic, cfg. span, E0536 , "expected 1 cfg-pattern" ) ;
378
401
return false ;
379
402
}
380
403
!cfg_matches ( cfgs, & mis[ 0 ] , sess, features)
381
404
}
382
405
ast:: MetaItemKind :: List ( ref pred, _) => {
383
- sess. span_diagnostic . span_err ( cfg. span , & format ! ( "invalid predicate `{}`" , pred) ) ;
406
+ span_err ! ( sess. span_diagnostic, cfg. span, E0537 , "invalid predicate `{}`" , pred) ;
384
407
false
385
408
} ,
386
409
ast:: MetaItemKind :: Word ( _) | ast:: MetaItemKind :: NameValue ( ..) => {
@@ -446,23 +469,23 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
446
469
if let Some ( metas) = attr. meta_item_list ( ) {
447
470
let get = |meta : & MetaItem , item : & mut Option < InternedString > | {
448
471
if item. is_some ( ) {
449
- diagnostic. span_err ( meta. span , & format ! ( "multiple '{}' items" ,
450
- meta. name( ) ) ) ;
472
+ handle_errors ( diagnostic, meta. span , AttrError :: MultipleItem ( meta. name ( ) ) ) ;
451
473
return false
452
474
}
453
475
if let Some ( v) = meta. value_str ( ) {
454
476
* item = Some ( v) ;
455
477
true
456
478
} else {
457
- diagnostic . span_err ( meta. span , "incorrect meta item" ) ;
479
+ span_err ! ( diagnostic , meta. span, E0539 , "incorrect meta item" ) ;
458
480
false
459
481
}
460
482
} ;
461
483
462
484
match tag {
463
485
"rustc_deprecated" => {
464
486
if rustc_depr. is_some ( ) {
465
- diagnostic. span_err ( item_sp, "multiple rustc_deprecated attributes" ) ;
487
+ span_err ! ( diagnostic, item_sp, E0540 ,
488
+ "multiple rustc_deprecated attributes" ) ;
466
489
break
467
490
}
468
491
@@ -473,8 +496,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
473
496
"since" => if !get ( meta, & mut since) { continue ' outer } ,
474
497
"reason" => if !get ( meta, & mut reason) { continue ' outer } ,
475
498
_ => {
476
- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
477
- meta. name( ) ) ) ;
499
+ handle_errors ( diagnostic , meta. span ,
500
+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
478
501
continue ' outer
479
502
}
480
503
}
@@ -488,18 +511,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
488
511
} )
489
512
}
490
513
( None , _) => {
491
- diagnostic . span_err ( attr. span ( ) , "missing 'since'" ) ;
514
+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingSince ) ;
492
515
continue
493
516
}
494
517
_ => {
495
- diagnostic . span_err ( attr. span ( ) , "missing 'reason'" ) ;
518
+ span_err ! ( diagnostic , attr. span( ) , E0543 , "missing 'reason'" ) ;
496
519
continue
497
520
}
498
521
}
499
522
}
500
523
"unstable" => {
501
524
if stab. is_some ( ) {
502
- diagnostic. span_err ( item_sp , "multiple stability levels" ) ;
525
+ handle_errors ( diagnostic, attr . span ( ) , AttrError :: MultipleStabilityLevels ) ;
503
526
break
504
527
}
505
528
@@ -512,8 +535,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
512
535
"reason" => if !get ( meta, & mut reason) { continue ' outer } ,
513
536
"issue" => if !get ( meta, & mut issue) { continue ' outer } ,
514
537
_ => {
515
- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
516
- meta. name( ) ) ) ;
538
+ handle_errors ( diagnostic , meta. span ,
539
+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
517
540
continue ' outer
518
541
}
519
542
}
@@ -528,7 +551,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
528
551
if let Ok ( issue) = issue. parse ( ) {
529
552
issue
530
553
} else {
531
- diagnostic. span_err ( attr. span ( ) , "incorrect 'issue'" ) ;
554
+ span_err ! ( diagnostic, attr. span( ) , E0545 ,
555
+ "incorrect 'issue'" ) ;
532
556
continue
533
557
}
534
558
}
@@ -538,18 +562,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
538
562
} )
539
563
}
540
564
( None , _, _) => {
541
- diagnostic . span_err ( attr. span ( ) , "missing 'feature'" ) ;
565
+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingFeature ) ;
542
566
continue
543
567
}
544
568
_ => {
545
- diagnostic . span_err ( attr. span ( ) , "missing 'issue'" ) ;
569
+ span_err ! ( diagnostic , attr. span( ) , E0547 , "missing 'issue'" ) ;
546
570
continue
547
571
}
548
572
}
549
573
}
550
574
"stable" => {
551
575
if stab. is_some ( ) {
552
- diagnostic. span_err ( item_sp , "multiple stability levels" ) ;
576
+ handle_errors ( diagnostic, attr . span ( ) , AttrError :: MultipleStabilityLevels ) ;
553
577
break
554
578
}
555
579
@@ -560,8 +584,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
560
584
"feature" => if !get ( meta, & mut feature) { continue ' outer } ,
561
585
"since" => if !get ( meta, & mut since) { continue ' outer } ,
562
586
_ => {
563
- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
564
- meta. name( ) ) ) ;
587
+ handle_errors ( diagnostic , meta. span ,
588
+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
565
589
continue ' outer
566
590
}
567
591
}
@@ -578,19 +602,19 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
578
602
} )
579
603
}
580
604
( None , _) => {
581
- diagnostic . span_err ( attr. span ( ) , "missing 'feature'" ) ;
605
+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingFeature ) ;
582
606
continue
583
607
}
584
608
_ => {
585
- diagnostic . span_err ( attr. span ( ) , "missing 'since'" ) ;
609
+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingSince ) ;
586
610
continue
587
611
}
588
612
}
589
613
}
590
614
_ => unreachable ! ( )
591
615
}
592
616
} else {
593
- diagnostic . span_err ( attr. span ( ) , "incorrect stability attribute type" ) ;
617
+ span_err ! ( diagnostic , attr. span( ) , E0548 , "incorrect stability attribute type" ) ;
594
618
continue
595
619
}
596
620
}
@@ -603,8 +627,9 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
603
627
}
604
628
stab. rustc_depr = Some ( rustc_depr) ;
605
629
} else {
606
- diagnostic. span_err ( item_sp, "rustc_deprecated attribute must be paired with \
607
- either stable or unstable attribute") ;
630
+ span_err ! ( diagnostic, item_sp, E0549 ,
631
+ "rustc_deprecated attribute must be paired with \
632
+ either stable or unstable attribute") ;
608
633
}
609
634
}
610
635
@@ -627,22 +652,21 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
627
652
mark_used ( attr) ;
628
653
629
654
if depr. is_some ( ) {
630
- diagnostic . span_err ( item_sp, "multiple deprecated attributes" ) ;
655
+ span_err ! ( diagnostic , item_sp, E0550 , "multiple deprecated attributes" ) ;
631
656
break
632
657
}
633
658
634
659
depr = if let Some ( metas) = attr. meta_item_list ( ) {
635
660
let get = |meta : & MetaItem , item : & mut Option < InternedString > | {
636
661
if item. is_some ( ) {
637
- diagnostic. span_err ( meta. span , & format ! ( "multiple '{}' items" ,
638
- meta. name( ) ) ) ;
662
+ handle_errors ( diagnostic, meta. span , AttrError :: MultipleItem ( meta. name ( ) ) ) ;
639
663
return false
640
664
}
641
665
if let Some ( v) = meta. value_str ( ) {
642
666
* item = Some ( v) ;
643
667
true
644
668
} else {
645
- diagnostic . span_err ( meta. span , "incorrect meta item" ) ;
669
+ span_err ! ( diagnostic , meta. span, E0551 , "incorrect meta item" ) ;
646
670
false
647
671
}
648
672
} ;
@@ -654,8 +678,8 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
654
678
"since" => if !get ( meta, & mut since) { continue ' outer } ,
655
679
"note" => if !get ( meta, & mut note) { continue ' outer } ,
656
680
_ => {
657
- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
658
- meta. name( ) ) ) ;
681
+ handle_errors ( diagnostic , meta. span ,
682
+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
659
683
continue ' outer
660
684
}
661
685
}
@@ -689,7 +713,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
689
713
690
714
if !set. insert ( name. clone ( ) ) {
691
715
panic ! ( diagnostic. span_fatal( meta. span,
692
- & format!( "duplicate meta item `{}`" , name) ) ) ;
716
+ & format!( "duplicate meta item `{}`" , name) ) ) ;
693
717
}
694
718
}
695
719
}
@@ -718,8 +742,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
718
742
Some ( ity) => Some ( ReprInt ( item. span , ity) ) ,
719
743
None => {
720
744
// Not a word we recognize
721
- diagnostic . span_err ( item. span ,
722
- "unrecognized representation hint" ) ;
745
+ span_err ! ( diagnostic , item. span, E0552 ,
746
+ "unrecognized representation hint" ) ;
723
747
None
724
748
}
725
749
}
@@ -731,7 +755,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
731
755
}
732
756
}
733
757
// Not a word:
734
- _ => diagnostic. span_err ( item. span , "unrecognized enum representation hint" )
758
+ _ => span_err ! ( diagnostic, item. span, E0553 ,
759
+ "unrecognized enum representation hint" ) ,
735
760
}
736
761
}
737
762
}
0 commit comments