@@ -34,6 +34,27 @@ thread_local! {
3434 static USED_ATTRS : RefCell <Vec <u64 >> = RefCell :: new( Vec :: new( ) )
3535}
3636
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+
3758pub fn mark_used ( attr : & Attribute ) {
3859 let AttrId ( id) = attr. node . id ;
3960 USED_ATTRS . with ( |slot| {
@@ -303,10 +324,10 @@ pub fn find_export_name_attr(diag: &Handler, attrs: &[Attribute]) -> Option<Inte
303324 if let s@Some ( _) = attr. value_str ( ) {
304325 s
305326 } 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 ( ) ;
310331 None
311332 }
312333 } else {
@@ -339,14 +360,16 @@ pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> In
339360 MetaItemKind :: List ( ref n, ref items) if n == "inline" => {
340361 mark_used ( attr) ;
341362 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" ) ; } ) ;
343364 InlineAttr :: None
344365 } else if contains_name ( & items[ ..] , "always" ) {
345366 InlineAttr :: Always
346367 } else if contains_name ( & items[ ..] , "never" ) {
347368 InlineAttr :: Never
348369 } 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+ } ) ;
350373 InlineAttr :: None
351374 }
352375 }
@@ -374,13 +397,13 @@ pub fn cfg_matches(cfgs: &[P<MetaItem>], cfg: &ast::MetaItem,
374397 mis. iter ( ) . all ( |mi| cfg_matches ( cfgs, & mi, sess, features) ) ,
375398 ast:: MetaItemKind :: List ( ref pred, ref mis) if & pred[ ..] == "not" => {
376399 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" ) ;
378401 return false ;
379402 }
380403 !cfg_matches ( cfgs, & mis[ 0 ] , sess, features)
381404 }
382405 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) ;
384407 false
385408 } ,
386409 ast:: MetaItemKind :: Word ( _) | ast:: MetaItemKind :: NameValue ( ..) => {
@@ -446,23 +469,23 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
446469 if let Some ( metas) = attr. meta_item_list ( ) {
447470 let get = |meta : & MetaItem , item : & mut Option < InternedString > | {
448471 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 ( ) ) ) ;
451473 return false
452474 }
453475 if let Some ( v) = meta. value_str ( ) {
454476 * item = Some ( v) ;
455477 true
456478 } else {
457- diagnostic . span_err ( meta. span , "incorrect meta item" ) ;
479+ span_err ! ( diagnostic , meta. span, E0539 , "incorrect meta item" ) ;
458480 false
459481 }
460482 } ;
461483
462484 match tag {
463485 "rustc_deprecated" => {
464486 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" ) ;
466489 break
467490 }
468491
@@ -473,8 +496,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
473496 "since" => if !get ( meta, & mut since) { continue ' outer } ,
474497 "reason" => if !get ( meta, & mut reason) { continue ' outer } ,
475498 _ => {
476- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
477- meta. name( ) ) ) ;
499+ handle_errors ( diagnostic , meta. span ,
500+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
478501 continue ' outer
479502 }
480503 }
@@ -488,18 +511,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
488511 } )
489512 }
490513 ( None , _) => {
491- diagnostic . span_err ( attr. span ( ) , "missing 'since'" ) ;
514+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingSince ) ;
492515 continue
493516 }
494517 _ => {
495- diagnostic . span_err ( attr. span ( ) , "missing 'reason'" ) ;
518+ span_err ! ( diagnostic , attr. span( ) , E0543 , "missing 'reason'" ) ;
496519 continue
497520 }
498521 }
499522 }
500523 "unstable" => {
501524 if stab. is_some ( ) {
502- diagnostic. span_err ( item_sp , "multiple stability levels" ) ;
525+ handle_errors ( diagnostic, attr . span ( ) , AttrError :: MultipleStabilityLevels ) ;
503526 break
504527 }
505528
@@ -512,8 +535,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
512535 "reason" => if !get ( meta, & mut reason) { continue ' outer } ,
513536 "issue" => if !get ( meta, & mut issue) { continue ' outer } ,
514537 _ => {
515- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
516- meta. name( ) ) ) ;
538+ handle_errors ( diagnostic , meta. span ,
539+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
517540 continue ' outer
518541 }
519542 }
@@ -528,7 +551,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
528551 if let Ok ( issue) = issue. parse ( ) {
529552 issue
530553 } else {
531- diagnostic. span_err ( attr. span ( ) , "incorrect 'issue'" ) ;
554+ span_err ! ( diagnostic, attr. span( ) , E0545 ,
555+ "incorrect 'issue'" ) ;
532556 continue
533557 }
534558 }
@@ -538,18 +562,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
538562 } )
539563 }
540564 ( None , _, _) => {
541- diagnostic . span_err ( attr. span ( ) , "missing 'feature'" ) ;
565+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingFeature ) ;
542566 continue
543567 }
544568 _ => {
545- diagnostic . span_err ( attr. span ( ) , "missing 'issue'" ) ;
569+ span_err ! ( diagnostic , attr. span( ) , E0547 , "missing 'issue'" ) ;
546570 continue
547571 }
548572 }
549573 }
550574 "stable" => {
551575 if stab. is_some ( ) {
552- diagnostic. span_err ( item_sp , "multiple stability levels" ) ;
576+ handle_errors ( diagnostic, attr . span ( ) , AttrError :: MultipleStabilityLevels ) ;
553577 break
554578 }
555579
@@ -560,8 +584,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
560584 "feature" => if !get ( meta, & mut feature) { continue ' outer } ,
561585 "since" => if !get ( meta, & mut since) { continue ' outer } ,
562586 _ => {
563- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
564- meta. name( ) ) ) ;
587+ handle_errors ( diagnostic , meta. span ,
588+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
565589 continue ' outer
566590 }
567591 }
@@ -578,19 +602,19 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
578602 } )
579603 }
580604 ( None , _) => {
581- diagnostic . span_err ( attr. span ( ) , "missing 'feature'" ) ;
605+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingFeature ) ;
582606 continue
583607 }
584608 _ => {
585- diagnostic . span_err ( attr. span ( ) , "missing 'since'" ) ;
609+ handle_errors ( diagnostic , attr. span ( ) , AttrError :: MissingSince ) ;
586610 continue
587611 }
588612 }
589613 }
590614 _ => unreachable ! ( )
591615 }
592616 } else {
593- diagnostic . span_err ( attr. span ( ) , "incorrect stability attribute type" ) ;
617+ span_err ! ( diagnostic , attr. span( ) , E0548 , "incorrect stability attribute type" ) ;
594618 continue
595619 }
596620 }
@@ -603,8 +627,9 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
603627 }
604628 stab. rustc_depr = Some ( rustc_depr) ;
605629 } 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") ;
608633 }
609634 }
610635
@@ -627,22 +652,21 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
627652 mark_used ( attr) ;
628653
629654 if depr. is_some ( ) {
630- diagnostic . span_err ( item_sp, "multiple deprecated attributes" ) ;
655+ span_err ! ( diagnostic , item_sp, E0550 , "multiple deprecated attributes" ) ;
631656 break
632657 }
633658
634659 depr = if let Some ( metas) = attr. meta_item_list ( ) {
635660 let get = |meta : & MetaItem , item : & mut Option < InternedString > | {
636661 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 ( ) ) ) ;
639663 return false
640664 }
641665 if let Some ( v) = meta. value_str ( ) {
642666 * item = Some ( v) ;
643667 true
644668 } else {
645- diagnostic . span_err ( meta. span , "incorrect meta item" ) ;
669+ span_err ! ( diagnostic , meta. span, E0551 , "incorrect meta item" ) ;
646670 false
647671 }
648672 } ;
@@ -654,8 +678,8 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
654678 "since" => if !get ( meta, & mut since) { continue ' outer } ,
655679 "note" => if !get ( meta, & mut note) { continue ' outer } ,
656680 _ => {
657- diagnostic . span_err ( meta . span , & format ! ( "unknown meta item '{}'" ,
658- meta. name( ) ) ) ;
681+ handle_errors ( diagnostic , meta. span ,
682+ AttrError :: UnknownMetaItem ( meta. name ( ) ) ) ;
659683 continue ' outer
660684 }
661685 }
@@ -689,7 +713,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
689713
690714 if !set. insert ( name. clone ( ) ) {
691715 panic ! ( diagnostic. span_fatal( meta. span,
692- & format!( "duplicate meta item `{}`" , name) ) ) ;
716+ & format!( "duplicate meta item `{}`" , name) ) ) ;
693717 }
694718 }
695719}
@@ -718,8 +742,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
718742 Some ( ity) => Some ( ReprInt ( item. span , ity) ) ,
719743 None => {
720744 // 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" ) ;
723747 None
724748 }
725749 }
@@ -731,7 +755,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
731755 }
732756 }
733757 // 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" ) ,
735760 }
736761 }
737762 }
0 commit comments