@@ -18,6 +18,7 @@ use rustc_middle::middle::codegen_fn_attrs::{
1818} ; 
1919use  rustc_middle:: mir:: mono:: Linkage ; 
2020use  rustc_middle:: query:: Providers ; 
21+ use  rustc_middle:: span_bug; 
2122use  rustc_middle:: ty:: { self  as  ty,  TyCtxt } ; 
2223use  rustc_session:: parse:: feature_err; 
2324use  rustc_session:: { Session ,  lint} ; 
@@ -872,114 +873,82 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
872873
873874    // check for exactly one autodiff attribute on placeholder functions. 
874875    // 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. 
878878    let  attr = match  attrs. len ( )  { 
879879        0  => return  AutoDiffAttrs :: error ( ) , 
880880        1  => attrs. get ( 0 ) . unwrap ( ) , 
881881        _ => { 
882882            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" ); 
885885        } 
886886    } ; 
887887
888888    let  list = attr. meta_item_list ( ) . unwrap_or_default ( ) ; 
889889
890890    // empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions 
891-     if  list. len ( )  ==  0  { 
891+     if  list. is_empty ( )  { 
892892        return  AutoDiffAttrs :: source ( ) ; 
893893    } 
894894
895895    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" ) ; 
901897    } ; 
902898    let  mode = if  let  MetaItemInner :: MetaItem ( MetaItem  {  path :  ref  p1,  .. } )  = mode { 
903899        p1. segments . first ( ) . unwrap ( ) . ident 
904900    }  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" ) ; 
908902    } ; 
909903
910904    // parse mode 
911-     let  msg_mode = "mode should be either forward or reverse" ; 
912905    let  mode = match  mode. as_str ( )  { 
913906        "Forward"  => DiffMode :: Forward , 
914907        "Reverse"  => DiffMode :: Reverse , 
915908        "ForwardFirst"  => DiffMode :: ForwardFirst , 
916909        "ReverseFirst"  => DiffMode :: ReverseFirst , 
917910        _ => { 
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" ) ; 
920912        } 
921913    } ; 
922914
923915    // First read the ret symbol from the attribute 
924916    let  ret_symbol = if  let  MetaItemInner :: MetaItem ( MetaItem  {  path :  ref  p1,  .. } )  = ret_activity { 
925917        p1. segments . first ( ) . unwrap ( ) . ident 
926918    }  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" ) ; 
930920    } ; 
931921
932922    // 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" ) ; 
943925    } ; 
944926
945927    // Now parse all the intermediate (input) activities 
946-     let  msg_arg_activity = "autodiff attribute must contain the return activity" ; 
947928    let  mut  arg_activities:  Vec < DiffActivity >  = vec ! [ ] ; 
948929    for  arg in  input_activities { 
949930        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  => {  span_bug ! ( attr. span,  "rustc_autodiff attribute must contain the input activity" ) ; } 
934+             } 
951935        }  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 ( ) ; 
936+             span_bug ! ( attr. span,  "rustc_autodiff attribute must contain the input activity" ) ; 
957937        } ; 
958938
959939        match  DiffActivity :: from_str ( arg_symbol. as_str ( ) )  { 
960940            Ok ( arg_activity)  => arg_activities. push ( arg_activity) , 
961-             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 ( ) ; 
967-             } 
941+             Err ( _)  => {  span_bug ! ( attr. span,  "invalid input activity" ) ;  } 
968942        } 
969943    } 
970944
971-     let  mut  msg = "" . to_string ( ) ; 
972945    for  & input in  & arg_activities { 
973946        if  !valid_input_activity ( mode,  input)  { 
974-             msg =  format ! ( "Invalid input activity {} for {} mode" ,  input,  mode) ; 
947+             span_bug ! ( attr . span ,   "Invalid input activity {} for {} mode" ,  input,  mode) ; 
975948        } 
976949    } 
977950    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 ( ) ; 
951+         span_bug ! ( attr. span,  "Invalid return activity {} for {} mode" ,  ret_activity,  mode) ; 
983952    } 
984953
985954    AutoDiffAttrs  {  mode,  ret_activity,  input_activity :  arg_activities } 
0 commit comments