Skip to content

Commit 5aa8f05

Browse files
committed
Initial implementation of FnPtr trait
This commit is an initial implementation of the `FnPtr` trait as described in the `fn_static` tracking issue, which consists of moving the internally unstable `core::marker::FnPtr` to `core::ops::FnPtr`, as well as changing the API. Because `NonNull` is used in the new `as_ptr` signature, it was also turned into a proper lang item.
1 parent 63b1dfc commit 5aa8f05

36 files changed

Lines changed: 241 additions & 97 deletions

File tree

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
638638
| ty::InstanceKind::FnPtrShim(..)
639639
| ty::InstanceKind::DropGlue(..)
640640
| ty::InstanceKind::CloneShim(..)
641-
| ty::InstanceKind::FnPtrAddrShim(..)
641+
| ty::InstanceKind::FnPtrAsPtrShim(..)
642+
| ty::InstanceKind::FnPtrFromPtrShim(..)
642643
| ty::InstanceKind::ThreadLocalShim(..)
643644
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
644645
| ty::InstanceKind::AsyncDropGlue(..)

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ declare_features! (
556556
(unstable, fn_align, "1.53.0", Some(82232)),
557557
/// Support delegating implementation of functions to other already implemented functions.
558558
(incomplete, fn_delegation, "1.76.0", Some(118212)),
559+
/// Traits for function pointers and items
560+
(unstable, fn_static, "CURRENT_RUSTC_VERSION", Some(148768)),
559561
/// Allows impls for the Freeze trait.
560562
(internal, freeze_impls, "1.78.0", Some(121675)),
561563
/// Frontmatter `---` blocks for use by external tools.

compiler/rustc_hir/src/lang_items.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,14 @@ language_item_table! {
175175
Metadata, sym::metadata_type, metadata_type, Target::AssocTy, GenericRequirement::None;
176176
DynMetadata, sym::dyn_metadata, dyn_metadata, Target::Struct, GenericRequirement::None;
177177

178+
NonNull, sym::non_null, non_null_trait, Target::Struct, GenericRequirement::Exact(1);
179+
178180
Freeze, sym::freeze, freeze_trait, Target::Trait, GenericRequirement::Exact(0);
179181
UnsafeUnpin, sym::unsafe_unpin, unsafe_unpin_trait, Target::Trait, GenericRequirement::Exact(0);
180182

181183
FnPtrTrait, sym::fn_ptr_trait, fn_ptr_trait, Target::Trait, GenericRequirement::Exact(0);
182-
FnPtrAddr, sym::fn_ptr_addr, fn_ptr_addr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
184+
FnPtrAsPtr, sym::fn_ptr_as_ptr, fn_ptr_as_ptr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
185+
FnPtrFromPtr, sym::fn_ptr_from_ptr, fn_ptr_from_ptr, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
183186

184187
Drop, sym::drop, drop_trait, Target::Trait, GenericRequirement::None;
185188
Destruct, sym::destruct, destruct_trait, Target::Trait, GenericRequirement::None;
@@ -235,6 +238,7 @@ language_item_table! {
235238
Fn, kw::Fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
236239
FnMut, sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
237240
FnOnce, sym::fn_once, fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
241+
FnStatic, sym::fn_static, fn_static_trait, Target::Trait, GenericRequirement::Exact(1);
238242

239243
AsyncFn, sym::async_fn, async_fn_trait, Target::Trait, GenericRequirement::Exact(1);
240244
AsyncFnMut, sym::async_fn_mut, async_fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,14 +1894,22 @@ fn check_method_receiver<'tcx>(
18941894
{
18951895
match receiver_validity_err {
18961896
ReceiverValidityError::DoesNotDeref if arbitrary_self_types_level.is_some() => {
1897-
let hint = match receiver_ty
1898-
.builtin_deref(false)
1899-
.unwrap_or(receiver_ty)
1900-
.ty_adt_def()
1901-
.and_then(|adt_def| tcx.get_diagnostic_name(adt_def.did()))
1902-
{
1903-
Some(sym::RcWeak | sym::ArcWeak) => Some(InvalidReceiverTyHint::Weak),
1904-
Some(sym::NonNull) => Some(InvalidReceiverTyHint::NonNull),
1897+
let adt_def =
1898+
receiver_ty.builtin_deref(false).unwrap_or(receiver_ty).ty_adt_def();
1899+
1900+
let hint = match adt_def {
1901+
Some(adt) => {
1902+
if tcx.is_lang_item(adt.did(), LangItem::NonNull) {
1903+
Some(InvalidReceiverTyHint::NonNull)
1904+
} else {
1905+
match tcx.get_diagnostic_name(adt.did()) {
1906+
Some(sym::RcWeak | sym::ArcWeak) => {
1907+
Some(InvalidReceiverTyHint::Weak)
1908+
}
1909+
_ => None,
1910+
}
1911+
}
1912+
}
19051913
_ => None,
19061914
};
19071915

compiler/rustc_lint/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ fn lint_wide_pointer<'tcx>(
312312
let mut modifiers = String::new();
313313
ty = match ty.kind() {
314314
ty::RawPtr(ty, _) => *ty,
315-
ty::Adt(def, args) if cx.tcx.is_diagnostic_item(sym::NonNull, def.did()) => {
315+
ty::Adt(def, args) if cx.tcx.is_lang_item(def.did(), LangItem::NonNull) => {
316316
modifiers.push_str(".as_ptr()");
317317
args.type_at(0)
318318
}

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ macro_rules! make_mir_visitor {
361361
ty::InstanceKind::FnPtrShim(_def_id, ty)
362362
| ty::InstanceKind::DropGlue(_def_id, Some(ty))
363363
| ty::InstanceKind::CloneShim(_def_id, ty)
364-
| ty::InstanceKind::FnPtrAddrShim(_def_id, ty)
364+
| ty::InstanceKind::FnPtrAsPtrShim(_def_id, ty)
365+
| ty::InstanceKind::FnPtrFromPtrShim(_def_id, ty)
365366
| ty::InstanceKind::AsyncDropGlue(_def_id, ty)
366367
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, ty) => {
367368
// FIXME(eddyb) use a better `TyContext` here.

compiler/rustc_middle/src/mono.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ impl<'tcx> CodegenUnit<'tcx> {
533533
| InstanceKind::DropGlue(..)
534534
| InstanceKind::CloneShim(..)
535535
| InstanceKind::ThreadLocalShim(..)
536-
| InstanceKind::FnPtrAddrShim(..)
536+
| InstanceKind::FnPtrAsPtrShim(..)
537+
| InstanceKind::FnPtrFromPtrShim(..)
537538
| InstanceKind::AsyncDropGlue(..)
538539
| InstanceKind::FutureDropPollShim(..)
539540
| InstanceKind::AsyncDropGlueCtorShim(..) => None,

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,19 @@ pub enum InstanceKind<'tcx> {
161161
/// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
162162
CloneShim(DefId, Ty<'tcx>),
163163

164-
/// Compiler-generated `<T as FnPtr>::addr` implementation.
164+
/// Compiler-generated `<T as FnPtr>::as_ptr` implementation.
165165
///
166166
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
167167
///
168168
/// The `DefId` is for `FnPtr::addr`, the `Ty` is the type `T`.
169-
FnPtrAddrShim(DefId, Ty<'tcx>),
169+
FnPtrAsPtrShim(DefId, Ty<'tcx>),
170+
171+
/// Compiler-generated `<T as FnPtr>::from_ptr` implementation.
172+
///
173+
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
174+
///
175+
/// The `DefId` is for `FnPtr::from_ptr`, the `Ty` is the type `T`.
176+
FnPtrFromPtrShim(DefId, Ty<'tcx>),
170177

171178
/// `core::future::async_drop::async_drop_in_place::<'_, T>`.
172179
///
@@ -254,7 +261,8 @@ impl<'tcx> InstanceKind<'tcx> {
254261
}
255262
| InstanceKind::DropGlue(def_id, _)
256263
| InstanceKind::CloneShim(def_id, _)
257-
| InstanceKind::FnPtrAddrShim(def_id, _)
264+
| InstanceKind::FnPtrAsPtrShim(def_id, _)
265+
| InstanceKind::FnPtrFromPtrShim(def_id, _)
258266
| InstanceKind::FutureDropPollShim(def_id, _, _)
259267
| InstanceKind::AsyncDropGlue(def_id, _)
260268
| InstanceKind::AsyncDropGlueCtorShim(def_id, _) => def_id,
@@ -279,7 +287,8 @@ impl<'tcx> InstanceKind<'tcx> {
279287
| ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
280288
| InstanceKind::DropGlue(..)
281289
| InstanceKind::CloneShim(..)
282-
| InstanceKind::FnPtrAddrShim(..) => None,
290+
| InstanceKind::FnPtrAsPtrShim(..)
291+
| InstanceKind::FnPtrFromPtrShim(..) => None,
283292
}
284293
}
285294

@@ -325,7 +334,8 @@ impl<'tcx> InstanceKind<'tcx> {
325334
match *self {
326335
InstanceKind::CloneShim(..)
327336
| InstanceKind::ThreadLocalShim(..)
328-
| InstanceKind::FnPtrAddrShim(..)
337+
| InstanceKind::FnPtrAsPtrShim(..)
338+
| InstanceKind::FnPtrFromPtrShim(..)
329339
| InstanceKind::FnPtrShim(..)
330340
| InstanceKind::DropGlue(_, Some(_))
331341
| InstanceKind::FutureDropPollShim(..)

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,8 @@ impl<'tcx> TyCtxt<'tcx> {
17411741
| ty::InstanceKind::DropGlue(..)
17421742
| ty::InstanceKind::CloneShim(..)
17431743
| ty::InstanceKind::ThreadLocalShim(..)
1744-
| ty::InstanceKind::FnPtrAddrShim(..)
1744+
| ty::InstanceKind::FnPtrAsPtrShim(..)
1745+
| ty::InstanceKind::FnPtrFromPtrShim(..)
17451746
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
17461747
| ty::InstanceKind::AsyncDropGlue(..) => self.mir_shims(instance),
17471748
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ impl<'tcx, P: Printer<'tcx> + std::fmt::Write> Print<P> for ty::Instance<'tcx> {
386386
cx.write_str(&format!(" - shim(Some({ty}))"))?
387387
}
388388
ty::InstanceKind::CloneShim(_, ty) => cx.write_str(&format!(" - shim({ty})"))?,
389-
ty::InstanceKind::FnPtrAddrShim(_, ty) => cx.write_str(&format!(" - shim({ty})"))?,
389+
ty::InstanceKind::FnPtrAsPtrShim(_, ty) => cx.write_str(&format!(" - shim({ty})"))?,
390+
ty::InstanceKind::FnPtrFromPtrShim(_, ty) => cx.write_str(&format!(" - shim({ty})"))?,
390391
ty::InstanceKind::FutureDropPollShim(_, proxy_ty, impl_ty) => {
391392
cx.write_str(&format!(" - dropshim({proxy_ty}-{impl_ty})"))?
392393
}

0 commit comments

Comments
 (0)