rustc_type_ir/
const_kind.rs

1use std::fmt;
2
3use derive_where::derive_where;
4#[cfg(feature = "nightly")]
5use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6#[cfg(feature = "nightly")]
7use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
8use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
9
10use crate::{self as ty, DebruijnIndex, Interner};
11
12/// Represents a constant in Rust.
13#[derive_where(Clone, Copy, Hash, PartialEq; I: Interner)]
14#[cfg_attr(
15    feature = "nightly",
16    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
17)]
18pub enum ConstKind<I: Interner> {
19    /// A const generic parameter.
20    Param(I::ParamConst),
21
22    /// Infer the value of the const.
23    Infer(InferConst),
24
25    /// Bound const variable, used only when preparing a trait query.
26    Bound(DebruijnIndex, I::BoundConst),
27
28    /// A placeholder const - universally quantified higher-ranked const.
29    Placeholder(I::PlaceholderConst),
30
31    /// An unnormalized const item such as an anon const or assoc const or free const item.
32    /// Right now anything other than anon consts does not actually work properly but this
33    /// should
34    Unevaluated(ty::UnevaluatedConst<I>),
35
36    /// Used to hold computed value.
37    Value(I::ValueConst),
38
39    /// A placeholder for a const which could not be computed; this is
40    /// propagated to avoid useless error messages.
41    Error(I::ErrorGuaranteed),
42
43    /// Unevaluated non-const-item, used by `feature(generic_const_exprs)` to represent
44    /// const arguments such as `N + 1` or `foo(N)`
45    Expr(I::ExprConst),
46}
47
48impl<I: Interner> Eq for ConstKind<I> {}
49
50impl<I: Interner> fmt::Debug for ConstKind<I> {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        use ConstKind::*;
53
54        match self {
55            Param(param) => write!(f, "{param:?}"),
56            Infer(var) => write!(f, "{var:?}"),
57            Bound(debruijn, var) => crate::debug_bound_var(f, *debruijn, var),
58            Placeholder(placeholder) => write!(f, "{placeholder:?}"),
59            Unevaluated(uv) => write!(f, "{uv:?}"),
60            Value(val) => write!(f, "{val:?}"),
61            Error(_) => write!(f, "{{const error}}"),
62            Expr(expr) => write!(f, "{expr:?}"),
63        }
64    }
65}
66
67/// An unevaluated (potentially generic) constant used in the type-system.
68#[derive_where(Clone, Copy, Debug, Hash, PartialEq; I: Interner)]
69#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
70#[cfg_attr(
71    feature = "nightly",
72    derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
73)]
74pub struct UnevaluatedConst<I: Interner> {
75    pub def: I::DefId,
76    pub args: I::GenericArgs,
77}
78
79impl<I: Interner> Eq for UnevaluatedConst<I> {}
80
81impl<I: Interner> UnevaluatedConst<I> {
82    #[inline]
83    pub fn new(def: I::DefId, args: I::GenericArgs) -> UnevaluatedConst<I> {
84        UnevaluatedConst { def, args }
85    }
86}
87
88rustc_index::newtype_index! {
89    /// A **`const`** **v**ariable **ID**.
90    #[encodable]
91    #[orderable]
92    #[debug_format = "?{}c"]
93    #[gate_rustc_only]
94    pub struct ConstVid {}
95}
96
97/// An inference variable for a const, for use in const generics.
98#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
99#[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))]
100pub enum InferConst {
101    /// Infer the value of the const.
102    Var(ConstVid),
103    /// A fresh const variable. See `infer::freshen` for more details.
104    Fresh(u32),
105}
106
107impl fmt::Debug for InferConst {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        match self {
110            InferConst::Var(var) => write!(f, "{var:?}"),
111            InferConst::Fresh(var) => write!(f, "Fresh({var:?})"),
112        }
113    }
114}
115
116#[cfg(feature = "nightly")]
117impl<CTX> HashStable<CTX> for InferConst {
118    fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
119        match self {
120            InferConst::Var(_) => {
121                panic!("const variables should not be hashed: {self:?}")
122            }
123            InferConst::Fresh(i) => i.hash_stable(hcx, hasher),
124        }
125    }
126}