@@ -708,12 +708,13 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
708
708
. collect :: < Vec < _ > > ( ) ;
709
709
710
710
// check for exactly one autodiff attribute on extern block
711
+ let msg_once = "autodiff attribute can only be applied once" ;
711
712
let attr = match & attrs[ ..] {
712
713
& [ ] => return AutoDiffAttrs :: inactive ( ) ,
713
714
& [ elm] => elm,
714
715
x => {
715
716
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 )
717
718
. span_label ( x[ 1 ] . span , "more than one" )
718
719
. emit ( ) ;
719
720
@@ -732,13 +733,14 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
732
733
} ;
733
734
}
734
735
736
+ let msg_ad_mode = "autodiff attribute must contain autodiff mode" ;
735
737
let mode = match & list[ 0 ] {
736
738
NestedMetaItem :: MetaItem ( MetaItem { path : ref p2, kind : MetaItemKind :: Word , .. } ) => {
737
739
p2. segments . first ( ) . unwrap ( ) . ident
738
740
}
739
741
_ => {
740
742
tcx. sess
741
- . struct_span_err ( attr. span , "attribute must contain autodiff mode" )
743
+ . struct_span_err ( attr. span , msg_ad_mode )
742
744
. span_label ( attr. span , "empty argument list" )
743
745
. emit ( ) ;
744
746
@@ -747,46 +749,50 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
747
749
} ;
748
750
749
751
// parse mode
752
+ let msg_mode = "mode should be either forward or reverse" ;
750
753
let mode = match mode. as_str ( ) {
751
754
//map(|x| x.as_str()) {
752
755
"Forward" => DiffMode :: Forward ,
753
756
"Reverse" => DiffMode :: Reverse ,
754
757
_ => {
755
758
tcx. sess
756
- . struct_span_err ( attr. span , "mode should be either forward or reverse" )
759
+ . struct_span_err ( attr. span , msg_mode )
757
760
. span_label ( attr. span , "invalid mode" )
758
761
. emit ( ) ;
759
762
760
763
return AutoDiffAttrs :: inactive ( ) ;
761
764
}
762
765
} ;
763
766
767
+ let msg_ret_activity = "autodiff attribute must contain the return activity" ;
764
768
let ret_symbol = match & list[ 1 ] {
765
769
NestedMetaItem :: MetaItem ( MetaItem { path : ref p2, kind : MetaItemKind :: Word , .. } ) => {
766
770
p2. segments . first ( ) . unwrap ( ) . ident
767
771
}
768
772
_ => {
769
773
tcx. sess
770
- . struct_span_err ( attr. span , "autodiff attribute must contain the return activity" )
774
+ . struct_span_err ( attr. span , msg_ret_activity )
771
775
. span_label ( attr. span , "missing return activity" )
772
776
. emit ( ) ;
773
777
774
778
return AutoDiffAttrs :: inactive ( ) ;
775
779
}
776
780
} ;
777
781
782
+ let msg_unknown_ret_activity = "unknown return activity" ;
778
783
let ret_activity = match DiffActivity :: from_str ( ret_symbol. as_str ( ) ) {
779
784
Ok ( x) => x,
780
785
Err ( _) => {
781
786
tcx. sess
782
- . struct_span_err ( attr. span , "unknown return activity" )
787
+ . struct_span_err ( attr. span , msg_unknown_ret_activity )
783
788
. span_label ( attr. span , "invalid return activity" )
784
789
. emit ( ) ;
785
790
786
791
return AutoDiffAttrs :: inactive ( ) ;
787
792
}
788
793
} ;
789
794
795
+ let msg_arg_activity = "autodiff attribute must contain the return activity" ;
790
796
let mut arg_activities: Vec < DiffActivity > = vec ! [ ] ;
791
797
for arg in & list[ 2 ..] {
792
798
let arg_symbol = match arg {
@@ -796,8 +802,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
796
802
_ => {
797
803
tcx. sess
798
804
. struct_span_err (
799
- attr. span ,
800
- "autodiff attribute must contain the return activity" ,
805
+ attr. span , msg_arg_activity,
801
806
)
802
807
. span_label ( attr. span , "missing return activity" )
803
808
. emit ( ) ;
@@ -810,7 +815,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
810
815
Ok ( arg_activity) => arg_activities. push ( arg_activity) ,
811
816
Err ( _) => {
812
817
tcx. sess
813
- . struct_span_err ( attr. span , "unknown return activity" )
818
+ . struct_span_err ( attr. span , msg_unknown_ret_activity )
814
819
. span_label ( attr. span , "invalid input activity" )
815
820
. emit ( ) ;
816
821
@@ -819,17 +824,20 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
819
824
}
820
825
}
821
826
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" ;
822
830
if mode == DiffMode :: Forward {
823
831
if ret_activity == DiffActivity :: Active {
824
832
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 )
826
834
. span_label ( attr. span , "invalid return activity" )
827
835
. emit ( ) ;
828
836
return AutoDiffAttrs :: inactive ( ) ;
829
837
}
830
838
if arg_activities. iter ( ) . filter ( |& x| * x == DiffActivity :: Active ) . count ( ) > 0 {
831
839
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 )
833
841
. span_label ( attr. span , "invalid input activity" )
834
842
. emit ( ) ;
835
843
return AutoDiffAttrs :: inactive ( ) ;
@@ -842,8 +850,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
842
850
{
843
851
tcx. sess
844
852
. 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,
847
854
)
848
855
. span_label ( attr. span , "invalid return activity" )
849
856
. emit ( ) ;
0 commit comments