1use rustc_abi::Size;
4use rustc_ast as ast;
5use rustc_hir::LangItem;
6use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, Scalar};
7use rustc_middle::mir::*;
8use rustc_middle::thir::*;
9use rustc_middle::ty::{
10 self, CanonicalUserType, CanonicalUserTypeAnnotation, LitToConstInput, Ty, TyCtxt,
11 TypeVisitableExt as _, 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 pub(crate) fn as_constant(&mut self, expr: &Expr<'tcx>) -> ConstOperand<'tcx> {
22 let this = self; let tcx = this.tcx;
24 let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
25 match kind {
26 ExprKind::Scope { region_scope: _, hir_id: _, 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_scope_id: _, span, ref kind } = *expr;
50
51 match *kind {
52 ExprKind::Literal { lit, neg } => {
53 let const_ =
54 lit_to_mir_constant(tcx, LitToConstInput { lit: lit.node, ty: Some(ty), neg });
55
56 ConstOperand { span, user_ty: None, const_ }
57 }
58 ExprKind::NonHirLiteral { lit, ref user_ty } => {
59 let user_ty = user_ty.as_ref().and_then(push_cuta);
60
61 let const_ = Const::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);
62
63 ConstOperand { span, user_ty, const_ }
64 }
65 ExprKind::ZstLiteral { ref user_ty } => {
66 let user_ty = user_ty.as_ref().and_then(push_cuta);
67
68 let const_ = Const::Val(ConstValue::ZeroSized, ty);
69
70 ConstOperand { span, user_ty, const_ }
71 }
72 ExprKind::NamedConst { def_id, args, ref user_ty } => {
73 let user_ty = user_ty.as_ref().and_then(push_cuta);
74 if tcx.is_type_const(def_id) {
75 let uneval = ty::UnevaluatedConst::new(def_id, args);
76 let ct = ty::Const::new_unevaluated(tcx, uneval);
77
78 let const_ = Const::Ty(ty, ct);
79 return ConstOperand { span, user_ty, const_ };
80 }
81
82 let uneval = mir::UnevaluatedConst::new(def_id, args);
83 let const_ = Const::Unevaluated(uneval, ty);
84
85 ConstOperand { user_ty, span, const_ }
86 }
87 ExprKind::ConstParam { param, def_id: _ } => {
88 let const_param = ty::Const::new_param(tcx, param);
89 let const_ = Const::Ty(expr.ty, const_param);
90
91 ConstOperand { user_ty: None, span, const_ }
92 }
93 ExprKind::ConstBlock { did: def_id, args } => {
94 let uneval = mir::UnevaluatedConst::new(def_id, args);
95 let const_ = Const::Unevaluated(uneval, ty);
96
97 ConstOperand { user_ty: None, span, const_ }
98 }
99 ExprKind::StaticRef { alloc_id, ty, .. } => {
100 let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx));
101 let const_ = Const::Val(const_val, ty);
102
103 ConstOperand { span, user_ty: None, const_ }
104 }
105 _ => ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("expression is not a valid constant {0:?}", kind))span_bug!(span, "expression is not a valid constant {:?}", kind),
106 }
107}
108
109#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lit_to_mir_constant",
"rustc_mir_build::builder::expr::as_constant",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/expr/as_constant.rs"),
::tracing_core::__macro_support::Option::Some(109u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::expr::as_constant"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Const<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let LitToConstInput { lit, ty, neg } = lit_input;
let ty = ty.expect("type of literal must be known at this point");
if let Err(guar) = ty.error_reported() {
return Const::Ty(Ty::new_error(tcx, guar),
ty::Const::new_error(tcx, guar));
}
let lit_ty =
match *ty.kind() { ty::Pat(base, _) => base, _ => ty, };
let trunc =
|n|
{
let width = lit_ty.primitive_size(tcx);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/expr/as_constant.rs:126",
"rustc_mir_build::builder::expr::as_constant",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/expr/as_constant.rs"),
::tracing_core::__macro_support::Option::Some(126u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::expr::as_constant"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("trunc {0} with size {1} and shift {2}",
n, width.bits(), 128 - width.bits()) as &dyn Value))])
});
} else { ; }
};
let result = width.truncate(n);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/expr/as_constant.rs:128",
"rustc_mir_build::builder::expr::as_constant",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/expr/as_constant.rs"),
::tracing_core::__macro_support::Option::Some(128u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::expr::as_constant"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("trunc result: {0}",
result) as &dyn Value))])
});
} else { ; }
};
ConstValue::Scalar(Scalar::from_uint(result, width))
};
let value =
match (lit, lit_ty.kind()) {
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if
inner_ty.is_str() => {
let s = s.as_str().as_bytes();
let len = s.len();
let allocation =
tcx.allocate_bytes_dedup(s, CTFE_ALLOC_SALT);
ConstValue::Slice {
alloc_id: allocation,
meta: len.try_into().unwrap(),
}
}
(ast::LitKind::ByteStr(byte_sym, _),
ty::Ref(_, inner_ty, _)) if
#[allow(non_exhaustive_omitted_patterns)] match inner_ty.kind()
{
ty::Slice(_) => true,
_ => false,
} => {
let data = byte_sym.as_byte_str();
let len = data.len();
let allocation =
tcx.allocate_bytes_dedup(data, CTFE_ALLOC_SALT);
ConstValue::Slice {
alloc_id: allocation,
meta: len.try_into().unwrap(),
}
}
(ast::LitKind::ByteStr(byte_sym, _),
ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
let id =
tcx.allocate_bytes_dedup(byte_sym.as_byte_str(),
CTFE_ALLOC_SALT);
ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
}
(ast::LitKind::CStr(byte_sym, _), ty::Ref(_, inner_ty, _))
if
#[allow(non_exhaustive_omitted_patterns)] match inner_ty.kind()
{
ty::Adt(def, _) if
tcx.is_lang_item(def.did(), LangItem::CStr) => true,
_ => false,
} => {
let data = byte_sym.as_byte_str();
let len = data.len();
let allocation =
tcx.allocate_bytes_dedup(data, CTFE_ALLOC_SALT);
ConstValue::Slice {
alloc_id: allocation,
meta: len.try_into().unwrap(),
}
}
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
ConstValue::Scalar(Scalar::from_uint(n,
Size::from_bytes(1)))
}
(ast::LitKind::Int(n, _), ty::Uint(_)) if !neg =>
trunc(n.get()),
(ast::LitKind::Int(n, _), ty::Int(_)) => {
trunc(if neg {
u128::wrapping_neg(n.get())
} else { n.get() })
}
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
parse_float_into_constval(n, *fty, neg).unwrap()
}
(ast::LitKind::Bool(b), ty::Bool) =>
ConstValue::Scalar(Scalar::from_bool(b)),
(ast::LitKind::Char(c), ty::Char) =>
ConstValue::Scalar(Scalar::from_char(c)),
(ast::LitKind::Err(guar), _) => {
return Const::Ty(Ty::new_error(tcx, guar),
ty::Const::new_error(tcx, guar));
}
_ =>
::rustc_middle::util::bug::bug_fmt(format_args!("invalid lit/ty combination in `lit_to_mir_constant`: {0:?}: {1:?}",
lit, ty)),
};
Const::Val(value, ty)
}
}
}#[instrument(skip(tcx, lit_input))]
110fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>) -> Const<'tcx> {
111 let LitToConstInput { lit, ty, neg } = lit_input;
112
113 let ty = ty.expect("type of literal must be known at this point");
114
115 if let Err(guar) = ty.error_reported() {
116 return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
117 }
118
119 let lit_ty = match *ty.kind() {
120 ty::Pat(base, _) => base,
121 _ => ty,
122 };
123
124 let trunc = |n| {
125 let width = lit_ty.primitive_size(tcx);
126 trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
127 let result = width.truncate(n);
128 trace!("trunc result: {}", result);
129 ConstValue::Scalar(Scalar::from_uint(result, width))
130 };
131
132 let value = match (lit, lit_ty.kind()) {
133 (ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
134 let s = s.as_str().as_bytes();
135 let len = s.len();
136 let allocation = tcx.allocate_bytes_dedup(s, CTFE_ALLOC_SALT);
137 ConstValue::Slice { alloc_id: allocation, meta: len.try_into().unwrap() }
138 }
139 (ast::LitKind::ByteStr(byte_sym, _), ty::Ref(_, inner_ty, _))
140 if matches!(inner_ty.kind(), ty::Slice(_)) =>
141 {
142 let data = byte_sym.as_byte_str();
143 let len = data.len();
144 let allocation = tcx.allocate_bytes_dedup(data, CTFE_ALLOC_SALT);
145 ConstValue::Slice { alloc_id: allocation, meta: len.try_into().unwrap() }
146 }
147 (ast::LitKind::ByteStr(byte_sym, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
148 let id = tcx.allocate_bytes_dedup(byte_sym.as_byte_str(), CTFE_ALLOC_SALT);
149 ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
150 }
151 (ast::LitKind::CStr(byte_sym, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::CStr)) =>
152 {
153 let data = byte_sym.as_byte_str();
154 let len = data.len();
155 let allocation = tcx.allocate_bytes_dedup(data, CTFE_ALLOC_SALT);
156 ConstValue::Slice { alloc_id: allocation, meta: len.try_into().unwrap() }
157 }
158 (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
159 ConstValue::Scalar(Scalar::from_uint(n, Size::from_bytes(1)))
160 }
161 (ast::LitKind::Int(n, _), ty::Uint(_)) if !neg => trunc(n.get()),
162 (ast::LitKind::Int(n, _), ty::Int(_)) => {
163 trunc(if neg { u128::wrapping_neg(n.get()) } else { n.get() })
166 }
167 (ast::LitKind::Float(n, _), ty::Float(fty)) => {
168 parse_float_into_constval(n, *fty, neg).unwrap()
169 }
170 (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(b)),
171 (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(c)),
172 (ast::LitKind::Err(guar), _) => {
173 return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
174 }
175 _ => bug!("invalid lit/ty combination in `lit_to_mir_constant`: {lit:?}: {ty:?}"),
176 };
177
178 Const::Val(value, ty)
179}