Skip to content

Commit 1c88d5b

Browse files
authored
Rollup merge of #165412 - bschoenmaeckers:fn-addr, r=fee1-dead
Implement `core::ops::FnAddr` This implements `FnAddr` as proposed in rust-lang/libs-team#837 (comment). This is my first contribution to the rust compiler, so I'm not 100% familiar with the process. All code was hand written by me. cc #148768
2 parents 793ada2 + 49918a6 commit 1c88d5b

25 files changed

Lines changed: 174 additions & 42 deletions

File tree

‎compiler/rustc_attr_ir/src/lang_items.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ language_item_table! {
187187
UnsafeUnpin, sym::unsafe_unpin, unsafe_unpin_trait, Target::Trait, GenericRequirement::Exact(0);
188188

189189
FnPtrTrait, sym::fn_ptr_trait, fn_ptr_trait, Target::Trait, GenericRequirement::Exact(0);
190-
FnPtrAsPtr, sym::fn_ptr_as_ptr, fn_ptr_as_ptr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
190+
FnAddrTrait, sym::fn_addr_trait, fn_addr_trait, Target::Trait, GenericRequirement::Exact(0);
191+
FnAddrAsPtr, sym::fn_addr_as_ptr, fn_addr_as_ptr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
192+
FnAddrAddrFn, sym::fn_addr_addr_fn, fn_addr_addr_fn, Target::Method(MethodKind::Trait { body: true }), GenericRequirement::None;
191193
FnPtrFromPtr, sym::fn_ptr_from_ptr, fn_ptr_from_ptr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
192194
Code, sym::code, code, Target::ForeignTy, GenericRequirement::None;
193195

‎compiler/rustc_const_eval/src/interpret/call.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
842842
| ty::InstanceKind::Shim(ty::ShimKind::FnPtr(..))
843843
| ty::InstanceKind::Shim(ty::ShimKind::DropGlue(..))
844844
| ty::InstanceKind::Shim(ty::ShimKind::Clone(..))
845-
| ty::InstanceKind::Shim(ty::ShimKind::FnPtrAsPtr(..))
845+
| ty::InstanceKind::Shim(ty::ShimKind::FnAddrAsPtr(..))
846846
| ty::InstanceKind::Shim(ty::ShimKind::FnPtrFromPtr(..))
847847
| ty::InstanceKind::Shim(ty::ShimKind::ThreadLocal(..))
848848
| ty::InstanceKind::Shim(ty::ShimKind::AsyncDropGlueCtor(..))

‎compiler/rustc_middle/src/mir/visit.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ macro_rules! make_mir_visitor {
362362
ty::InstanceKind::Shim(ty::ShimKind::FnPtr(_def_id, ty))
363363
| ty::InstanceKind::Shim(ty::ShimKind::DropGlue(_def_id, Some(ty)))
364364
| ty::InstanceKind::Shim(ty::ShimKind::Clone(_def_id, ty))
365-
| ty::InstanceKind::Shim(ty::ShimKind::FnPtrAsPtr(_def_id, ty))
365+
| ty::InstanceKind::Shim(ty::ShimKind::FnAddrAsPtr(_def_id, ty))
366366
| ty::InstanceKind::Shim(ty::ShimKind::FnPtrFromPtr(_def_id, ty))
367367
| ty::InstanceKind::Shim(ty::ShimKind::AsyncDropGlue(_def_id, ty))
368368
| ty::InstanceKind::Shim(ty::ShimKind::AsyncDropGlueCtor(_def_id, ty)) => {

‎compiler/rustc_middle/src/mono.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ impl<'tcx> CodegenUnit<'tcx> {
548548
| InstanceKind::Shim(ShimKind::DropGlue(..))
549549
| InstanceKind::Shim(ShimKind::Clone(..))
550550
| InstanceKind::Shim(ShimKind::ThreadLocal(..))
551-
| InstanceKind::Shim(ShimKind::FnPtrAsPtr(..))
551+
| InstanceKind::Shim(ShimKind::FnAddrAsPtr(..))
552552
| InstanceKind::Shim(ShimKind::FnPtrFromPtr(..))
553553
| InstanceKind::Shim(ShimKind::AsyncDropGlue(..))
554554
| InstanceKind::Shim(ShimKind::FutureDropPoll(..))

‎compiler/rustc_middle/src/ty/context/impl_interner.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ bidirectional_lang_item_map! {
841841
Drop,
842842
Field,
843843
Fn,
844+
FnAddrTrait,
844845
FnMut,
845846
FnOnce,
846847
FnPtrTrait,

‎compiler/rustc_middle/src/ty/instance.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub enum ShimKind<'tcx> {
182182
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
183183
///
184184
/// The `DefId` is for `FnPtr::as_ptr`, the `Ty` is the type `T`.
185-
FnPtrAsPtr(DefId, Ty<'tcx>),
185+
FnAddrAsPtr(DefId, Ty<'tcx>),
186186

187187
/// Compiler-generated `<T as FnPtr>::from_ptr` implementation.
188188
///
@@ -351,7 +351,7 @@ impl<'tcx> ShimKind<'tcx> {
351351
}
352352
| ShimKind::DropGlue(def_id, _)
353353
| ShimKind::Clone(def_id, _)
354-
| ShimKind::FnPtrAsPtr(def_id, _)
354+
| ShimKind::FnAddrAsPtr(def_id, _)
355355
| ShimKind::FnPtrFromPtr(def_id, _)
356356
| ShimKind::FutureDropPoll(def_id, _, _)
357357
| ShimKind::AsyncDropGlue(def_id, _)
@@ -374,7 +374,7 @@ impl<'tcx> ShimKind<'tcx> {
374374
| ShimKind::ConstructCoroutineInClosure { .. }
375375
| ShimKind::DropGlue(..)
376376
| ShimKind::Clone(..)
377-
| ShimKind::FnPtrAsPtr(..)
377+
| ShimKind::FnAddrAsPtr(..)
378378
| ShimKind::FnPtrFromPtr(..) => None,
379379
}
380380
}
@@ -395,7 +395,7 @@ impl<'tcx> ShimKind<'tcx> {
395395
ShimKind::Clone(..)
396396
| ShimKind::ThreadLocal(..)
397397
| ShimKind::FnPtr(..)
398-
| ShimKind::FnPtrAsPtr(..)
398+
| ShimKind::FnAddrAsPtr(..)
399399
| ShimKind::FnPtrFromPtr(..)
400400
| ShimKind::DropGlue(_, Some(_))
401401
| ShimKind::FutureDropPoll(..)

‎compiler/rustc_middle/src/ty/print/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl<'tcx, P: Printer<'tcx> + std::fmt::Write> Print<P> for ty::ShimKind<'tcx> {
400400
ty::ShimKind::DropGlue(_, None) => cx.write_str("shim(None)"),
401401
ty::ShimKind::DropGlue(_, Some(ty)) => cx.write_str(&format!("shim(Some({ty}))")),
402402
ty::ShimKind::Clone(_, ty) => cx.write_str(&format!("shim({ty})")),
403-
ty::ShimKind::FnPtrAsPtr(_, ty) => cx.write_str(&format!("shim({ty})")),
403+
ty::ShimKind::FnAddrAsPtr(_, ty) => cx.write_str(&format!("shim({ty})")),
404404
ty::ShimKind::FnPtrFromPtr(_, ty) => cx.write_str(&format!("shim({ty})")),
405405
ty::ShimKind::FutureDropPoll(_, proxy_ty, impl_ty) => {
406406
cx.write_str(&format!("dropshim({proxy_ty}-{impl_ty})"))

‎compiler/rustc_mir_transform/src/inline.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ fn check_mir_is_available<'tcx, I: Inliner<'tcx>>(
763763
| InstanceKind::Shim(ShimKind::DropGlue(..))
764764
| InstanceKind::Shim(ShimKind::Clone(..))
765765
| InstanceKind::Shim(ShimKind::ThreadLocal(..))
766-
| InstanceKind::Shim(ShimKind::FnPtrAsPtr(..))
766+
| InstanceKind::Shim(ShimKind::FnAddrAsPtr(..))
767767
| InstanceKind::Shim(ShimKind::FnPtrFromPtr(..)) => return Ok(()),
768768
}
769769

‎compiler/rustc_mir_transform/src/inline/cycle.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn should_recurse<'tcx>(tcx: TyCtxt<'tcx>, callee: ty::Instance<'tcx>) -> bool {
3636
| InstanceKind::Shim(ShimKind::Clone(..)) => {}
3737

3838
// This shim does not call any other functions, thus there can be no recursion.
39-
InstanceKind::Shim(ShimKind::FnPtrAsPtr(..) | ShimKind::FnPtrFromPtr(..)) => return false,
39+
InstanceKind::Shim(ShimKind::FnAddrAsPtr(..) | ShimKind::FnPtrFromPtr(..)) => return false,
4040

4141
// FIXME: A not fully instantiated drop shim can cause ICEs if one attempts to
4242
// have its MIR built. Likely oli-obk just screwed up the `ParamEnv`s, so this

‎compiler/rustc_mir_transform/src/shim.rs‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)