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