rustc_type_ir/
const_kind.rs1use 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; 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> 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#[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 #[encodable]
91 #[orderable]
92 #[debug_format = "?{}c"]
93 #[gate_rustc_only]
94 pub struct ConstVid {}
95}
96
97#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
99#[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))]
100pub enum InferConst {
101 Var(ConstVid),
103 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}