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::TypeKind;
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 the reflection attributes given a the variant represented by the base `TypeId` and variant_idx.
112///
113/// The more user-friendly version of this intrinsic is [`core::mem::type_info::VariantId::reflected_attrs`].
114#[rustc_intrinsic]
115#[rustc_comptime]
116pub fn type_id_variant_reflected_attrs(
117    _base: crate::any::TypeId,
118    _variant_index: usize,
119) -> &'static [&'static str];
120
121/// Gets the raw documentation attributes of the variant represented by the base `TypeId` and variant_idx.
122///
123/// The more user-friendly version of this intrinsic is [`core::mem::type_info::VariantId::docs`].
124///
125/// [`TypeId`]: crate::any::TypeId
126#[rustc_intrinsic]
127#[unstable(feature = "core_intrinsics", issue = "none")]
128#[rustc_comptime]
129pub fn type_id_variant_docs(
130    _base: crate::any::TypeId,
131    _variant_index: usize,
132) -> &'static [&'static str];
133
134/// Returns true when the variant represented by the base `TypeId` and variant_idx is non
135/// exhaustive.
136///
137/// The more user-friendly version of this intrinsic is
138/// [`core::mem::type_info::VariantId::non_exhaustive`].
139///
140/// [`TypeId`]: crate::any::TypeId
141#[rustc_intrinsic]
142#[rustc_comptime]
143pub fn type_id_variant_non_exhaustive(base: crate::any::TypeId, variant: usize) -> bool;
144
145/// Gets the number of fields at the given `variant_index` represented by this `TypeId`.
146///
147/// The more user-friendly version of this intrinsic is [`core::any::TypeId::fields`].
148#[rustc_intrinsic]
149#[rustc_comptime]
150pub fn type_id_fields(_id: crate::any::TypeId, _variant_index: usize) -> usize;
151
152/// Checks whether this type is non-exhaustive.
153#[rustc_intrinsic]
154#[rustc_comptime]
155pub fn type_id_non_exhaustive(_id: crate::any::TypeId) -> bool;
156
157/// Returns the list of generic args on this type.
158/// Only meaningful for Adts, closures, ... Everything else returns an empty slice.
159#[rustc_intrinsic]
160#[rustc_comptime]
161pub fn type_id_generics(_id: crate::any::TypeId) -> &'static [crate::mem::type_info::Generic];
162
163/// Gets the [`FieldRepresentingType`]'s `TypeId` at the given index of the type represented by this `TypeId`.
164///
165/// The more user-friendly version of this intrinsic is [`core::any::TypeId::field`].
166///
167/// [`FieldRepresentingType`]: crate::field::FieldRepresentingType
168#[rustc_intrinsic]
169#[rustc_comptime]
170pub fn type_id_field_representing_type(
171    _id: crate::any::TypeId,
172    _variant_index: usize,
173    _field_index: usize,
174) -> crate::any::TypeId;
175
176/// Given a `TypeId` that represents a pointer this returns the `TypeId` which that pointer
177/// points to. When called on anything else this returns None.
178///
179/// The more user-friendly version of this intrinsic is [`core::any::TypeId::points_to`].
180#[rustc_intrinsic]
181#[rustc_comptime]
182pub fn type_id_points_to(_id: crate::any::TypeId) -> Option<crate::any::TypeId>;
183
184/// Given a `TypeId` that represents a pointer returns whether that pointer is mutable.
185/// When called on anything else this returns `false`.
186///
187/// The more user-friendly version of this intrinsic is [`core::any::TypeId::points_mutably`].
188#[rustc_intrinsic]
189#[rustc_comptime]
190pub fn type_id_points_mutably(_id: crate::any::TypeId) -> bool;
191
192/// Given a `TypeId` that represents a dyn trait returns an [`core::mem::type_info::DynTrait`]. When called on
193/// something else this returns `None`.
194///
195/// The more user-friendly version of this intrinsic is [`core::any::TypeId::dyn_trait`].
196#[rustc_intrinsic]
197#[rustc_comptime]
198pub fn type_id_dyn_trait(_type_id: crate::any::TypeId) -> Option<crate::mem::type_info::DynTrait>;
199
200/// Given a `TypeId` that represents a function pointer returns an [`core::mem::type_info::FnPtr`].
201/// When called on something else this returns `None`.
202///
203/// The more user-friendly version of this intrinsic is [`core::any::TypeId::function_ptr`].
204#[rustc_intrinsic]
205#[rustc_comptime]
206pub fn type_id_function_ptr(_type_id: crate::any::TypeId) -> Option<crate::mem::type_info::FnPtr>;
207
208/// Returns the reflection attributes given a `TypeId` which represents a structure, enumeration, union or `FieldId`.
209///
210/// The more user-friendly version of this intrinsic is [`core::any::TypeId::reflected_attrs`].
211#[rustc_intrinsic]
212#[rustc_comptime]
213pub fn type_id_reflected_attrs(_type_id: crate::any::TypeId) -> &'static [&'static str];
214
215/// Returns the raw documentation attributes given a `TypeId` which represents a structure, enumeration, union or `FieldId`.
216///
217/// The more user-friendly version of this intrinsic is [`core::any::TypeId::docs`].
218#[rustc_intrinsic]
219#[unstable(feature = "core_intrinsics", issue = "none")]
220#[rustc_comptime]
221pub fn type_id_docs(_type_id: crate::any::TypeId) -> &'static [&'static str];