1use std::borrow::Cow;
2
3use rustc_data_structures::intern::Interned;
4use rustc_error_messages::MultiSpan;
5use rustc_macros::{HashStable, TyDecodable, TyEncodable};
6use rustc_type_ir::walk::TypeWalker;
7use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
8
9use crate::ty::{self, Ty, TyCtxt};
10
11mod int;
12mod kind;
13mod valtree;
14
15pub use int::*;
16pub use kind::*;
17use rustc_span::{DUMMY_SP, ErrorGuaranteed};
18pub use valtree::*;
19
20pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
21pub type UnevaluatedConst<'tcx> = ir::UnevaluatedConst<TyCtxt<'tcx>>;
22
23#[cfg(target_pointer_width = "64")]
24rustc_data_structures::static_assert_size!(ConstKind<'_>, 24);
25
26#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
27#[rustc_pass_by_value]
28pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstKind<'tcx>>>);
29
30impl<'tcx> rustc_type_ir::inherent::IntoKind for Const<'tcx> {
31 type Kind = ConstKind<'tcx>;
32
33 fn kind(self) -> ConstKind<'tcx> {
34 self.kind()
35 }
36}
37
38impl<'tcx> rustc_type_ir::Flags for Const<'tcx> {
39 fn flags(&self) -> TypeFlags {
40 self.0.flags
41 }
42
43 fn outer_exclusive_binder(&self) -> rustc_type_ir::DebruijnIndex {
44 self.0.outer_exclusive_binder
45 }
46}
47
48impl<'tcx> Const<'tcx> {
49 #[inline]
50 pub fn kind(self) -> ConstKind<'tcx> {
51 let a: &ConstKind<'tcx> = self.0.0;
52 *a
53 }
54
55 #[inline]
57 pub fn flags(self) -> TypeFlags {
58 self.0.flags
59 }
60
61 #[inline]
63 pub fn outer_exclusive_binder(self) -> ty::DebruijnIndex {
64 self.0.outer_exclusive_binder
65 }
66
67 #[inline]
68 pub fn new(tcx: TyCtxt<'tcx>, kind: ty::ConstKind<'tcx>) -> Const<'tcx> {
69 tcx.mk_ct_from_kind(kind)
70 }
71
72 #[inline]
73 pub fn new_param(tcx: TyCtxt<'tcx>, param: ty::ParamConst) -> Const<'tcx> {
74 Const::new(tcx, ty::ConstKind::Param(param))
75 }
76
77 #[inline]
78 pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid) -> Const<'tcx> {
79 Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)))
80 }
81
82 #[inline]
83 pub fn new_fresh(tcx: TyCtxt<'tcx>, fresh: u32) -> Const<'tcx> {
84 Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Fresh(fresh)))
85 }
86
87 #[inline]
88 pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst) -> Const<'tcx> {
89 Const::new(tcx, ty::ConstKind::Infer(infer))
90 }
91
92 #[inline]
93 pub fn new_bound(
94 tcx: TyCtxt<'tcx>,
95 debruijn: ty::DebruijnIndex,
96 bound_const: ty::BoundConst,
97 ) -> Const<'tcx> {
98 Const::new(tcx, ty::ConstKind::Bound(debruijn, bound_const))
99 }
100
101 #[inline]
102 pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Const<'tcx> {
103 Const::new(tcx, ty::ConstKind::Placeholder(placeholder))
104 }
105
106 #[inline]
107 pub fn new_unevaluated(tcx: TyCtxt<'tcx>, uv: ty::UnevaluatedConst<'tcx>) -> Const<'tcx> {
108 tcx.debug_assert_args_compatible(uv.def, uv.args);
109 Const::new(tcx, ty::ConstKind::Unevaluated(uv))
110 }
111
112 #[inline]
113 pub fn new_value(tcx: TyCtxt<'tcx>, valtree: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
114 Const::new(tcx, ty::ConstKind::Value(ty::Value { ty, valtree }))
115 }
116
117 #[inline]
118 pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Const<'tcx> {
119 Const::new(tcx, ty::ConstKind::Expr(expr))
120 }
121
122 #[inline]
123 pub fn new_error(tcx: TyCtxt<'tcx>, e: ty::ErrorGuaranteed) -> Const<'tcx> {
124 Const::new(tcx, ty::ConstKind::Error(e))
125 }
126
127 #[track_caller]
129 pub fn new_misc_error(tcx: TyCtxt<'tcx>) -> Const<'tcx> {
130 Const::new_error_with_message(
131 tcx,
132 DUMMY_SP,
133 "ty::ConstKind::Error constructed but no error reported",
134 )
135 }
136
137 #[track_caller]
139 pub fn new_error_with_message<S: Into<MultiSpan>>(
140 tcx: TyCtxt<'tcx>,
141 span: S,
142 msg: impl Into<Cow<'static, str>>,
143 ) -> Const<'tcx> {
144 let reported = tcx.dcx().span_delayed_bug(span, msg);
145 Const::new_error(tcx, reported)
146 }
147
148 pub fn is_trivially_wf(self) -> bool {
149 match self.kind() {
150 ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) | ty::ConstKind::Bound(..) => {
151 true
152 }
153 ty::ConstKind::Infer(_)
154 | ty::ConstKind::Unevaluated(..)
155 | ty::ConstKind::Value(_)
156 | ty::ConstKind::Error(_)
157 | ty::ConstKind::Expr(_) => false,
158 }
159 }
160}
161
162impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
163 fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst) -> Self {
164 Const::new_infer(tcx, infer)
165 }
166
167 fn new_var(tcx: TyCtxt<'tcx>, vid: ty::ConstVid) -> Self {
168 Const::new_var(tcx, vid)
169 }
170
171 fn new_bound(
172 interner: TyCtxt<'tcx>,
173 debruijn: ty::DebruijnIndex,
174 bound_const: ty::BoundConst,
175 ) -> Self {
176 Const::new_bound(interner, debruijn, bound_const)
177 }
178
179 fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
180 Const::new_bound(tcx, debruijn, ty::BoundConst { var })
181 }
182
183 fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Self {
184 Const::new_placeholder(tcx, placeholder)
185 }
186
187 fn new_unevaluated(interner: TyCtxt<'tcx>, uv: ty::UnevaluatedConst<'tcx>) -> Self {
188 Const::new_unevaluated(interner, uv)
189 }
190
191 fn new_expr(interner: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Self {
192 Const::new_expr(interner, expr)
193 }
194
195 fn new_error(interner: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
196 Const::new_error(interner, guar)
197 }
198}
199
200impl<'tcx> Const<'tcx> {
201 #[inline]
203 pub fn from_bits(
204 tcx: TyCtxt<'tcx>,
205 bits: u128,
206 typing_env: ty::TypingEnv<'tcx>,
207 ty: Ty<'tcx>,
208 ) -> Self {
209 let size = tcx
210 .layout_of(typing_env.as_query_input(ty))
211 .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}"))
212 .size;
213 ty::Const::new_value(
214 tcx,
215 ty::ValTree::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap()),
216 ty,
217 )
218 }
219
220 #[inline]
221 pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
223 ty::Const::new_value(tcx, ty::ValTree::zst(tcx), ty)
224 }
225
226 #[inline]
227 pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
229 Self::from_bits(tcx, v as u128, ty::TypingEnv::fully_monomorphized(), tcx.types.bool)
230 }
231
232 #[inline]
233 pub fn from_target_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
235 Self::from_bits(tcx, n as u128, ty::TypingEnv::fully_monomorphized(), tcx.types.usize)
236 }
237
238 pub fn to_value(self) -> ty::Value<'tcx> {
240 match self.kind() {
241 ty::ConstKind::Value(cv) => cv,
242 _ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
243 }
244 }
245
246 pub fn try_to_value(self) -> Option<ty::Value<'tcx>> {
250 match self.kind() {
251 ty::ConstKind::Value(cv) => Some(cv),
252 _ => None,
253 }
254 }
255
256 #[inline]
261 pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
262 self.try_to_value()?.try_to_target_usize(tcx)
263 }
264
265 pub fn is_ct_infer(self) -> bool {
266 matches!(self.kind(), ty::ConstKind::Infer(_))
267 }
268
269 pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
280 TypeWalker::new(self.into())
281 }
282}
283
284#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable, HashStable)]
285pub enum AnonConstKind {
286 GCE,
288 MCG,
290 RepeatExprCount,
293 NonTypeSystem,
295}