rustc_mir_build/builder/expr/
as_constant.rs

1//! See docs in build/expr/mod.rs
2
3use rustc_abi::Size;
4use rustc_ast as ast;
5use rustc_hir::LangItem;
6use rustc_middle::mir::interpret::{Allocation, CTFE_ALLOC_SALT, LitToConstInput, Scalar};
7use rustc_middle::mir::*;
8use rustc_middle::thir::*;
9use rustc_middle::ty::{
10    self, CanonicalUserType, CanonicalUserTypeAnnotation, Ty, TyCtxt, TypeVisitableExt as _,
11    UserTypeAnnotationIndex,
12};
13use rustc_middle::{bug, mir, span_bug};
14use tracing::{instrument, trace};
15
16use crate::builder::{Builder, parse_float_into_constval};
17
18impl<'a, 'tcx> Builder<'a, 'tcx> {
19    /// Compile `expr`, yielding a compile-time constant. Assumes that
20    /// `expr` is a valid compile-time constant!
21    pub(crate) fn as_constant(&mut self, expr: &Expr<'tcx>) -> ConstOperand<'tcx> {
22        let this = self;
23        let tcx = this.tcx;
24        let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
25        match kind {
26            ExprKind::Scope { region_scope: _, lint_level: _, value } => {
27                this.as_constant(&this.thir[*value])
28            }
29            _ => as_constant_inner(
30                expr,
31                |user_ty| {
32                    Some(this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
33                        span,
34                        user_ty: user_ty.clone(),
35                        inferred_ty: ty,
36                    }))
37                },
38                tcx,
39            ),
40        }
41    }
42}
43
44pub(crate) fn as_constant_inner<'tcx>(
45    expr: &Expr<'tcx>,
46    push_cuta: impl FnMut(&Box<CanonicalUserType<'tcx>>) -> Option<UserTypeAnnotationIndex>,
47    tcx: TyCtxt<'tcx>,
48) -> ConstOperand<'tcx> {
49    let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
50    match *kind {
51        ExprKind::Literal { lit, neg } => {
52            let const_ = lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg });
53
54            ConstOperand { span, user_ty: None, const_ }
55        }
56        ExprKind::NonHirLiteral { lit, ref user_ty } => {
57            let user_ty = user_ty.as_ref().and_then(push_cuta);
58
59            let const_ = Const::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);
60
61            ConstOperand { span, user_ty, const_ }
62        }
63        ExprKind::ZstLiteral { ref user_ty } => {
64            let user_ty = user_ty.as_ref().and_then(push_cuta);
65
66            let const_ = Const::Val(ConstValue::ZeroSized, ty);
67
68            ConstOperand { span, user_ty, const_ }
69        }
70        ExprKind::NamedConst { def_id, args, ref user_ty } => {
71            let user_ty = user_ty.as_ref().and_then(push_cuta);
72
73            let uneval = mir::UnevaluatedConst::new(def_id, args);
74            let const_ = Const::Unevaluated(uneval, ty);
75
76            ConstOperand { user_ty, span, const_ }
77        }
78        ExprKind::ConstParam { param, def_id: _ } => {
79            let const_param = ty::Const::new_param(tcx, param);
80            let const_ = Const::Ty(expr.ty, const_param);
81
82            ConstOperand { user_ty: None, span, const_ }
83        }
84        ExprKind::ConstBlock { did: def_id, args } => {
85            let uneval = mir::UnevaluatedConst::new(def_id, args);
86            let const_ = Const::Unevaluated(uneval, ty);
87
88            ConstOperand { user_ty: None, span, const_ }
89        }
90        ExprKind::StaticRef { alloc_id, ty, .. } => {
91            let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx));
92            let const_ = Const::Val(const_val, ty);
93
94            ConstOperand { span, user_ty: None, const_ }
95        }
96        _ => span_bug!(span, "expression is not a valid constant {:?}", kind),
97    }
98}
99
100#[instrument(skip(tcx, lit_input))]
101fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>) -> Const<'tcx> {
102    let LitToConstInput { lit, ty, neg } = lit_input;
103
104    if let Err(guar) = ty.error_reported() {
105        return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
106    }
107
108    let trunc = |n, width: ty::UintTy| {
109        let width = width
110            .normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
111            .bit_width()
112            .unwrap();
113        let width = Size::from_bits(width);
114        trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
115        let result = width.truncate(n);
116        trace!("trunc result: {}", result);
117        ConstValue::Scalar(Scalar::from_uint(result, width))
118    };
119
120    let value = match (lit, ty.kind()) {
121        (ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
122            let s = s.as_str();
123            let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
124            let allocation = tcx.mk_const_alloc(allocation);
125            ConstValue::Slice { data: allocation, meta: allocation.inner().size().bytes() }
126        }
127        (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _))
128            if matches!(inner_ty.kind(), ty::Slice(_)) =>
129        {
130            let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
131            let allocation = tcx.mk_const_alloc(allocation);
132            ConstValue::Slice { data: allocation, meta: allocation.inner().size().bytes() }
133        }
134        (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
135            let id = tcx.allocate_bytes_dedup(data, CTFE_ALLOC_SALT);
136            ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
137        }
138        (ast::LitKind::CStr(data, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::CStr)) =>
139        {
140            let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
141            let allocation = tcx.mk_const_alloc(allocation);
142            ConstValue::Slice { data: allocation, meta: allocation.inner().size().bytes() }
143        }
144        (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
145            ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
146        }
147        (ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => trunc(n.get(), *ui),
148        (ast::LitKind::Int(n, _), ty::Int(i)) => trunc(
149            if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
150            i.to_unsigned(),
151        ),
152        (ast::LitKind::Float(n, _), ty::Float(fty)) => {
153            parse_float_into_constval(*n, *fty, neg).unwrap()
154        }
155        (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
156        (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
157        (ast::LitKind::Err(guar), _) => {
158            return Const::Ty(Ty::new_error(tcx, *guar), ty::Const::new_error(tcx, *guar));
159        }
160        _ => bug!("invalid lit/ty combination in `lit_to_mir_constant`: {lit:?}: {ty:?}"),
161    };
162
163    Const::Val(value, ty)
164}