@@ -126,7 +126,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, shim: ty::ShimKind<'tcx>) -> Body<'tcx> {
126126 }
127127 ty:: ShimKind :: ThreadLocal ( ..) => build_thread_local_shim ( tcx, shim) ,
128128 ty:: ShimKind :: Clone ( def_id, ty) => build_clone_shim ( tcx, def_id, ty) ,
129- ty:: ShimKind :: FnPtrAsPtr ( def_id, ty) => build_fn_ptr_as_ptr_shim ( tcx, def_id, ty) ,
129+ ty:: ShimKind :: FnAddrAsPtr ( def_id, ty) => build_fn_addr_as_ptr_shim ( tcx, def_id, ty) ,
130130 ty:: ShimKind :: FnPtrFromPtr ( def_id, ty) => build_fn_ptr_from_ptr_shim ( tcx, def_id, ty) ,
131131 ty:: ShimKind :: FutureDropPoll ( def_id, proxy_ty, impl_ty) => {
132132 let mut body =
@@ -1095,12 +1095,16 @@ pub(super) fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
10951095/// }
10961096/// }
10971097/// ```
1098- fn build_fn_ptr_as_ptr_shim < ' tcx > (
1098+ fn build_fn_addr_as_ptr_shim < ' tcx > (
10991099 tcx : TyCtxt < ' tcx > ,
11001100 def_id : DefId ,
11011101 self_ty : Ty < ' tcx > ,
11021102) -> Body < ' tcx > {
1103- assert_matches ! ( self_ty. kind( ) , ty:: FnPtr ( ..) , "expected fn ptr, found {self_ty}" ) ;
1103+ assert_matches ! (
1104+ self_ty. kind( ) ,
1105+ ty:: FnPtr ( ..) | ty:: FnDef ( ..) ,
1106+ "expected fn ptr or function def, found {self_ty}"
1107+ ) ;
11041108
11051109 let span = tcx. def_span ( def_id) ;
11061110 let nonnull_did = tcx. require_lang_item ( LangItem :: NonNull , span) ;
@@ -1116,7 +1120,7 @@ fn build_fn_ptr_as_ptr_shim<'tcx>(
11161120 let Some ( sig) =
11171121 tcx. fn_sig ( def_id) . instantiate ( tcx, & [ self_ty. into ( ) ] ) . skip_norm_wip ( ) . no_bound_vars ( )
11181122 else {
1119- span_bug ! ( span, "FnPtr ::as_ptr with bound vars for `{self_ty}`" ) ;
1123+ span_bug ! ( span, "FnAddr ::as_ptr with bound vars for `{self_ty}`" ) ;
11201124 } ;
11211125 let mut locals = local_decls_for_sig ( & sig, span) ;
11221126
@@ -1126,8 +1130,36 @@ fn build_fn_ptr_as_ptr_shim<'tcx>(
11261130 // FIXME: use `expose_provenance` once we figure out whether function pointers have meaningful
11271131 // provenance.
11281132 let raw_unit_ptr = Ty :: new_imm_ptr ( tcx, tcx. types . unit ) ;
1129- let cast_to_raw_rvalue =
1130- Rvalue :: Cast ( CastKind :: FnPtrToPtr , Operand :: Move ( Place :: from ( Local :: arg ( 0 ) ) ) , raw_unit_ptr) ;
1133+
1134+ let fn_ptr_operand = match self_ty. kind ( ) {
1135+ ty:: FnDef ( ..) => {
1136+ let fn_sig = self_ty. fn_sig ( tcx) ;
1137+ let fn_ptr_ty = Ty :: new_fn_ptr ( tcx, fn_sig) ;
1138+ let fn_ptr =
1139+ Place :: from ( locals. push ( LocalDecl :: with_source_info ( fn_ptr_ty, source_info) ) ) ;
1140+
1141+ statements. push ( Statement :: new (
1142+ source_info,
1143+ StatementKind :: Assign ( Box :: new ( (
1144+ fn_ptr,
1145+ Rvalue :: Cast (
1146+ CastKind :: PointerCoercion (
1147+ ty:: adjustment:: PointerCoercion :: ReifyFnPointer ( fn_sig. safety ( ) ) ,
1148+ CoercionSource :: Implicit ,
1149+ ) ,
1150+ Operand :: Move ( Place :: from ( Local :: arg ( 0 ) ) ) ,
1151+ fn_ptr_ty,
1152+ ) ,
1153+ ) ) ) ,
1154+ ) ) ;
1155+
1156+ Operand :: Move ( fn_ptr)
1157+ }
1158+ _ => Operand :: Move ( Place :: from ( Local :: arg ( 0 ) ) ) ,
1159+ } ;
1160+
1161+ let cast_to_raw_rvalue = Rvalue :: Cast ( CastKind :: FnPtrToPtr , fn_ptr_operand, raw_unit_ptr) ;
1162+
11311163 let raw_ptr = locals. push ( LocalDecl :: with_source_info ( raw_unit_ptr, source_info) ) . into ( ) ;
11321164 statements. push ( Statement :: new (
11331165 source_info,
@@ -1146,7 +1178,7 @@ fn build_fn_ptr_as_ptr_shim<'tcx>(
11461178 Terminator { source_info, kind : TerminatorKind :: Return } ,
11471179 false ,
11481180 ) ;
1149- let source = MirSource :: from_shim ( ty:: ShimKind :: FnPtrAsPtr ( def_id, self_ty) ) ;
1181+ let source = MirSource :: from_shim ( ty:: ShimKind :: FnAddrAsPtr ( def_id, self_ty) ) ;
11501182 new_body ( source, IndexVec :: from_elem_n ( start_block, 1 ) , locals, sig. inputs ( ) . len ( ) , span)
11511183}
11521184
0 commit comments