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 var: ty::BoundVar,
97 ) -> Const<'tcx> {
98 Const::new(tcx, ty::ConstKind::Bound(debruijn, var))
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(interner: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
172 Const::new_bound(interner, debruijn, var)
173 }
174
175 fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
176 Const::new_bound(tcx, debruijn, var)
177 }
178
179 fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Self {
180 Const::new_placeholder(tcx, placeholder)
181 }
182
183 fn new_unevaluated(interner: TyCtxt<'tcx>, uv: ty::UnevaluatedConst<'tcx>) -> Self {
184 Const::new_unevaluated(interner, uv)
185 }
186
187 fn new_expr(interner: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Self {
188 Const::new_expr(interner, expr)
189 }
190
191 fn new_error(interner: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
192 Const::new_error(interner, guar)
193 }
194}
195
196impl<'tcx> Const<'tcx> {
197 #[inline]
199 pub fn from_bits(
200 tcx: TyCtxt<'tcx>,
201 bits: u128,
202 typing_env: ty::TypingEnv<'tcx>,
203 ty: Ty<'tcx>,
204 ) -> Self {
205 let size = tcx
206 .layout_of(typing_env.as_query_input(ty))
207 .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}"))
208 .size;
209 ty::Const::new_value(
210 tcx,
211 ty::ValTree::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap()),
212 ty,
213 )
214 }
215
216 #[inline]
217 pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
219 ty::Const::new_value(tcx, ty::ValTree::zst(tcx), ty)
220 }
221
222 #[inline]
223 pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
225 Self::from_bits(tcx, v as u128, ty::TypingEnv::fully_monomorphized(), tcx.types.bool)
226 }
227
228 #[inline]
229 pub fn from_target_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
231 Self::from_bits(tcx, n as u128, ty::TypingEnv::fully_monomorphized(), tcx.types.usize)
232 }
233
234 pub fn to_value(self) -> ty::Value<'tcx> {
236 match self.kind() {
237 ty::ConstKind::Value(cv) => cv,
238 _ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
239 }
240 }
241
242 pub fn try_to_value(self) -> Option<ty::Value<'tcx>> {
246 match self.kind() {
247 ty::ConstKind::Value(cv) => Some(cv),
248 _ => None,
249 }
250 }
251
252 #[inline]
257 pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
258 self.try_to_value()?.try_to_target_usize(tcx)
259 }
260
261 pub fn is_ct_infer(self) -> bool {
262 matches!(self.kind(), ty::ConstKind::Infer(_))
263 }
264
265 pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
276 TypeWalker::new(self.into())
277 }
278}
279
280#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable, HashStable)]
281pub enum AnonConstKind {
282 GCE,
284 MCG,
286 RepeatExprCount,
289 NonTypeSystem,
291}