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#[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)]
14#[cfg_attr(
15 feature = "nightly",
16 derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
17)]
18pub enum ConstKind<I: Interner> {
19 Param(I::ParamConst),
21
22 Infer(InferConst),
24
25 Bound(DebruijnIndex, I::BoundConst),
27
28 Placeholder(I::PlaceholderConst),
30
31 Unevaluated(ty::UnevaluatedConst<I>),
35
36 Value(I::ValueConst),
38
39 Error(I::ErrorGuaranteed),
42
43 Expr(I::ExprConst),
46}
47
48impl<I: Interner> fmt::Debug for ConstKind<I> {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 use ConstKind::*;
51
52 match self {
53 Param(param) => write!(f, "{param:?}"),
54 Infer(var) => write!(f, "{var:?}"),
55 Bound(debruijn, var) => crate::debug_bound_var(f, *debruijn, var),
56 Placeholder(placeholder) => write!(f, "{placeholder:?}"),
57 Unevaluated(uv) => write!(f, "{uv:?}"),
58 Value(val) => write!(f, "{val:?}"),
59 Error(_) => write!(f, "{{const error}}"),
60 Expr(expr) => write!(f, "{expr:?}"),
61 }
62 }
63}
64
65#[derive_where(Clone, Copy, Debug, Hash, PartialEq, Eq; I: Interner)]
67#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
68#[cfg_attr(
69 feature = "nightly",
70 derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
71)]
72pub struct UnevaluatedConst<I: Interner> {
73 pub def: I::DefId,
74 pub args: I::GenericArgs,
75}
76
77impl<I: Interner> UnevaluatedConst<I> {
78 #[inline]
79 pub fn new(def: I::DefId, args: I::GenericArgs) -> UnevaluatedConst<I> {
80 UnevaluatedConst { def, args }
81 }
82}
83
84rustc_index::newtype_index! {
85 #[encodable]
87 #[orderable]
88 #[debug_format = "?{}c"]
89 #[gate_rustc_only]
90 pub struct ConstVid {}
91}
92
93#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
95#[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))]
96pub enum InferConst {
97 Var(ConstVid),
99 Fresh(u32),
101}
102
103impl fmt::Debug for InferConst {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 match self {
106 InferConst::Var(var) => write!(f, "{var:?}"),
107 InferConst::Fresh(var) => write!(f, "Fresh({var:?})"),
108 }
109 }
110}
111
112#[cfg(feature = "nightly")]
113impl<CTX> HashStable<CTX> for InferConst {
114 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
115 match self {
116 InferConst::Var(_) => {
117 panic!("const variables should not be hashed: {self:?}")
118 }
119 InferConst::Fresh(i) => i.hash_stable(hcx, hasher),
120 }
121 }
122}