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 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/// Returns true when the variant represented by the base `TypeId` and variant_idx is non
122/// exhaustive.
123///
124/// The more user-friendly version of this intrinsic is
125/// [`core::mem::type_info::VariantId::non_exhaustive`].
126///
127/// [`TypeId`]: crate::any::TypeId
128#[rustc_intrinsic]
129#[rustc_comptime]
130pub fn type_id_variant_non_exhaustive(base: crate::any::TypeId, variant: usize) -> bool;
131
132/// Gets the number of fields at the given `variant_index` represented by this `TypeId`.
133///
134/// The more user-friendly version of this intrinsic is [`core::any::TypeId::fields`].
135#[rustc_intrinsic]
136#[rustc_comptime]
137pub fn type_id_fields(_id: crate::any::TypeId, _variant_index: usize) -> usize;
138
139/// Checks whether this type is non-exhaustive.
140#[rustc_intrinsic]
141#[rustc_comptime]
142pub fn type_id_non_exhaustive(_id: crate::any::TypeId) -> bool;
143
144/// Returns the list of generic args on this type.
145/// Only meaningful for Adts, closures, ... Everything else returns an empty slice.
146#[rustc_intrinsic]
147#[rustc_comptime]
148pub fn type_id_generics(_id: crate::any::TypeId) -> &'static [crate::mem::type_info::Generic];
149
150/// Gets the [`FieldRepresentingType`]'s `TypeId` at the given index of the type represented by this `TypeId`.
151///
152/// The more user-friendly version of this intrinsic is [`core::any::TypeId::field`].
153///
154/// [`FieldRepresentingType`]: crate::field::FieldRepresentingType
155#[rustc_intrinsic]
156#[rustc_comptime]
157pub fn type_id_field_representing_type(
158    _id: crate::any::TypeId,
159    _variant_index: usize,
160    _field_index: usize,
161) -> crate::any::TypeId;
162
163/// Given a `TypeId` that represents a pointer this returns the `TypeId` which that pointer
164/// points to. When called on anything else this returns None.
165///
166/// The more user-friendly version of this intrinsic is [`core::any::TypeId::points_to`].
167#[rustc_intrinsic]
168#[rustc_comptime]
169pub fn type_id_points_to(_id: crate::any::TypeId) -> Option<crate::any::TypeId>;
170
171/// Given a `TypeId` that represents a pointer returns whether that pointer is mutable.
172/// When called on anything else this returns `false`.
173///
174/// The more user-friendly version of this intrinsic is [`core::any::TypeId::points_mutably`].
175#[rustc_intrinsic]
176#[rustc_comptime]
177pub fn type_id_points_mutably(_id: crate::any::TypeId) -> bool;
178
179/// Given a `TypeId` that represents a function pointer returns an [`core::mem::type_info::FnPtr`].
180/// When called on something else this returns `None`.
181///
182/// The more user-friendly version of this intrinsic is [`core::any::TypeId::function_ptr`].
183#[rustc_intrinsic]
184#[rustc_comptime]
185pub fn type_id_function_ptr(_type_id: crate::any::TypeId) -> Option<crate::mem::type_info::FnPtr>;
186
187/// Returns the reflection attributes given a `TypeId` which represents a structure, enumeration, union or `FieldId`.
188///
189/// The more user-friendly version of this intrinsic is [`core::any::TypeId::reflected_attrs`].
190#[rustc_intrinsic]
191#[rustc_comptime]
192pub fn type_id_reflected_attrs(_type_id: crate::any::TypeId) -> &'static [&'static str];