]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/build/expr/as_constant.rs
New upstream version 1.63.0+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;
5e7ed085 5use rustc_hir::def_id::DefId;
923072b8
FG
6use rustc_middle::mir::interpret::{
7 Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
8};
ba9703b0 9use rustc_middle::mir::*;
17df50a5 10use rustc_middle::thir::*;
5e7ed085
FG
11use rustc_middle::ty::subst::SubstsRef;
12use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt};
13use rustc_target::abi::Size;
e9174d1e 14
dc9dc135 15impl<'a, 'tcx> Builder<'a, 'tcx> {
e9174d1e
SL
16 /// Compile `expr`, yielding a compile-time constant. Assumes that
17 /// `expr` is a valid compile-time constant!
923072b8 18 pub(crate) fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
5e7ed085
FG
19 let create_uneval_from_def_id =
20 |tcx: TyCtxt<'tcx>, def_id: DefId, ty: Ty<'tcx>, substs: SubstsRef<'tcx>| {
21 let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs);
923072b8 22 tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Unevaluated(uneval), ty })
5e7ed085
FG
23 };
24
e9174d1e 25 let this = self;
5e7ed085 26 let tcx = this.tcx;
6a06907d
XL
27 let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
28 match *kind {
17df50a5
XL
29 ExprKind::Scope { region_scope: _, lint_level: _, value } => {
30 this.as_constant(&this.thir[value])
31 }
5e7ed085
FG
32 ExprKind::Literal { lit, neg } => {
33 let literal =
04454e1e 34 match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
5e7ed085
FG
35 Ok(c) => c,
36 Err(LitToConstError::Reported) => ConstantKind::Ty(tcx.const_error(ty)),
37 Err(LitToConstError::TypeError) => {
04454e1e 38 bug!("encountered type error in `lit_to_mir_constant")
5e7ed085
FG
39 }
40 };
41
04454e1e 42 Constant { span, user_ty: None, literal }
5e7ed085
FG
43 }
44 ExprKind::NonHirLiteral { lit, user_ty } => {
9fa01778
XL
45 let user_ty = user_ty.map(|user_ty| {
46 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
47 span,
48 user_ty,
49 inferred_ty: ty,
50 })
0731742a 51 });
5e7ed085
FG
52
53 let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);
54
55 Constant { span, user_ty: user_ty, literal }
56 }
57 ExprKind::NamedConst { def_id, substs, user_ty } => {
58 let user_ty = user_ty.map(|user_ty| {
59 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
60 span,
61 user_ty,
62 inferred_ty: ty,
63 })
64 });
65 let literal = ConstantKind::Ty(create_uneval_from_def_id(tcx, def_id, ty, substs));
66
67 Constant { user_ty, span, literal }
68 }
69 ExprKind::ConstParam { param, def_id: _ } => {
70 let const_param =
923072b8 71 tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Param(param), ty: expr.ty });
5e7ed085
FG
72 let literal = ConstantKind::Ty(const_param);
73
74 Constant { user_ty: None, span, literal }
75 }
76 ExprKind::ConstBlock { did: def_id, substs } => {
77 let literal = ConstantKind::Ty(create_uneval_from_def_id(tcx, def_id, ty, substs));
78
79 Constant { user_ty: None, span, literal }
6a06907d 80 }
5099ac24 81 ExprKind::StaticRef { alloc_id, ty, .. } => {
5e7ed085 82 let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx));
5099ac24
FG
83 let literal = ConstantKind::Val(const_val, ty);
84
85 Constant { span, user_ty: None, literal }
6a06907d 86 }
b7449926 87 _ => span_bug!(span, "expression is not a valid constant {:?}", kind),
b039eaaf 88 }
e9174d1e
SL
89 }
90}
5e7ed085 91
04454e1e 92#[instrument(skip(tcx, lit_input))]
923072b8 93pub(crate) fn lit_to_mir_constant<'tcx>(
5e7ed085
FG
94 tcx: TyCtxt<'tcx>,
95 lit_input: LitToConstInput<'tcx>,
96) -> Result<ConstantKind<'tcx>, LitToConstError> {
97 let LitToConstInput { lit, ty, neg } = lit_input;
98 let trunc = |n| {
99 let param_ty = ty::ParamEnv::reveal_all().and(ty);
100 let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
101 trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
102 let result = width.truncate(n);
103 trace!("trunc result: {}", result);
104 Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
105 };
106
107 let value = match (lit, &ty.kind()) {
108 (ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
109 let s = s.as_str();
110 let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
111 let allocation = tcx.intern_const_alloc(allocation);
112 ConstValue::Slice { data: allocation, start: 0, end: s.len() }
113 }
114 (ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _))
115 if matches!(inner_ty.kind(), ty::Slice(_)) =>
116 {
117 let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
118 let allocation = tcx.intern_const_alloc(allocation);
119 ConstValue::Slice { data: allocation, start: 0, end: data.len() }
120 }
121 (ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
122 let id = tcx.allocate_bytes(data);
123 ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
124 }
125 (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
126 ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
127 }
128 (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
129 trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
130 }
131 (ast::LitKind::Float(n, _), ty::Float(fty)) => {
923072b8 132 parse_float_into_constval(*n, *fty, neg).ok_or(LitToConstError::Reported)?
5e7ed085
FG
133 }
134 (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
135 (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
136 (ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
137 _ => return Err(LitToConstError::TypeError),
138 };
139
140 Ok(ConstantKind::Val(value, ty))
141}