rustc_middle/mir/
consts.rs

1use std::fmt::{self, Debug, Display, Formatter};
2
3use rustc_abi::{HasDataLayout, Size};
4use rustc_hir::def_id::DefId;
5use rustc_macros::{HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
6use rustc_span::{DUMMY_SP, RemapPathScopeComponents, Span, Symbol};
7use rustc_type_ir::TypeVisitableExt;
8
9use super::interpret::ReportedErrorInfo;
10use crate::mir::interpret::{AllocId, AllocRange, ErrorHandled, GlobalAlloc, Scalar, alloc_range};
11use crate::mir::{Promoted, pretty_print_const_value};
12use crate::ty::print::{pretty_print_const, with_no_trimmed_paths};
13use crate::ty::{self, ConstKind, GenericArgsRef, ScalarInt, Ty, TyCtxt};
14
15///////////////////////////////////////////////////////////////////////////
16/// Evaluated Constants
17///
18/// Represents the result of const evaluation via the `eval_to_allocation` query.
19/// Not to be confused with `ConstAllocation`, which directly refers to the underlying data!
20/// Here we indirect via an `AllocId`.
21#[derive(Copy, Clone, HashStable, TyEncodable, TyDecodable, Debug, Hash, Eq, PartialEq)]
22pub struct ConstAlloc<'tcx> {
23    /// The value lives here, at offset 0, and that allocation definitely is an `AllocKind::Memory`
24    /// (so you can use `AllocMap::unwrap_memory`).
25    pub alloc_id: AllocId,
26    pub ty: Ty<'tcx>,
27}
28
29/// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
30/// array length computations, enum discriminants and the pattern matching logic.
31#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
32#[derive(HashStable)]
33pub enum ConstValue {
34    /// Used for types with `layout::abi::Scalar` ABI.
35    ///
36    /// Not using the enum `Value` to encode that this must not be `Uninit`.
37    Scalar(Scalar),
38
39    /// Only for ZSTs.
40    ZeroSized,
41
42    /// Used for references to unsized types with slice tail.
43    ///
44    /// This is worth an optimized representation since Rust has literals of type `&str` and
45    /// `&[u8]`. Not having to indirect those through an `AllocId` (or two, if we used `Indirect`)
46    /// has shown measurable performance improvements on stress tests. We then reuse this
47    /// optimization for slice-tail types more generally during valtree-to-constval conversion.
48    Slice {
49        /// The allocation storing the slice contents.
50        /// This always points to the beginning of the allocation.
51        alloc_id: AllocId,
52        /// The metadata field of the reference.
53        /// This is a "target usize", so we use `u64` as in the interpreter.
54        meta: u64,
55    },
56
57    /// A value not representable by the other variants; needs to be stored in-memory.
58    ///
59    /// Must *not* be used for scalars or ZST, but having `&str` or other slices in this variant is fine.
60    Indirect {
61        /// The backing memory of the value. May contain more memory than needed for just the value
62        /// if this points into some other larger ConstValue.
63        ///
64        /// We use an `AllocId` here instead of a `ConstAllocation<'tcx>` to make sure that when a
65        /// raw constant (which is basically just an `AllocId`) is turned into a `ConstValue` and
66        /// back, we can preserve the original `AllocId`.
67        alloc_id: AllocId,
68        /// Offset into `alloc`
69        offset: Size,
70    },
71}
72
73#[cfg(target_pointer_width = "64")]
74rustc_data_structures::static_assert_size!(ConstValue, 24);
75
76impl ConstValue {
77    #[inline]
78    pub fn try_to_scalar(&self) -> Option<Scalar> {
79        match *self {
80            ConstValue::Indirect { .. } | ConstValue::Slice { .. } | ConstValue::ZeroSized => None,
81            ConstValue::Scalar(val) => Some(val),
82        }
83    }
84
85    pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
86        self.try_to_scalar()?.try_to_scalar_int().ok()
87    }
88
89    pub fn try_to_bits(&self, size: Size) -> Option<u128> {
90        Some(self.try_to_scalar_int()?.to_bits(size))
91    }
92
93    pub fn try_to_bool(&self) -> Option<bool> {
94        self.try_to_scalar_int()?.try_into().ok()
95    }
96
97    pub fn try_to_target_usize(&self, tcx: TyCtxt<'_>) -> Option<u64> {
98        Some(self.try_to_scalar_int()?.to_target_usize(tcx))
99    }
100
101    pub fn try_to_bits_for_ty<'tcx>(
102        &self,
103        tcx: TyCtxt<'tcx>,
104        typing_env: ty::TypingEnv<'tcx>,
105        ty: Ty<'tcx>,
106    ) -> Option<u128> {
107        let size = tcx
108            .layout_of(typing_env.with_post_analysis_normalized(tcx).as_query_input(ty))
109            .ok()?
110            .size;
111        self.try_to_bits(size)
112    }
113
114    pub fn from_bool(b: bool) -> Self {
115        ConstValue::Scalar(Scalar::from_bool(b))
116    }
117
118    pub fn from_u64(i: u64) -> Self {
119        ConstValue::Scalar(Scalar::from_u64(i))
120    }
121
122    pub fn from_u128(i: u128) -> Self {
123        ConstValue::Scalar(Scalar::from_u128(i))
124    }
125
126    pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
127        ConstValue::Scalar(Scalar::from_target_usize(i, cx))
128    }
129
130    /// Must only be called on constants of type `&str` or `&[u8]`!
131    pub fn try_get_slice_bytes_for_diagnostics<'tcx>(
132        &self,
133        tcx: TyCtxt<'tcx>,
134    ) -> Option<&'tcx [u8]> {
135        let (alloc_id, start, len) = match self {
136            ConstValue::Scalar(_) | ConstValue::ZeroSized => {
137                bug!("`try_get_slice_bytes` on non-slice constant")
138            }
139            &ConstValue::Slice { alloc_id, meta } => (alloc_id, 0, meta),
140            &ConstValue::Indirect { alloc_id, offset } => {
141                // The reference itself is stored behind an indirection.
142                // Load the reference, and then load the actual slice contents.
143                let a = tcx.global_alloc(alloc_id).unwrap_memory().inner();
144                let ptr_size = tcx.data_layout.pointer_size();
145                if a.size() < offset + 2 * ptr_size {
146                    // (partially) dangling reference
147                    return None;
148                }
149                // Read the wide pointer components.
150                let ptr = a
151                    .read_scalar(
152                        &tcx,
153                        alloc_range(offset, ptr_size),
154                        /* read_provenance */ true,
155                    )
156                    .ok()?;
157                let ptr = ptr.to_pointer(&tcx).discard_err()?;
158                let len = a
159                    .read_scalar(
160                        &tcx,
161                        alloc_range(offset + ptr_size, ptr_size),
162                        /* read_provenance */ false,
163                    )
164                    .ok()?;
165                let len = len.to_target_usize(&tcx).discard_err()?;
166                if len == 0 {
167                    return Some(&[]);
168                }
169                // Non-empty slice, must have memory. We know this is a relative pointer.
170                let (inner_prov, offset) =
171                    ptr.into_pointer_or_addr().ok()?.prov_and_relative_offset();
172                (inner_prov.alloc_id(), offset.bytes(), len)
173            }
174        };
175
176        let data = tcx.global_alloc(alloc_id).unwrap_memory();
177
178        // This is for diagnostics only, so we are okay to use `inspect_with_uninit_and_ptr_outside_interpreter`.
179        let start = start.try_into().unwrap();
180        let end = start + usize::try_from(len).unwrap();
181        Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end))
182    }
183
184    /// Check if a constant may contain provenance information. This is used by MIR opts.
185    /// Can return `true` even if there is no provenance.
186    pub fn may_have_provenance(&self, tcx: TyCtxt<'_>, size: Size) -> bool {
187        match *self {
188            ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false,
189            ConstValue::Scalar(Scalar::Ptr(..)) => return true,
190            // It's hard to find out the part of the allocation we point to;
191            // just conservatively check everything.
192            ConstValue::Slice { alloc_id, meta: _ } => {
193                !tcx.global_alloc(alloc_id).unwrap_memory().inner().provenance().ptrs().is_empty()
194            }
195            ConstValue::Indirect { alloc_id, offset } => !tcx
196                .global_alloc(alloc_id)
197                .unwrap_memory()
198                .inner()
199                .provenance()
200                .range_empty(AllocRange::from(offset..offset + size), &tcx),
201        }
202    }
203
204    /// Check if a constant only contains uninitialized bytes.
205    pub fn all_bytes_uninit(&self, tcx: TyCtxt<'_>) -> bool {
206        let ConstValue::Indirect { alloc_id, .. } = self else {
207            return false;
208        };
209        let alloc = tcx.global_alloc(*alloc_id);
210        let GlobalAlloc::Memory(alloc) = alloc else {
211            return false;
212        };
213        let init_mask = alloc.0.init_mask();
214        let init_range = init_mask.is_range_initialized(AllocRange {
215            start: Size::ZERO,
216            size: Size::from_bytes(alloc.0.len()),
217        });
218        if let Err(range) = init_range {
219            if range.size == alloc.0.size() {
220                return true;
221            }
222        }
223        false
224    }
225}
226
227///////////////////////////////////////////////////////////////////////////
228/// Constants
229
230#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
231#[derive(TypeFoldable, TypeVisitable, Lift)]
232pub enum Const<'tcx> {
233    /// This constant came from the type system.
234    ///
235    /// Any way of turning `ty::Const` into `ConstValue` should go through `valtree_to_const_val`;
236    /// this ensures that we consistently produce "clean" values without data in the padding or
237    /// anything like that.
238    ///
239    /// FIXME(BoxyUwU): We should remove this `Ty` and look up the type for params via `ParamEnv`
240    Ty(Ty<'tcx>, ty::Const<'tcx>),
241
242    /// An unevaluated mir constant which is not part of the type system.
243    ///
244    /// Note that `Ty(ty::ConstKind::Unevaluated)` and this variant are *not* identical! `Ty` will
245    /// always flow through a valtree, so all data not captured in the valtree is lost. This variant
246    /// directly uses the evaluated result of the given constant, including e.g. data stored in
247    /// padding.
248    Unevaluated(UnevaluatedConst<'tcx>, Ty<'tcx>),
249
250    /// This constant cannot go back into the type system, as it represents
251    /// something the type system cannot handle (e.g. pointers).
252    Val(ConstValue, Ty<'tcx>),
253}
254
255impl<'tcx> Const<'tcx> {
256    /// Creates an unevaluated const from a `DefId` for a const item.
257    /// The binders of the const item still need to be instantiated.
258    pub fn from_unevaluated(
259        tcx: TyCtxt<'tcx>,
260        def_id: DefId,
261    ) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
262        ty::EarlyBinder::bind(Const::Unevaluated(
263            UnevaluatedConst {
264                def: def_id,
265                args: ty::GenericArgs::identity_for_item(tcx, def_id),
266                promoted: None,
267            },
268            tcx.type_of(def_id).skip_binder(),
269        ))
270    }
271
272    #[inline(always)]
273    pub fn ty(&self) -> Ty<'tcx> {
274        match self {
275            Const::Ty(ty, ct) => {
276                match ct.kind() {
277                    // Dont use the outer ty as on invalid code we can wind up with them not being the same.
278                    // this then results in allowing const eval to add `1_i64 + 1_usize` in cases where the mir
279                    // was originally `({N: usize} + 1_usize)` under `generic_const_exprs`.
280                    ty::ConstKind::Value(cv) => cv.ty,
281                    _ => *ty,
282                }
283            }
284            Const::Val(_, ty) | Const::Unevaluated(_, ty) => *ty,
285        }
286    }
287
288    /// Determines whether we need to add this const to `required_consts`. This is the case if and
289    /// only if evaluating it may error.
290    #[inline]
291    pub fn is_required_const(&self) -> bool {
292        match self {
293            Const::Ty(_, c) => match c.kind() {
294                ty::ConstKind::Value(_) => false, // already a value, cannot error
295                _ => true,
296            },
297            Const::Val(..) => false, // already a value, cannot error
298            Const::Unevaluated(..) => true,
299        }
300    }
301
302    #[inline]
303    pub fn try_to_scalar(self) -> Option<Scalar> {
304        match self {
305            Const::Ty(_, c) => c.try_to_scalar(),
306            Const::Val(val, _) => val.try_to_scalar(),
307            Const::Unevaluated(..) => None,
308        }
309    }
310
311    #[inline]
312    pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
313        // This is equivalent to `self.try_to_scalar()?.try_to_int().ok()`, but measurably faster.
314        match self {
315            Const::Val(ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
316            Const::Ty(_, c) => c.try_to_leaf(),
317            _ => None,
318        }
319    }
320
321    #[inline]
322    pub fn try_to_bits(self, size: Size) -> Option<u128> {
323        Some(self.try_to_scalar_int()?.to_bits(size))
324    }
325
326    #[inline]
327    pub fn try_to_bool(self) -> Option<bool> {
328        self.try_to_scalar_int()?.try_into().ok()
329    }
330
331    #[inline]
332    pub fn eval(
333        self,
334        tcx: TyCtxt<'tcx>,
335        typing_env: ty::TypingEnv<'tcx>,
336        span: Span,
337    ) -> Result<ConstValue, ErrorHandled> {
338        match self {
339            Const::Ty(_, c) => {
340                if c.has_non_region_param() {
341                    return Err(ErrorHandled::TooGeneric(span));
342                }
343
344                match c.kind() {
345                    ConstKind::Value(cv) => Ok(tcx.valtree_to_const_val(cv)),
346                    ConstKind::Expr(_) => {
347                        bug!("Normalization of `ty::ConstKind::Expr` is unimplemented")
348                    }
349                    _ => Err(ReportedErrorInfo::non_const_eval_error(
350                        tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body"),
351                    )
352                    .into()),
353                }
354            }
355            Const::Unevaluated(uneval, _) => {
356                // FIXME: We might want to have a `try_eval`-like function on `Unevaluated`
357                tcx.const_eval_resolve(typing_env, uneval, span)
358            }
359            Const::Val(val, _) => Ok(val),
360        }
361    }
362
363    #[inline]
364    pub fn try_eval_scalar(
365        self,
366        tcx: TyCtxt<'tcx>,
367        typing_env: ty::TypingEnv<'tcx>,
368    ) -> Option<Scalar> {
369        if let Const::Ty(_, c) = self {
370            // We don't evaluate anything for type system constants as normalizing
371            // the MIR will handle this for us
372            c.try_to_scalar()
373        } else {
374            self.eval(tcx, typing_env, DUMMY_SP).ok()?.try_to_scalar()
375        }
376    }
377
378    #[inline]
379    pub fn try_eval_scalar_int(
380        self,
381        tcx: TyCtxt<'tcx>,
382        typing_env: ty::TypingEnv<'tcx>,
383    ) -> Option<ScalarInt> {
384        self.try_eval_scalar(tcx, typing_env)?.try_to_scalar_int().ok()
385    }
386
387    #[inline]
388    pub fn try_eval_bits(
389        &self,
390        tcx: TyCtxt<'tcx>,
391        typing_env: ty::TypingEnv<'tcx>,
392    ) -> Option<u128> {
393        let int = self.try_eval_scalar_int(tcx, typing_env)?;
394        let size = tcx
395            .layout_of(typing_env.with_post_analysis_normalized(tcx).as_query_input(self.ty()))
396            .ok()?
397            .size;
398        Some(int.to_bits(size))
399    }
400
401    /// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
402    #[inline]
403    pub fn eval_bits(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> u128 {
404        self.try_eval_bits(tcx, typing_env)
405            .unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", self.ty(), self))
406    }
407
408    #[inline]
409    pub fn try_eval_target_usize(
410        self,
411        tcx: TyCtxt<'tcx>,
412        typing_env: ty::TypingEnv<'tcx>,
413    ) -> Option<u64> {
414        Some(self.try_eval_scalar_int(tcx, typing_env)?.to_target_usize(tcx))
415    }
416
417    #[inline]
418    /// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
419    pub fn eval_target_usize(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> u64 {
420        self.try_eval_target_usize(tcx, typing_env)
421            .unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
422    }
423
424    #[inline]
425    pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Option<bool> {
426        self.try_eval_scalar_int(tcx, typing_env)?.try_into().ok()
427    }
428
429    #[inline]
430    pub fn from_value(val: ConstValue, ty: Ty<'tcx>) -> Self {
431        Self::Val(val, ty)
432    }
433
434    #[inline]
435    pub fn from_ty_value(tcx: TyCtxt<'tcx>, val: ty::Value<'tcx>) -> Self {
436        Self::Ty(val.ty, ty::Const::new_value(tcx, val.valtree, val.ty))
437    }
438
439    pub fn from_bits(
440        tcx: TyCtxt<'tcx>,
441        bits: u128,
442        typing_env: ty::TypingEnv<'tcx>,
443        ty: Ty<'tcx>,
444    ) -> Self {
445        let size = tcx
446            .layout_of(typing_env.as_query_input(ty))
447            .unwrap_or_else(|e| bug!("could not compute layout for {ty:?}: {e:?}"))
448            .size;
449        let cv = ConstValue::Scalar(Scalar::from_uint(bits, size));
450
451        Self::Val(cv, ty)
452    }
453
454    #[inline]
455    pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
456        let cv = ConstValue::from_bool(v);
457        Self::Val(cv, tcx.types.bool)
458    }
459
460    #[inline]
461    pub fn zero_sized(ty: Ty<'tcx>) -> Self {
462        let cv = ConstValue::ZeroSized;
463        Self::Val(cv, ty)
464    }
465
466    pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
467        let ty = tcx.types.usize;
468        let typing_env = ty::TypingEnv::fully_monomorphized();
469        Self::from_bits(tcx, n as u128, typing_env, ty)
470    }
471
472    #[inline]
473    pub fn from_scalar(_tcx: TyCtxt<'tcx>, s: Scalar, ty: Ty<'tcx>) -> Self {
474        let val = ConstValue::Scalar(s);
475        Self::Val(val, ty)
476    }
477
478    /// Return true if any evaluation of this constant always returns the same value,
479    /// taking into account even pointer identity tests.
480    pub fn is_deterministic(&self) -> bool {
481        // Some constants may generate fresh allocations for pointers they contain,
482        // so using the same constant twice can yield two different results.
483        // Notably, valtrees purposefully generate new allocations.
484        match self {
485            Const::Ty(_, c) => match c.kind() {
486                ty::ConstKind::Param(..) => true,
487                // A valtree may be a reference. Valtree references correspond to a
488                // different allocation each time they are evaluated. Valtrees for primitive
489                // types are fine though.
490                ty::ConstKind::Value(cv) => cv.ty.is_primitive(),
491                ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false,
492                // This can happen if evaluation of a constant failed. The result does not matter
493                // much since compilation is doomed.
494                ty::ConstKind::Error(..) => false,
495                // Should not appear in runtime MIR.
496                ty::ConstKind::Infer(..)
497                | ty::ConstKind::Bound(..)
498                | ty::ConstKind::Placeholder(..) => bug!(),
499            },
500            Const::Unevaluated(..) => false,
501            Const::Val(
502                ConstValue::Slice { .. }
503                | ConstValue::ZeroSized
504                | ConstValue::Scalar(_)
505                | ConstValue::Indirect { .. },
506                _,
507            ) => true,
508        }
509    }
510}
511
512/// An unevaluated (potentially generic) constant used in MIR.
513#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable)]
514#[derive(Hash, HashStable, TypeFoldable, TypeVisitable, Lift)]
515pub struct UnevaluatedConst<'tcx> {
516    pub def: DefId,
517    pub args: GenericArgsRef<'tcx>,
518    pub promoted: Option<Promoted>,
519}
520
521impl<'tcx> UnevaluatedConst<'tcx> {
522    #[inline]
523    pub fn shrink(self) -> ty::UnevaluatedConst<'tcx> {
524        assert_eq!(self.promoted, None);
525        ty::UnevaluatedConst { def: self.def, args: self.args }
526    }
527}
528
529impl<'tcx> UnevaluatedConst<'tcx> {
530    #[inline]
531    pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> {
532        UnevaluatedConst { def, args, promoted: Default::default() }
533    }
534
535    #[inline]
536    pub fn from_instance(instance: ty::Instance<'tcx>) -> Self {
537        UnevaluatedConst::new(instance.def_id(), instance.args)
538    }
539}
540
541impl<'tcx> Display for Const<'tcx> {
542    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
543        match *self {
544            Const::Ty(_, c) => pretty_print_const(c, fmt, true),
545            Const::Val(val, ty) => pretty_print_const_value(val, ty, fmt),
546            // FIXME(valtrees): Correctly print mir constants.
547            Const::Unevaluated(c, _ty) => {
548                ty::tls::with(move |tcx| {
549                    let c = tcx.lift(c).unwrap();
550                    // Matches `GlobalId` printing.
551                    let instance =
552                        with_no_trimmed_paths!(tcx.def_path_str_with_args(c.def, c.args));
553                    write!(fmt, "{instance}")?;
554                    if let Some(promoted) = c.promoted {
555                        write!(fmt, "::{promoted:?}")?;
556                    }
557                    Ok(())
558                })
559            }
560        }
561    }
562}
563
564///////////////////////////////////////////////////////////////////////////
565// Const-related utilities
566
567impl<'tcx> TyCtxt<'tcx> {
568    pub fn span_as_caller_location(self, span: Span) -> ConstValue {
569        let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
570        let caller = self.sess.source_map().lookup_char_pos(topmost.lo());
571        self.const_caller_location(
572            Symbol::intern(
573                &caller.file.name.display(RemapPathScopeComponents::MACRO).to_string_lossy(),
574            ),
575            caller.line as u32,
576            caller.col_display as u32 + 1,
577        )
578    }
579}