@@ -18,6 +18,7 @@ use rustc_middle::middle::codegen_fn_attrs::{
18
18
} ;
19
19
use rustc_middle:: mir:: mono:: Linkage ;
20
20
use rustc_middle:: query:: Providers ;
21
+ use rustc_middle:: span_bug;
21
22
use rustc_middle:: ty:: { self as ty, TyCtxt } ;
22
23
use rustc_session:: parse:: feature_err;
23
24
use rustc_session:: { Session , lint} ;
@@ -872,114 +873,89 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
872
873
873
874
// check for exactly one autodiff attribute on placeholder functions.
874
875
// There should only be one, since we generate a new placeholder per ad macro.
875
- // TODO: re-enable this. We should fix that rustc_autodiff isn't applied multiple times to the
876
- // source function.
877
- let msg_once = "cg_ssa: implementation bug. Autodiff attribute can only be applied once" ;
876
+ // FIXME(ZuseZ4): re-enable this check. Currently we add multiple, which doesn't cause harm but
877
+ // looks strange e.g. under cargo-expand.
878
878
let attr = match attrs. len ( ) {
879
879
0 => return AutoDiffAttrs :: error ( ) ,
880
880
1 => attrs. get ( 0 ) . unwrap ( ) ,
881
881
_ => {
882
882
attrs. get ( 0 ) . unwrap ( )
883
- //tcx.dcx().struct_span_err(attrs[1].span, msg_once).with_note("more than one").emit();
884
- //return AutoDiffAttrs::error( );
883
+ //FIXME(ZuseZ4): re-enable this check
884
+ //span_bug!(attrs[1].span, "cg_ssa: rustc_autodiff should only exist once per source" );
885
885
}
886
886
} ;
887
887
888
888
let list = attr. meta_item_list ( ) . unwrap_or_default ( ) ;
889
889
890
890
// empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions
891
- if list. len ( ) == 0 {
891
+ if list. is_empty ( ) {
892
892
return AutoDiffAttrs :: source ( ) ;
893
893
}
894
894
895
895
let [ mode, input_activities @ .., ret_activity] = & list[ ..] else {
896
- tcx. dcx ( )
897
- . struct_span_err ( attr. span , msg_once)
898
- . with_note ( "Implementation bug in autodiff_attrs. Please report this!" )
899
- . emit ( ) ;
900
- return AutoDiffAttrs :: error ( ) ;
896
+ span_bug ! ( attr. span, "rustc_autodiff attribute must contain mode and activities" ) ;
901
897
} ;
902
898
let mode = if let MetaItemInner :: MetaItem ( MetaItem { path : ref p1, .. } ) = mode {
903
899
p1. segments . first ( ) . unwrap ( ) . ident
904
900
} else {
905
- let msg = "autodiff attribute must contain autodiff mode" ;
906
- tcx. dcx ( ) . struct_span_err ( attr. span , msg) . with_note ( "empty argument list" ) . emit ( ) ;
907
- return AutoDiffAttrs :: error ( ) ;
901
+ span_bug ! ( attr. span, "rustc_autodiff attribute must contain mode" ) ;
908
902
} ;
909
903
910
904
// parse mode
911
- let msg_mode = "mode should be either forward or reverse" ;
912
905
let mode = match mode. as_str ( ) {
913
906
"Forward" => DiffMode :: Forward ,
914
907
"Reverse" => DiffMode :: Reverse ,
915
908
"ForwardFirst" => DiffMode :: ForwardFirst ,
916
909
"ReverseFirst" => DiffMode :: ReverseFirst ,
917
910
_ => {
918
- tcx. dcx ( ) . struct_span_err ( attr. span , msg_mode) . with_note ( "invalid mode" ) . emit ( ) ;
919
- return AutoDiffAttrs :: error ( ) ;
911
+ span_bug ! ( attr. span, "rustc_autodiff attribute contains invalid mode" ) ;
920
912
}
921
913
} ;
922
914
923
915
// First read the ret symbol from the attribute
924
916
let ret_symbol = if let MetaItemInner :: MetaItem ( MetaItem { path : ref p1, .. } ) = ret_activity {
925
917
p1. segments . first ( ) . unwrap ( ) . ident
926
918
} else {
927
- let msg = "autodiff attribute must contain the return activity" ;
928
- tcx. dcx ( ) . struct_span_err ( attr. span , msg) . with_note ( "missing return activity" ) . emit ( ) ;
929
- return AutoDiffAttrs :: error ( ) ;
919
+ span_bug ! ( attr. span, "rustc_autodiff attribute must contain the return activity" ) ;
930
920
} ;
931
921
932
922
// Then parse it into an actual DiffActivity
933
- let msg_unknown_ret_activity = "unknown return activity" ;
934
- let ret_activity = match DiffActivity :: from_str ( ret_symbol. as_str ( ) ) {
935
- Ok ( x) => x,
936
- Err ( _) => {
937
- tcx. dcx ( )
938
- . struct_span_err ( attr. span , msg_unknown_ret_activity)
939
- . with_note ( "invalid return activity" )
940
- . emit ( ) ;
941
- return AutoDiffAttrs :: error ( ) ;
942
- }
923
+ let Ok ( ret_activity) = DiffActivity :: from_str ( ret_symbol. as_str ( ) ) else {
924
+ span_bug ! ( attr. span, "invalid return activity" ) ;
943
925
} ;
944
926
945
927
// Now parse all the intermediate (input) activities
946
- let msg_arg_activity = "autodiff attribute must contain the return activity" ;
947
928
let mut arg_activities: Vec < DiffActivity > = vec ! [ ] ;
948
929
for arg in input_activities {
949
930
let arg_symbol = if let MetaItemInner :: MetaItem ( MetaItem { path : ref p2, .. } ) = arg {
950
- p2. segments . first ( ) . unwrap ( ) . ident
931
+ match p2. segments . first ( ) {
932
+ Some ( x) => x. ident ,
933
+ None => {
934
+ span_bug ! (
935
+ attr. span,
936
+ "rustc_autodiff attribute must contain the input activity"
937
+ ) ;
938
+ }
939
+ }
951
940
} else {
952
- tcx. dcx ( )
953
- . struct_span_err ( attr. span , msg_arg_activity)
954
- . with_note ( "Implementation bug, please report this!" )
955
- . emit ( ) ;
956
- return AutoDiffAttrs :: error ( ) ;
941
+ span_bug ! ( attr. span, "rustc_autodiff attribute must contain the input activity" ) ;
957
942
} ;
958
943
959
944
match DiffActivity :: from_str ( arg_symbol. as_str ( ) ) {
960
945
Ok ( arg_activity) => arg_activities. push ( arg_activity) ,
961
946
Err ( _) => {
962
- tcx. dcx ( )
963
- . struct_span_err ( attr. span , msg_unknown_ret_activity)
964
- . with_note ( "invalid input activity" )
965
- . emit ( ) ;
966
- return AutoDiffAttrs :: error ( ) ;
947
+ span_bug ! ( attr. span, "invalid input activity" ) ;
967
948
}
968
949
}
969
950
}
970
951
971
- let mut msg = "" . to_string ( ) ;
972
952
for & input in & arg_activities {
973
953
if !valid_input_activity ( mode, input) {
974
- msg = format ! ( "Invalid input activity {} for {} mode" , input, mode) ;
954
+ span_bug ! ( attr . span , "Invalid input activity {} for {} mode" , input, mode) ;
975
955
}
976
956
}
977
957
if !valid_ret_activity ( mode, ret_activity) {
978
- msg = format ! ( "Invalid return activity {} for {} mode" , ret_activity, mode) ;
979
- }
980
- if msg != "" . to_string ( ) {
981
- tcx. dcx ( ) . struct_span_err ( attr. span , msg) . with_note ( "invalid activity" ) . emit ( ) ;
982
- return AutoDiffAttrs :: error ( ) ;
958
+ span_bug ! ( attr. span, "Invalid return activity {} for {} mode" , ret_activity, mode) ;
983
959
}
984
960
985
961
AutoDiffAttrs { mode, ret_activity, input_activity : arg_activities }
0 commit comments