Skip to main content

core/intrinsics/
reflection.rs

1//! Intrinsics for compile time reflection
2//!
3//! This are all methods operating on [`TypeId`](core::any::TypeId) which is the
4//! entry point to all compile time reflection.
5
6use crate::ptr;
7
8/// Gets an identifier which is globally unique to the specified type. This
9/// function will return the same value for a type regardless of whichever
10/// crate it is invoked in.
11///
12/// Note that, unlike most intrinsics, this can only be called at compile-time
13/// as backends do not have an implementation for it. The only caller (its
14/// stable counterpart) wraps this intrinsic call in a `const` block so that
15/// backends only see an evaluated constant.
16///
17/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
18#[rustc_nounwind]
19#[rustc_intrinsic]
20#[rustc_comptime]
21pub fn type_id<T: ?Sized>() -> crate::any::TypeId;
22
23/// Compute the type information of a concrete type.
24/// It can only be called at compile time, the backends do
25/// not implement it.
26#[rustc_intrinsic]
27#[rustc_comptime]
28pub fn type_id_type_of(_id: crate::any::TypeId) -> crate::mem::type_info::Type;
29
30/// Tests (at compile-time) if two [`crate::any::TypeId`] instances identify the
31/// same type. This is necessary because at const-eval time the actual discriminating
32/// data is opaque and cannot be inspected directly.
33///
34/// The stabilized version of this intrinsic is the [PartialEq] impl for [`core::any::TypeId`].
35#[rustc_nounwind]
36#[rustc_intrinsic]
37#[rustc_do_not_const_check]
38pub const fn type_id_eq(a: crate::any::TypeId, b: crate::any::TypeId) -> bool {
39    // SAFETY: we know `TypeId` is 16 bytes of initialized data.
40    // This is runtime-only code so we do not have to worry about provenance.
41    unsafe { crate::mem::transmute::<_, u128>(a) == crate::mem::transmute::<_, u128>(b) }
42}
43
44#[rustc_intrinsic]
45#[rustc_comptime]
46/// Check if a type represented by a `TypeId` implements a trait represented by a `TypeId`.
47/// It can only be called at compile time, the backends do
48/// not implement it. If it implements the trait the dyn metadata gets returned for vtable access.
49pub fn type_id_vtable(
50    _id: crate::any::TypeId,
51    _trait: crate::any::TypeId,
52) -> Option<ptr::DynMetadata<*const ()>>;
53
54/// Gets a static string slice containing the name of a type.
55///
56/// Note that, unlike most intrinsics, this can only be called at compile-time
57/// as backends do not have an implementation for it. The only caller (its
58/// stable counterpart) wraps this intrinsic call in a `const` block so that
59/// backends only see an evaluated constant.
60///
61/// The stabilized version of this intrinsic is [`core::any::type_name`].
62#[rustc_nounwind]
63#[rustc_intrinsic]
64#[rustc_comptime]
65pub fn type_name<T: ?Sized>() -> &'static str;
66
67/// Returns whether the type represented by this `TypeId` is a signed integer.
68///
69/// The more user-friendly version of this intrinsic is [`core::any::TypeId::is_signed`].
70#[rustc_intrinsic]
71#[rustc_comptime]
72pub fn type_id_is_signed(_id: crate::any::TypeId) -> bool;
73
74/// Gets the length of the array represented by this `TypeId`.
75///
76/// The more user-friendly version of this intrinsic is [`core::any::TypeId::array_len`].
77#[rustc_intrinsic]
78#[rustc_comptime]
79pub fn type_id_array_len(_id: crate::any::TypeId) -> usize;
80
81/// Gets the type of each element of the array or slice represented by this `TypeId`.
82///
83/// The more user-friendly version of this intrinsic is [`core::any::TypeId::element_ty`].
84#[rustc_intrinsic]
85#[rustc_comptime]
86pub fn type_id_element_ty(_id: crate::any::TypeId) -> Option<crate::any::TypeId>;
87
88/// Gets the size of the type represented by this `TypeId`.
89///
90/// The more user-friendly version of this intrinsic is [`core::any::TypeId::size`].
91#[rustc_intrinsic]
92#[rustc_comptime]
93pub fn type_id_size_of(_id: crate::any::TypeId) -> Option<usize>;
94
95/// Gets the number of variants of the type represented by this `TypeId`.
96///
97/// The more user-friendly version of this intrinsic is [`core::any::TypeId::variants`].
98#[rustc_intrinsic]
99#[rustc_comptime]
100pub fn type_id_variants(_id: crate::any::TypeId) -> usize;
101
102/// Gets the name of the variant represented by the base `TypeId` and variant_idx.
103///
104/// The more user-friendly version of this intrinsic is [`core::mem::type_info::VariantId::name`].
105///
106/// [`TypeId`]: crate::any::TypeId
107#[rustc_intrinsic]
108#[rustc_comptime]
109pub fn type_id_variant_name(_base: crate::any::TypeId, _variant_index: usize) -> &'static str;
110
111/// Returns true when the variant represented by the base `TypeId` and variant_idx is non
112/// exhaustive.
113///
114/// The more user-friendly version of this intrinsic is
115/// [`core::mem::type_info::VariantId::non_exhaustive`].
116///
117/// [`TypeId`]: crate::any::TypeId
118#[rustc_intrinsic]
119#[rustc_comptime]
120pub fn type_id_variant_non_exhaustive(base: crate::any::TypeId, variant: usize) -> bool;
121
122/// Gets the number of fields at the given `variant_index` represented by this `TypeId`.
123///
124/// The more user-friendly version of this intrinsic is [`core::any::TypeId::fields`].
125#[rustc_intrinsic]
126#[rustc_comptime]
127pub fn type_id_fields(_id: crate::any::TypeId, _variant_index: usize) -> usize;
128
129/// Checks whether this type is non-exhaustive.
130#[rustc_intrinsic]
131#[rustc_comptime]
132pub fn type_id_non_exhaustive(_id: crate::any::TypeId) -> bool;
133
134/// Returns the list of generic args on this type.
135/// Only meaningful for Adts, closures, ... Everything else returns an empty slice.
136#[rustc_intrinsic]
137#[rustc_comptime]
138pub fn type_id_generics(_id: crate::any::TypeId) -> &'static [crate::mem::type_info::Generic];
139
140/// Gets the [`FieldRepresentingType`]'s `TypeId` at the given index of the type represented by this `TypeId`.
141///
142/// The more user-friendly version of this intrinsic is [`core::any::TypeId::field`].
143///
144/// [`FieldRepresentingType`]: crate::field::FieldRepresentingType
145#[rustc_intrinsic]
146#[rustc_comptime]
147pub fn type_id_field_representing_type(
148    _id: crate::any::TypeId,
149    _variant_index: usize,
150    _field_index: usize,
151) -> crate::any::TypeId;
152
153/// Given a `TypeId` that represents a pointer this returns the `TypeId` which that pointer
154/// points to. When called on anything else this returns None.
155///
156/// The more user-friendly version of this intrinsic is [`core::any::TypeId::points_to`].
157#[rustc_intrinsic]
158#[rustc_comptime]
159pub fn type_id_points_to(_id: crate::any::TypeId) -> Option<crate::any::TypeId>;
160
161/// Given a `TypeId` that represents a pointer returns whether that pointer is mutable.
162/// When called on anything else this returns `false`.
163///
164/// The more user-friendly version of this intrinsic is [`core::any::TypeId::points_mutably`].
165#[rustc_intrinsic]
166#[rustc_comptime]
167pub fn type_id_points_mutably(_id: crate::any::TypeId) -> bool;
168
169/// Given a `TypeId` that represents a function pointer returns an [`core::mem::type_info::FnPtr`].
170/// When called on something else this returns `None`.
171///
172/// The more user-friendly version of this intrinsic is [`core::any::TypeId::function_ptr`].
173#[rustc_intrinsic]
174#[rustc_comptime]
175pub fn type_id_function_ptr(_type_id: crate::any::TypeId) -> Option<crate::mem::type_info::FnPtr>;