@@ -708,12 +708,13 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
708708 . collect :: < Vec < _ > > ( ) ;
709709
710710 // check for exactly one autodiff attribute on extern block
711+ let msg_once = "autodiff attribute can only be applied once" ;
711712 let attr = match & attrs[ ..] {
712713 & [ ] => return AutoDiffAttrs :: inactive ( ) ,
713714 & [ elm] => elm,
714715 x => {
715716 tcx. sess
716- . struct_span_err ( x[ 1 ] . span , "autodiff attribute can only be applied once" )
717+ . struct_span_err ( x[ 1 ] . span , msg_once )
717718 . span_label ( x[ 1 ] . span , "more than one" )
718719 . emit ( ) ;
719720
@@ -732,13 +733,14 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
732733 } ;
733734 }
734735
736+ let msg_ad_mode = "autodiff attribute must contain autodiff mode" ;
735737 let mode = match & list[ 0 ] {
736738 NestedMetaItem :: MetaItem ( MetaItem { path : ref p2, kind : MetaItemKind :: Word , .. } ) => {
737739 p2. segments . first ( ) . unwrap ( ) . ident
738740 }
739741 _ => {
740742 tcx. sess
741- . struct_span_err ( attr. span , "attribute must contain autodiff mode" )
743+ . struct_span_err ( attr. span , msg_ad_mode )
742744 . span_label ( attr. span , "empty argument list" )
743745 . emit ( ) ;
744746
@@ -747,46 +749,50 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
747749 } ;
748750
749751 // parse mode
752+ let msg_mode = "mode should be either forward or reverse" ;
750753 let mode = match mode. as_str ( ) {
751754 //map(|x| x.as_str()) {
752755 "Forward" => DiffMode :: Forward ,
753756 "Reverse" => DiffMode :: Reverse ,
754757 _ => {
755758 tcx. sess
756- . struct_span_err ( attr. span , "mode should be either forward or reverse" )
759+ . struct_span_err ( attr. span , msg_mode )
757760 . span_label ( attr. span , "invalid mode" )
758761 . emit ( ) ;
759762
760763 return AutoDiffAttrs :: inactive ( ) ;
761764 }
762765 } ;
763766
767+ let msg_ret_activity = "autodiff attribute must contain the return activity" ;
764768 let ret_symbol = match & list[ 1 ] {
765769 NestedMetaItem :: MetaItem ( MetaItem { path : ref p2, kind : MetaItemKind :: Word , .. } ) => {
766770 p2. segments . first ( ) . unwrap ( ) . ident
767771 }
768772 _ => {
769773 tcx. sess
770- . struct_span_err ( attr. span , "autodiff attribute must contain the return activity" )
774+ . struct_span_err ( attr. span , msg_ret_activity )
771775 . span_label ( attr. span , "missing return activity" )
772776 . emit ( ) ;
773777
774778 return AutoDiffAttrs :: inactive ( ) ;
775779 }
776780 } ;
777781
782+ let msg_unknown_ret_activity = "unknown return activity" ;
778783 let ret_activity = match DiffActivity :: from_str ( ret_symbol. as_str ( ) ) {
779784 Ok ( x) => x,
780785 Err ( _) => {
781786 tcx. sess
782- . struct_span_err ( attr. span , "unknown return activity" )
787+ . struct_span_err ( attr. span , msg_unknown_ret_activity )
783788 . span_label ( attr. span , "invalid return activity" )
784789 . emit ( ) ;
785790
786791 return AutoDiffAttrs :: inactive ( ) ;
787792 }
788793 } ;
789794
795+ let msg_arg_activity = "autodiff attribute must contain the return activity" ;
790796 let mut arg_activities: Vec < DiffActivity > = vec ! [ ] ;
791797 for arg in & list[ 2 ..] {
792798 let arg_symbol = match arg {
@@ -796,8 +802,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
796802 _ => {
797803 tcx. sess
798804 . struct_span_err (
799- attr. span ,
800- "autodiff attribute must contain the return activity" ,
805+ attr. span , msg_arg_activity,
801806 )
802807 . span_label ( attr. span , "missing return activity" )
803808 . emit ( ) ;
@@ -810,7 +815,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
810815 Ok ( arg_activity) => arg_activities. push ( arg_activity) ,
811816 Err ( _) => {
812817 tcx. sess
813- . struct_span_err ( attr. span , "unknown return activity" )
818+ . struct_span_err ( attr. span , msg_unknown_ret_activity )
814819 . span_label ( attr. span , "invalid input activity" )
815820 . emit ( ) ;
816821
@@ -819,17 +824,20 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
819824 }
820825 }
821826
827+ let msg_fwd_incompatible_ret = "Forward Mode is incompatible with Active ret" ;
828+ let msg_fwd_incompatible_arg = "Forward Mode is incompatible with Active ret" ;
829+ let msg_rev_incompatible_arg = "Reverse Mode is only compatible with Active, None, or Const ret" ;
822830 if mode == DiffMode :: Forward {
823831 if ret_activity == DiffActivity :: Active {
824832 tcx. sess
825- . struct_span_err ( attr. span , "Forward Mode is incompatible with Active ret" )
833+ . struct_span_err ( attr. span , msg_fwd_incompatible_ret )
826834 . span_label ( attr. span , "invalid return activity" )
827835 . emit ( ) ;
828836 return AutoDiffAttrs :: inactive ( ) ;
829837 }
830838 if arg_activities. iter ( ) . filter ( |& x| * x == DiffActivity :: Active ) . count ( ) > 0 {
831839 tcx. sess
832- . struct_span_err ( attr. span , "Forward Mode is incompatible with Active args" )
840+ . struct_span_err ( attr. span , msg_fwd_incompatible_arg )
833841 . span_label ( attr. span , "invalid input activity" )
834842 . emit ( ) ;
835843 return AutoDiffAttrs :: inactive ( ) ;
@@ -842,8 +850,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
842850 {
843851 tcx. sess
844852 . struct_span_err (
845- attr. span ,
846- "Reverse Mode is only compatible with Active, None, or Const ret" ,
853+ attr. span , msg_rev_incompatible_arg,
847854 )
848855 . span_label ( attr. span , "invalid return activity" )
849856 . emit ( ) ;
0 commit comments