1 use crate::mir::interpret::LitToConstInput;
2 use crate::mir::ConstantKind;
4 self, InlineConstSubsts, InlineConstSubstsParts, InternalSubsts, ParamEnv, ParamEnvAnd, Ty,
7 use rustc_data_structures::intern::Interned;
8 use rustc_errors::ErrorGuaranteed;
10 use rustc_hir::def_id::{DefId, LocalDefId};
11 use rustc_macros::HashStable;
22 /// Use this rather than `ConstS`, whenever possible.
23 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
24 #[rustc_pass_by_value]
25 pub struct Const<'tcx>(pub Interned<'tcx, ConstS<'tcx>>);
27 impl<'tcx> fmt::Debug for Const<'tcx> {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 // This reflects what `Const` looked liked before `Interned` was
30 // introduced. We print it like this to avoid having to update expected
31 // output in a lot of tests.
32 write!(f, "Const {{ ty: {:?}, kind: {:?} }}", self.ty(), self.kind())
36 /// Typed constant value.
37 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)]
38 pub struct ConstS<'tcx> {
40 pub kind: ConstKind<'tcx>,
43 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
44 static_assert_size!(ConstS<'_>, 48);
46 impl<'tcx> Const<'tcx> {
48 pub fn ty(self) -> Ty<'tcx> {
53 pub fn kind(self) -> ConstKind<'tcx> {
57 /// Literals and const generic parameters are eagerly converted to a constant, everything else
58 /// becomes `Unevaluated`.
59 pub fn from_anon_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
60 Self::from_opt_const_arg_anon_const(tcx, ty::WithOptConstParam::unknown(def_id))
63 #[instrument(skip(tcx), level = "debug")]
64 pub fn from_opt_const_arg_anon_const(
66 def: ty::WithOptConstParam<LocalDefId>,
68 let body_id = match tcx.hir().get_by_def_id(def.did) {
69 hir::Node::AnonConst(ac) => ac.body,
71 tcx.def_span(def.did.to_def_id()),
72 "from_anon_const can only process anonymous constants"
76 let expr = &tcx.hir().body(body_id).value;
79 let ty = tcx.type_of(def.def_id_for_type_of());
81 match Self::try_eval_lit_or_param(tcx, ty, expr) {
83 None => tcx.mk_const(ty::ConstS {
84 kind: ty::ConstKind::Unevaluated(ty::Unevaluated {
86 substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
94 #[instrument(skip(tcx), level = "debug")]
95 fn try_eval_lit_or_param(
98 expr: &'tcx hir::Expr<'tcx>,
100 // Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
101 // currently have to be wrapped in curly brackets, so it's necessary to special-case.
102 let expr = match &expr.kind {
103 hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
104 block.expr.as_ref().unwrap()
109 let lit_input = match expr.kind {
110 hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
111 hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => match expr.kind {
112 hir::ExprKind::Lit(ref lit) => {
113 Some(LitToConstInput { lit: &lit.node, ty, neg: true })
120 if let Some(lit_input) = lit_input {
121 // If an error occurred, ignore that it's a literal and leave reporting the error up to
123 match tcx.at(expr.span).lit_to_const(lit_input) {
124 Ok(c) => return Some(c),
126 tcx.sess.delay_span_bug(
128 &format!("Const::from_anon_const: couldn't lit_to_const {:?}", e),
134 use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
136 ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
137 // Find the name and index of the const parameter by indexing the generics of
138 // the parent item and construct a `ParamConst`.
139 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
140 let item_id = tcx.hir().get_parent_node(hir_id);
141 let item_def_id = tcx.hir().local_def_id(item_id);
142 let generics = tcx.generics_of(item_def_id.to_def_id());
143 let index = generics.param_def_id_to_index[&def_id];
144 let name = tcx.hir().name(hir_id);
145 Some(tcx.mk_const(ty::ConstS {
146 kind: ty::ConstKind::Param(ty::ParamConst::new(index, name)),
154 pub fn from_inline_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
155 debug!("Const::from_inline_const(def_id={:?})", def_id);
157 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
159 let body_id = match tcx.hir().get(hir_id) {
160 hir::Node::AnonConst(ac) => ac.body,
162 tcx.def_span(def_id.to_def_id()),
163 "from_inline_const can only process anonymous constants"
167 let expr = &tcx.hir().body(body_id).value;
169 let ty = tcx.typeck(def_id).node_type(hir_id);
171 let ret = match Self::try_eval_lit_or_param(tcx, ty, expr) {
174 let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id());
176 tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));
178 InlineConstSubsts::new(tcx, InlineConstSubstsParts { parent_substs, ty })
180 tcx.mk_const(ty::ConstS {
181 kind: ty::ConstKind::Unevaluated(ty::Unevaluated {
182 def: ty::WithOptConstParam::unknown(def_id).to_global(),
190 debug_assert!(!ret.has_free_regions());
194 /// Interns the given value as a constant.
196 pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self {
197 tcx.mk_const(ConstS { kind: ConstKind::Value(val), ty })
200 /// Panics if self.kind != ty::ConstKind::Value
201 pub fn to_valtree(self) -> ty::ValTree<'tcx> {
203 ty::ConstKind::Value(valtree) => valtree,
204 _ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
208 pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self {
209 let valtree = ty::ValTree::from_scalar_int(i);
210 Self::from_value(tcx, valtree, ty)
214 /// Creates a constant with the given integer value and interns it.
215 pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Self {
218 .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
220 Self::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap(), ty.value)
224 /// Creates an interned zst constant.
225 pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
226 let valtree = ty::ValTree::zst();
227 Self::from_value(tcx, valtree, ty)
231 /// Creates an interned bool constant.
232 pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
233 Self::from_bits(tcx, v as u128, ParamEnv::empty().and(tcx.types.bool))
237 /// Creates an interned usize constant.
238 pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
239 Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize))
243 /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
244 /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
245 /// contains const generic parameters or pointers).
246 pub fn try_eval_bits(
249 param_env: ParamEnv<'tcx>,
252 assert_eq!(self.ty(), ty);
253 let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
254 // if `ty` does not depend on generic parameters, use an empty param_env
255 self.kind().eval(tcx, param_env).try_to_bits(size)
259 pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<bool> {
260 self.kind().eval(tcx, param_env).try_to_bool()
264 pub fn try_eval_usize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<u64> {
265 self.kind().eval(tcx, param_env).try_to_machine_usize(tcx)
269 /// Tries to evaluate the constant if it is `Unevaluated`. If that doesn't succeed, return the
270 /// unevaluated constant.
271 pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> {
272 if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
274 Ok(val) => Const::from_value(tcx, val, self.ty()),
275 Err(ErrorGuaranteed { .. }) => tcx.const_error(self.ty()),
278 // Either the constant isn't evaluatable or ValTree creation failed.
284 /// Tries to evaluate the constant if it is `Unevaluated` and creates a ConstValue if the
285 /// evaluation succeeds. If it doesn't succeed, returns the unevaluated constant.
286 pub fn eval_for_mir(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> ConstantKind<'tcx> {
287 if let Some(val) = self.kind().try_eval_for_mir(tcx, param_env) {
289 Ok(const_val) => ConstantKind::from_value(const_val, self.ty()),
290 Err(ErrorGuaranteed { .. }) => ConstantKind::Ty(tcx.const_error(self.ty())),
293 ConstantKind::Ty(self)
298 /// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
299 pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
300 self.try_eval_bits(tcx, param_env, ty)
301 .unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
305 /// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
306 pub fn eval_usize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u64 {
307 self.try_eval_usize(tcx, param_env)
308 .unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
312 pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> {
313 let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) {
314 hir::Node::GenericParam(hir::GenericParam {
315 kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
317 }) => tcx.hir().local_def_id(ac.hir_id),
319 tcx.def_span(def_id),
320 "`const_param_default` expected a generic parameter with a constant"
323 Const::from_anon_const(tcx, default_def_id)