Skip to content

Commit 293714d

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 cb40c25 commit 293714d

36 files changed

Lines changed: 241 additions & 95 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
@@ -656,7 +656,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
656656
| ty::InstanceKind::FnPtrShim(..)
657657
| ty::InstanceKind::DropGlue(..)
658658
| ty::InstanceKind::CloneShim(..)
659-
| ty::InstanceKind::FnPtrAddrShim(..)
659+
| ty::InstanceKind::FnPtrAsPtrShim(..)
660+
| ty::InstanceKind::FnPtrFromPtrShim(..)
660661
| ty::InstanceKind::ThreadLocalShim(..)
661662
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
662663
| 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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ pub enum InstanceKind<'tcx> {
167167
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
168168
///
169169
/// The `DefId` is for `FnPtr::addr`, the `Ty` is the type `T`.
170-
FnPtrAddrShim(DefId, Ty<'tcx>),
170+
FnPtrAsPtrShim(DefId, Ty<'tcx>),
171+
172+
/// Compiler-generated `<T as FnPtr>::from_ptr` implementation.
173+
///
174+
/// Automatically generated for all potentially higher-ranked `fn(I) -> R` types.
175+
///
176+
/// The `DefId` is for `FnPtr::from_ptr`, the `Ty` is the type `T`.
177+
FnPtrFromPtrShim(DefId, Ty<'tcx>),
171178

172179
/// `core::future::async_drop::async_drop_in_place::<'_, T>`.
173180
///
@@ -255,7 +262,8 @@ impl<'tcx> InstanceKind<'tcx> {
255262
}
256263
| InstanceKind::DropGlue(def_id, _)
257264
| InstanceKind::CloneShim(def_id, _)
258-
| InstanceKind::FnPtrAddrShim(def_id, _)
265+
| InstanceKind::FnPtrAsPtrShim(def_id, _)
266+
| InstanceKind::FnPtrFromPtrShim(def_id, _)
259267
| InstanceKind::FutureDropPollShim(def_id, _, _)
260268
| InstanceKind::AsyncDropGlue(def_id, _)
261269
| InstanceKind::AsyncDropGlueCtorShim(def_id, _) => def_id,
@@ -280,7 +288,8 @@ impl<'tcx> InstanceKind<'tcx> {
280288
| ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
281289
| InstanceKind::DropGlue(..)
282290
| InstanceKind::CloneShim(..)
283-
| InstanceKind::FnPtrAddrShim(..) => None,
291+
| InstanceKind::FnPtrAsPtrShim(..)
292+
| InstanceKind::FnPtrFromPtrShim(..) => None,
284293
}
285294
}
286295

@@ -326,7 +335,8 @@ impl<'tcx> InstanceKind<'tcx> {
326335
match *self {
327336
InstanceKind::CloneShim(..)
328337
| InstanceKind::ThreadLocalShim(..)
329-
| InstanceKind::FnPtrAddrShim(..)
338+
| InstanceKind::FnPtrAsPtrShim(..)
339+
| InstanceKind::FnPtrFromPtrShim(..)
330340
| InstanceKind::FnPtrShim(..)
331341
| InstanceKind::DropGlue(_, Some(_))
332342
| InstanceKind::FutureDropPollShim(..)

compiler/rustc_middle/src/ty/mod.rs

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

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)