]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/build/expr/as_constant.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / compiler / rustc_mir_build / src / build / expr / as_constant.rs
CommitLineData
e9174d1e
SL
1//! See docs in build/expr/mod.rs
2
923072b8 3use crate::build::{parse_float_into_constval, Builder};
04454e1e 4use rustc_ast as ast;
2b03887a 5use rustc_middle::mir;
923072b8
FG
6use rustc_middle::mir::interpret::{
7 Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
8};
ba9703b0 9use rustc_middle::mir::*;
17df50a5 10use rustc_middle::thir::*;
487cf647
FG
11use rustc_middle::ty::{
12 self, CanonicalUserType, CanonicalUserTypeAnnotation, TyCtxt, UserTypeAnnotationIndex,
13};
14use rustc_span::DUMMY_SP;
5e7ed085 15use rustc_target::abi::Size;
e9174d1e 16
dc9dc135 17impl<'a, 'tcx> Builder<'a, 'tcx> {
e9174d1e
SL
18 /// Compile `expr`, yielding a compile-time constant. Assumes that
19 /// `expr` is a valid compile-time constant!
923072b8 20 pub(crate) fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
e9174d1e 21 let this = self;
5e7ed085 22 let tcx = this.tcx;
6a06907d 23 let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
487cf647 24 match kind {
17df50a5 25 ExprKind::Scope { region_scope: _, lint_level: _, value } => {
487cf647 26 this.as_constant(&this.thir[*value])
5e7ed085 27 }
487cf647
FG
28 _ => as_constant_inner(
29 expr,
30 |user_ty| {
31 Some(this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
9fa01778 32 span,
f2b60f7d 33 user_ty: user_ty.clone(),
9fa01778 34 inferred_ty: ty,
487cf647
FG
35 }))
36 },
37 tcx,
38 ),
39 }
40 }
41}
5e7ed085 42
487cf647
FG
43pub fn as_constant_inner<'tcx>(
44 expr: &Expr<'tcx>,
45 push_cuta: impl FnMut(&Box<CanonicalUserType<'tcx>>) -> Option<UserTypeAnnotationIndex>,
46 tcx: TyCtxt<'tcx>,
47) -> Constant<'tcx> {
48 let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
49 match *kind {
50 ExprKind::Literal { lit, neg } => {
51 let literal =
52 match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
53 Ok(c) => c,
54 Err(LitToConstError::Reported(guar)) => {
fe692bf9 55 ConstantKind::Ty(ty::Const::new_error(tcx, guar, ty))
487cf647
FG
56 }
57 Err(LitToConstError::TypeError) => {
9ffffee4 58 bug!("encountered type error in `lit_to_mir_constant`")
487cf647
FG
59 }
60 };
064997fb 61
487cf647
FG
62 Constant { span, user_ty: None, literal }
63 }
64 ExprKind::NonHirLiteral { lit, ref user_ty } => {
353b0b11 65 let user_ty = user_ty.as_ref().and_then(push_cuta);
f2b60f7d 66
487cf647 67 let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);
5e7ed085 68
9c376795 69 Constant { span, user_ty, literal }
487cf647
FG
70 }
71 ExprKind::ZstLiteral { ref user_ty } => {
353b0b11 72 let user_ty = user_ty.as_ref().and_then(push_cuta);
5e7ed085 73
487cf647 74 let literal = ConstantKind::Val(ConstValue::ZeroSized, ty);
5e7ed085 75
9c376795 76 Constant { span, user_ty, literal }
487cf647
FG
77 }
78 ExprKind::NamedConst { def_id, substs, ref user_ty } => {
353b0b11 79 let user_ty = user_ty.as_ref().and_then(push_cuta);
5099ac24 80
49aad941 81 let uneval = mir::UnevaluatedConst::new(def_id, substs);
487cf647
FG
82 let literal = ConstantKind::Unevaluated(uneval, ty);
83
84 Constant { user_ty, span, literal }
b039eaaf 85 }
487cf647 86 ExprKind::ConstParam { param, def_id: _ } => {
fe692bf9 87 let const_param = ty::Const::new_param(tcx, param, expr.ty);
487cf647
FG
88 let literal = ConstantKind::Ty(const_param);
89
90 Constant { user_ty: None, span, literal }
91 }
92 ExprKind::ConstBlock { did: def_id, substs } => {
49aad941 93 let uneval = mir::UnevaluatedConst::new(def_id, substs);
487cf647
FG
94 let literal = ConstantKind::Unevaluated(uneval, ty);
95
96 Constant { user_ty: None, span, literal }
97 }
98 ExprKind::StaticRef { alloc_id, ty, .. } => {
99 let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx));
100 let literal = ConstantKind::Val(const_val, ty);
101
102 Constant { span, user_ty: None, literal }
103 }
104 _ => span_bug!(span, "expression is not a valid constant {:?}", kind),
e9174d1e
SL
105 }
106}
5e7ed085 107
04454e1e 108#[instrument(skip(tcx, lit_input))]
fe692bf9 109fn lit_to_mir_constant<'tcx>(
5e7ed085
FG
110 tcx: TyCtxt<'tcx>,
111 lit_input: LitToConstInput<'tcx>,
112) -> Result<ConstantKind<'tcx>, LitToConstError> {
113 let LitToConstInput { lit, ty, neg } = lit_input;
114 let trunc = |n| {
115 let param_ty = ty::ParamEnv::reveal_all().and(ty);
487cf647
FG
116 let width = tcx
117 .layout_of(param_ty)
118 .map_err(|_| {
119 LitToConstError::Reported(tcx.sess.delay_span_bug(
120 DUMMY_SP,
121 format!("couldn't compute width of literal: {:?}", lit_input.lit),
122 ))
123 })?
124 .size;
5e7ed085
FG
125 trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
126 let result = width.truncate(n);
127 trace!("trunc result: {}", result);
128 Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
129 };
130
131 let value = match (lit, &ty.kind()) {
132 (ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
133 let s = s.as_str();
134 let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
9ffffee4 135 let allocation = tcx.mk_const_alloc(allocation);
5e7ed085
FG
136 ConstValue::Slice { data: allocation, start: 0, end: s.len() }
137 }
9c376795 138 (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _))
5e7ed085
FG
139 if matches!(inner_ty.kind(), ty::Slice(_)) =>
140 {
141 let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
9ffffee4 142 let allocation = tcx.mk_const_alloc(allocation);
5e7ed085
FG
143 ConstValue::Slice { data: allocation, start: 0, end: data.len() }
144 }
9c376795 145 (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
5e7ed085
FG
146 let id = tcx.allocate_bytes(data);
147 ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
148 }
49aad941
FG
149 (ast::LitKind::CStr(data, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if Some(def.did()) == tcx.lang_items().c_str()) =>
150 {
151 let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
152 let allocation = tcx.mk_const_alloc(allocation);
153 ConstValue::Slice { data: allocation, start: 0, end: data.len() }
154 }
5e7ed085
FG
155 (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
156 ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
157 }
158 (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
159 trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
160 }
487cf647
FG
161 (ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float_into_constval(*n, *fty, neg)
162 .ok_or_else(|| {
163 LitToConstError::Reported(tcx.sess.delay_span_bug(
164 DUMMY_SP,
165 format!("couldn't parse float literal: {:?}", lit_input.lit),
166 ))
167 })?,
5e7ed085
FG
168 (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
169 (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
487cf647
FG
170 (ast::LitKind::Err, _) => {
171 return Err(LitToConstError::Reported(
172 tcx.sess.delay_span_bug(DUMMY_SP, "encountered LitKind::Err during mir build"),
173 ));
174 }
5e7ed085
FG
175 _ => return Err(LitToConstError::TypeError),
176 };
177
178 Ok(ConstantKind::Val(value, ty))
179}