@@ -10,7 +10,7 @@ use rustc_middle::query::Providers;
1010use rustc_middle:: ty:: layout:: {
1111 FnAbiError , HasTyCtxt , HasTypingEnv , LayoutCx , LayoutOf , TyAndLayout , fn_can_unwind,
1212} ;
13- use rustc_middle:: ty:: { self , InstanceKind , ShimKind , Ty , TyCtxt , Unnormalized } ;
13+ use rustc_middle:: ty:: { self , Instance , InstanceKind , ShimKind , Ty , TyCtxt , Unnormalized } ;
1414use rustc_span:: def_id:: DefId ;
1515use rustc_span:: { DUMMY_SP , bug} ;
1616use rustc_target:: callconv:: {
@@ -231,8 +231,7 @@ struct FnAbiDesc<'tcx> {
231231 layout_cx : LayoutCx < ' tcx > ,
232232 sig : ty:: FnSig < ' tcx > ,
233233
234- /// The function's definition, if its body can be used to deduce parameter attributes.
235- determined_fn_def_id : Option < DefId > ,
234+ instance : Option < Instance < ' tcx > > ,
236235 caller_location : Option < Ty < ' tcx > > ,
237236 is_virtual_call : bool ,
238237 extra_args : & ' tcx [ Ty < ' tcx > ] ,
@@ -250,9 +249,7 @@ impl<'tcx> FnAbiDesc<'tcx> {
250249 typing_env,
251250 Unnormalized :: new_wip ( tcx. instantiate_bound_regions_with_erased ( sig) ) ,
252251 ) ,
253- // Parameter attributes can never be deduced for indirect calls, as there is no
254- // function body available to use.
255- determined_fn_def_id : None ,
252+ instance : None ,
256253 caller_location : None ,
257254 is_virtual_call : false ,
258255 extra_args,
@@ -265,19 +262,13 @@ impl<'tcx> FnAbiDesc<'tcx> {
265262 ) -> Self {
266263 let ty:: PseudoCanonicalInput { typing_env, value : ( instance, extra_args) } = query;
267264 let is_virtual_call = matches ! ( instance. def, ty:: InstanceKind :: Virtual ( ..) ) ;
268- let is_tls_shim_call =
269- matches ! ( instance. def, ty:: InstanceKind :: Shim ( ty:: ShimKind :: ThreadLocal ( _) ) ) ;
270265 Self {
271266 layout_cx : LayoutCx :: new ( tcx, typing_env) ,
272267 sig : tcx. normalize_erasing_regions (
273268 typing_env,
274269 Unnormalized :: new_wip ( fn_sig_for_fn_abi ( tcx, instance, typing_env) ) ,
275270 ) ,
276- // Parameter attributes can be deduced from the bodies of neither:
277- // - virtual calls, as they might call other functions from the vtable; nor
278- // - TLS shims, as they would refer to the underlying static.
279- determined_fn_def_id : ( !is_virtual_call && !is_tls_shim_call)
280- . then ( || instance. def_id ( ) ) ,
271+ instance : Some ( instance) ,
281272 caller_location : instance
282273 . def
283274 . requires_caller_location ( tcx)
@@ -312,11 +303,14 @@ fn fn_abi_of_instance_raw<'tcx>(
312303 // delegate to it here in order to reuse (and, if necessary, augment) its result.
313304 tcx. fn_abi_of_instance_no_deduced_attrs ( query) . map ( |fn_abi| {
314305 let params = FnAbiDesc :: for_instance ( tcx, query) ;
315- // If the function's body can be used to deduce parameter attributes, then adjust such
316- // "no deduced attrs" ABI; otherwise, return that ABI unadjusted.
317- params. determined_fn_def_id . map_or ( fn_abi, |fn_def_id| {
306+ // Apply attributes deduced from MIR body. This requires that an instance MIR is its optimized MIR.
307+ // For example, DropGlue(def_id, ty) MIR is compiler generated, while optimized MIR for def_id is
308+ // that of an empty placeholder function `drop_glue` from the standard library.
309+ if let Some ( Instance { def : InstanceKind :: Item ( fn_def_id) , .. } ) = params. instance {
318310 fn_abi_adjust_for_deduced_attrs ( & params. layout_cx , fn_abi, params. sig . abi ( ) , fn_def_id)
319- } )
311+ } else {
312+ fn_abi
313+ }
320314 } )
321315}
322316
@@ -327,7 +321,7 @@ fn arg_attrs_for_rust_scalar<'tcx>(
327321 layout : TyAndLayout < ' tcx > ,
328322 offset : Size ,
329323 is_return : bool ,
330- determined_fn_def_id : Option < DefId > ,
324+ instance : Option < Instance < ' tcx > > ,
331325) -> ArgAttributes {
332326 let mut attrs = ArgAttributes :: new ( ) ;
333327
@@ -381,13 +375,11 @@ fn arg_attrs_for_rust_scalar<'tcx>(
381375 && matches ! ( kind, PointerKind :: MutableRef { unpin: true } )
382376 && !is_return
383377 {
384- let rustc_no_writable = match determined_fn_def_id {
385- Some ( def_id) => find_attr ! ( tcx, def_id, RustcNoWritable ) ,
386- None => true , // If no def_id exists, we make the conservative choice and disable the feature.
387- } ;
388-
389- if !rustc_no_writable {
390- attrs. set ( ArgAttribute :: Writable ) ;
378+ if let Some ( Instance { def : InstanceKind :: Item ( def_id) , .. } ) = instance {
379+ let rustc_no_writable = find_attr ! ( tcx, def_id, RustcNoWritable ) ;
380+ if !rustc_no_writable {
381+ attrs. set ( ArgAttribute :: Writable ) ;
382+ }
391383 }
392384 }
393385
@@ -536,15 +528,12 @@ fn fn_abi_sanity_check<'tcx>(
536528 fn_arg_sanity_check ( cx, spec_abi, & fn_abi. ret , true ) ;
537529}
538530
539- #[ tracing:: instrument(
540- level = "debug" ,
541- skip( cx, caller_location, determined_fn_def_id, is_virtual_call)
542- ) ]
531+ #[ tracing:: instrument( level = "debug" , skip( cx, caller_location, instance, is_virtual_call) ) ]
543532fn fn_abi_new_uncached < ' tcx > (
544533 FnAbiDesc {
545534 layout_cx : ref cx,
546535 sig,
547- determined_fn_def_id ,
536+ instance ,
548537 caller_location,
549538 is_virtual_call,
550539 extra_args,
@@ -591,7 +580,7 @@ fn fn_abi_new_uncached<'tcx>(
591580 } ;
592581
593582 Ok ( ArgAbi :: new ( layout, |scalar, offset| {
594- arg_attrs_for_rust_scalar ( * cx, scalar, layout, offset, is_return, determined_fn_def_id )
583+ arg_attrs_for_rust_scalar ( * cx, scalar, layout, offset, is_return, instance )
595584 } ) )
596585 } ;
597586
@@ -611,8 +600,17 @@ fn fn_abi_new_uncached<'tcx>(
611600 // FIXME return false for tls shim
612601 can_unwind : fn_can_unwind (
613602 tcx,
614- // Since `#[rustc_nounwind]` can change unwinding, we cannot infer unwinding by `fn_def_id` for a virtual call.
615- determined_fn_def_id,
603+ instance. and_then ( |instance| match instance. def {
604+ InstanceKind :: Item ( item) => Some ( item) ,
605+ InstanceKind :: Intrinsic ( item) => Some ( item) ,
606+ // Used to recognize that with -Z panic-in-drop=abort, `drop_glue` never unwinds.
607+ InstanceKind :: Shim ( ShimKind :: DropGlue ( def_id, _ty) ) => Some ( def_id) ,
608+ InstanceKind :: Shim ( _) => None ,
609+ // Don't apply rustc_nounwind from trait method to impls.
610+ InstanceKind :: Virtual ( ..) => None ,
611+ // Not used with fn_abi_of_instance.
612+ InstanceKind :: LlvmIntrinsic ( _) => None ,
613+ } ) ,
616614 sig. abi ( ) ,
617615 ) ,
618616 } ;
0 commit comments