rustc_middle/ty/
consts.rs

1use std::borrow::Cow;
2
3use rustc_data_structures::intern::Interned;
4use rustc_error_messages::MultiSpan;
5use rustc_macros::HashStable;
6use rustc_type_ir::walk::TypeWalker;
7use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
8
9use crate::ty::{self, Ty, TyCtxt};
10
11mod int;
12mod kind;
13mod valtree;
14
15pub use int::*;
16pub use kind::*;
17use rustc_span::{DUMMY_SP, ErrorGuaranteed};
18pub use valtree::*;
19
20pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
21pub type UnevaluatedConst<'tcx> = ir::UnevaluatedConst<TyCtxt<'tcx>>;
22
23#[cfg(target_pointer_width = "64")]
24rustc_data_structures::static_assert_size!(ConstKind<'_>, 24);
25
26#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
27#[rustc_pass_by_value]
28pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstKind<'tcx>>>);
29
30impl<'tcx> rustc_type_ir::inherent::IntoKind for Const<'tcx> {
31    type Kind = ConstKind<'tcx>;
32
33    fn kind(self) -> ConstKind<'tcx> {
34        self.kind()
35    }
36}
37
38impl<'tcx> rustc_type_ir::Flags for Const<'tcx> {
39    fn flags(&self) -> TypeFlags {
40        self.0.flags
41    }
42
43    fn outer_exclusive_binder(&self) -> rustc_type_ir::DebruijnIndex {
44        self.0.outer_exclusive_binder
45    }
46}
47
48impl<'tcx> Const<'tcx> {
49    #[inline]
50    pub fn kind(self) -> ConstKind<'tcx> {
51        let a: &ConstKind<'tcx> = self.0.0;
52        *a
53    }
54
55    // FIXME(compiler-errors): Think about removing this.
56    #[inline]
57    pub fn flags(self) -> TypeFlags {
58        self.0.flags
59    }
60
61    // FIXME(compiler-errors): Think about removing this.
62    #[inline]
63    pub fn outer_exclusive_binder(self) -> ty::DebruijnIndex {
64        self.0.outer_exclusive_binder
65    }
66
67    #[inline]
68    pub fn new(tcx: TyCtxt<'tcx>, kind: ty::ConstKind<'tcx>) -> Const<'tcx> {
69        tcx.mk_ct_from_kind(kind)
70    }
71
72    #[inline]
73    pub fn new_param(tcx: TyCtxt<'tcx>, param: ty::ParamConst) -> Const<'tcx> {
74        Const::new(tcx, ty::ConstKind::Param(param))
75    }
76
77    #[inline]
78    pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid) -> Const<'tcx> {
79        Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)))
80    }
81
82    #[inline]
83    pub fn new_fresh(tcx: TyCtxt<'tcx>, fresh: u32) -> Const<'tcx> {
84        Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Fresh(fresh)))
85    }
86
87    #[inline]
88    pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst) -> Const<'tcx> {
89        Const::new(tcx, ty::ConstKind::Infer(infer))
90    }
91
92    #[inline]
93    pub fn new_bound(
94        tcx: TyCtxt<'tcx>,
95        debruijn: ty::DebruijnIndex,
96        var: ty::BoundVar,
97    ) -> Const<'tcx> {
98        Const::new(tcx, ty::ConstKind::Bound(debruijn, var))
99    }
100
101    #[inline]
102    pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Const<'tcx> {
103        Const::new(tcx, ty::ConstKind::Placeholder(placeholder))
104    }
105
106    #[inline]
107    pub fn new_unevaluated(tcx: TyCtxt<'tcx>, uv: ty::UnevaluatedConst<'tcx>) -> Const<'tcx> {
108        tcx.debug_assert_args_compatible(uv.def, uv.args);
109        Const::new(tcx, ty::ConstKind::Unevaluated(uv))
110    }
111
112    #[inline]
113    pub fn new_value(tcx: TyCtxt<'tcx>, valtree: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
114        Const::new(tcx, ty::ConstKind::Value(ty::Value { ty, valtree }))
115    }
116
117    #[inline]
118    pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Const<'tcx> {
119        Const::new(tcx, ty::ConstKind::Expr(expr))
120    }
121
122    #[inline]
123    pub fn new_error(tcx: TyCtxt<'tcx>, e: ty::ErrorGuaranteed) -> Const<'tcx> {
124        Const::new(tcx, ty::ConstKind::Error(e))
125    }
126
127    /// Like [Ty::new_error] but for constants.
128    #[track_caller]
129    pub fn new_misc_error(tcx: TyCtxt<'tcx>) -> Const<'tcx> {
130        Const::new_error_with_message(
131            tcx,
132            DUMMY_SP,
133            "ty::ConstKind::Error constructed but no error reported",
134        )
135    }
136
137    /// Like [Ty::new_error_with_message] but for constants.
138    #[track_caller]
139    pub fn new_error_with_message<S: Into<MultiSpan>>(
140        tcx: TyCtxt<'tcx>,
141        span: S,
142        msg: impl Into<Cow<'static, str>>,
143    ) -> Const<'tcx> {
144        let reported = tcx.dcx().span_delayed_bug(span, msg);
145        Const::new_error(tcx, reported)
146    }
147}
148
149impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
150    fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst) -> Self {
151        Const::new_infer(tcx, infer)
152    }
153
154    fn new_var(tcx: TyCtxt<'tcx>, vid: ty::ConstVid) -> Self {
155        Const::new_var(tcx, vid)
156    }
157
158    fn new_bound(interner: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
159        Const::new_bound(interner, debruijn, var)
160    }
161
162    fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
163        Const::new_bound(tcx, debruijn, var)
164    }
165
166    fn new_unevaluated(interner: TyCtxt<'tcx>, uv: ty::UnevaluatedConst<'tcx>) -> Self {
167        Const::new_unevaluated(interner, uv)
168    }
169
170    fn new_expr(interner: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Self {
171        Const::new_expr(interner, expr)
172    }
173
174    fn new_error(interner: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
175        Const::new_error(interner, guar)
176    }
177}
178
179impl<'tcx> Const<'tcx> {
180    /// Creates a constant with the given integer value and interns it.
181    #[inline]
182    pub fn from_bits(
183        tcx: TyCtxt<'tcx>,
184        bits: u128,
185        typing_env: ty::TypingEnv<'tcx>,
186        ty: Ty<'tcx>,
187    ) -> Self {
188        let size = tcx
189            .layout_of(typing_env.as_query_input(ty))
190            .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}"))
191            .size;
192        ty::Const::new_value(
193            tcx,
194            ty::ValTree::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap()),
195            ty,
196        )
197    }
198
199    #[inline]
200    /// Creates an interned zst constant.
201    pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
202        ty::Const::new_value(tcx, ty::ValTree::zst(tcx), ty)
203    }
204
205    #[inline]
206    /// Creates an interned bool constant.
207    pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
208        Self::from_bits(tcx, v as u128, ty::TypingEnv::fully_monomorphized(), tcx.types.bool)
209    }
210
211    #[inline]
212    /// Creates an interned usize constant.
213    pub fn from_target_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
214        Self::from_bits(tcx, n as u128, ty::TypingEnv::fully_monomorphized(), tcx.types.usize)
215    }
216
217    /// Panics if `self.kind != ty::ConstKind::Value`.
218    pub fn to_value(self) -> ty::Value<'tcx> {
219        match self.kind() {
220            ty::ConstKind::Value(cv) => cv,
221            _ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
222        }
223    }
224
225    /// Attempts to convert to a value.
226    ///
227    /// Note that this does not evaluate the constant.
228    pub fn try_to_value(self) -> Option<ty::Value<'tcx>> {
229        match self.kind() {
230            ty::ConstKind::Value(cv) => Some(cv),
231            _ => None,
232        }
233    }
234
235    /// Convenience method to extract the value of a usize constant,
236    /// useful to get the length of an array type.
237    ///
238    /// Note that this does not evaluate the constant.
239    #[inline]
240    pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
241        self.try_to_value()?.try_to_target_usize(tcx)
242    }
243
244    pub fn is_ct_infer(self) -> bool {
245        matches!(self.kind(), ty::ConstKind::Infer(_))
246    }
247
248    /// Iterator that walks `self` and any types reachable from
249    /// `self`, in depth-first order. Note that just walks the types
250    /// that appear in `self`, it does not descend into the fields of
251    /// structs or variants. For example:
252    ///
253    /// ```text
254    /// isize => { isize }
255    /// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
256    /// [isize] => { [isize], isize }
257    /// ```
258    pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
259        TypeWalker::new(self.into())
260    }
261}