Skip to content

Commit bdd8042

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 8b6558a commit bdd8042

36 files changed

Lines changed: 244 additions & 100 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
@@ -659,7 +659,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
659659
| ty::InstanceKind::FnPtrShim(..)
660660
| ty::InstanceKind::DropGlue(..)
661661
| ty::InstanceKind::CloneShim(..)
662-
| ty::InstanceKind::FnPtrAddrShim(..)
662+
| ty::InstanceKind::FnPtrAsPtrShim(..)
663+
| ty::InstanceKind::FnPtrFromPtrShim(..)
663664
| ty::InstanceKind::ThreadLocalShim(..)
664665
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
665666
| ty::InstanceKind::AsyncDropGlue(..)

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ declare_features! (
560560
(unstable, fn_align, "1.53.0", Some(82232)),
561561
/// Support delegating implementation of functions to other already implemented functions.
562562
(incomplete, fn_delegation, "1.76.0", Some(118212)),
563+
/// Traits for function pointers and items
564+
(unstable, fn_static, "CURRENT_RUSTC_VERSION", Some(148768)),
563565
/// Allows impls for the Freeze trait.
564566
(internal, freeze_impls, "1.78.0", Some(121675)),
565567
/// 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
@@ -1866,14 +1866,22 @@ fn check_method_receiver<'tcx>(
18661866
{
18671867
match receiver_validity_err {
18681868
ReceiverValidityError::DoesNotDeref if arbitrary_self_types_level.is_some() => {
1869-
let hint = match receiver_ty
1870-
.builtin_deref(false)
1871-
.unwrap_or(receiver_ty)
1872-
.ty_adt_def()
1873-
.and_then(|adt_def| tcx.get_diagnostic_name(adt_def.did()))
1874-
{
1875-
Some(sym::RcWeak | sym::ArcWeak) => Some(InvalidReceiverTyHint::Weak),
1876-
Some(sym::NonNull) => Some(InvalidReceiverTyHint::NonNull),
1869+
let adt_def =
1870+
receiver_ty.builtin_deref(false).unwrap_or(receiver_ty).ty_adt_def();
1871+
1872+
let hint = match adt_def {
1873+
Some(adt) => {
1874+
if tcx.is_lang_item(adt.did(), LangItem::NonNull) {
1875+
Some(InvalidReceiverTyHint::NonNull)
1876+
} else {
1877+
match tcx.get_diagnostic_name(adt.did()) {
1878+
Some(sym::RcWeak | sym::ArcWeak) => {
1879+
Some(InvalidReceiverTyHint::Weak)
1880+
}
1881+
_ => None,
1882+
}
1883+
}
1884+
}
18771885
_ => None,
18781886
};
18791887

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
@@ -164,12 +164,19 @@ pub enum InstanceKind<'tcx> {
164164
/// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
165165
CloneShim(DefId, Ty<'tcx>),
166166

167-
/// Compiler-generated `<T as FnPtr>::addr` implementation.
167+
/// Compiler-generated `<T as FnPtr>::as_ptr` implementation.
168168
///
169169
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
170170
///
171171
/// The `DefId` is for `FnPtr::addr`, the `Ty` is the type `T`.
172-
FnPtrAddrShim(DefId, Ty<'tcx>),
172+
FnPtrAsPtrShim(DefId, Ty<'tcx>),
173+
174+
/// Compiler-generated `<T as FnPtr>::from_ptr` implementation.
175+
///
176+
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
177+
///
178+
/// The `DefId` is for `FnPtr::from_ptr`, the `Ty` is the type `T`.
179+
FnPtrFromPtrShim(DefId, Ty<'tcx>),
173180

174181
/// `core::future::async_drop::async_drop_in_place::<'_, T>`.
175182
///
@@ -257,7 +264,8 @@ impl<'tcx> InstanceKind<'tcx> {
257264
}
258265
| InstanceKind::DropGlue(def_id, _)
259266
| InstanceKind::CloneShim(def_id, _)
260-
| InstanceKind::FnPtrAddrShim(def_id, _)
267+
| InstanceKind::FnPtrAsPtrShim(def_id, _)
268+
| InstanceKind::FnPtrFromPtrShim(def_id, _)
261269
| InstanceKind::FutureDropPollShim(def_id, _, _)
262270
| InstanceKind::AsyncDropGlue(def_id, _)
263271
| InstanceKind::AsyncDropGlueCtorShim(def_id, _) => def_id,
@@ -282,7 +290,8 @@ impl<'tcx> InstanceKind<'tcx> {
282290
| ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
283291
| InstanceKind::DropGlue(..)
284292
| InstanceKind::CloneShim(..)
285-
| InstanceKind::FnPtrAddrShim(..) => None,
293+
| InstanceKind::FnPtrAsPtrShim(..)
294+
| InstanceKind::FnPtrFromPtrShim(..) => None,
286295
}
287296
}
288297

@@ -332,7 +341,8 @@ impl<'tcx> InstanceKind<'tcx> {
332341
match *self {
333342
InstanceKind::CloneShim(..)
334343
| InstanceKind::ThreadLocalShim(..)
335-
| InstanceKind::FnPtrAddrShim(..)
344+
| InstanceKind::FnPtrAsPtrShim(..)
345+
| InstanceKind::FnPtrFromPtrShim(..)
336346
| InstanceKind::FnPtrShim(..)
337347
| InstanceKind::DropGlue(_, Some(_))
338348
| InstanceKind::FutureDropPollShim(..)

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,8 @@ impl<'tcx> TyCtxt<'tcx> {
18131813
| ty::InstanceKind::DropGlue(..)
18141814
| ty::InstanceKind::CloneShim(..)
18151815
| ty::InstanceKind::ThreadLocalShim(..)
1816-
| ty::InstanceKind::FnPtrAddrShim(..)
1816+
| ty::InstanceKind::FnPtrAsPtrShim(..)
1817+
| ty::InstanceKind::FnPtrFromPtrShim(..)
18171818
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
18181819
| ty::InstanceKind::AsyncDropGlue(..) => self.mir_shims(instance),
18191820
};

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)