rustc_const_eval/const_eval/
mod.rs1use rustc_abi::{FieldIdx, VariantIdx};
4use rustc_middle::ty::{self, Ty, TyCtxt};
5use rustc_middle::{bug, mir};
6use rustc_span::DUMMY_SP;
7use tracing::instrument;
8
9use crate::interpret::InterpCx;
10
11mod dummy_machine;
12mod dyn_trait;
13mod error;
14mod eval_queries;
15mod fn_queries;
16mod machine;
17mod type_info;
18mod valtrees;
19
20pub use self::dummy_machine::*;
21pub use self::error::*;
22pub use self::eval_queries::*;
23pub use self::fn_queries::*;
24pub use self::machine::*;
25pub(crate) use self::valtrees::{eval_to_valtree, valtree_to_const_value};
26
27const VALTREE_MAX_NODES: usize = 100000;
29
30#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::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("try_destructure_mir_constant_for_user_output",
"rustc_const_eval::const_eval", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/const_eval/mod.rs"),
::tracing_core::__macro_support::Option::Some(30u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::const_eval"),
::tracing_core::field::FieldSet::new(&["val", "ty"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::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,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&val)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
as &dyn Value))])
})
} 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:
Option<mir::DestructuredConstant<'tcx>> = loop {};
return __tracing_attr_fake_return;
}
{
let typing_env = ty::TypingEnv::fully_monomorphized();
let (ecx, op) =
mk_eval_cx_for_const_val(tcx.at(rustc_span::DUMMY_SP),
typing_env, val, ty)?;
let (field_count, variant, down) =
match ty.kind() {
ty::Array(_, len) =>
(len.try_to_target_usize(tcx)? as usize, None, op),
ty::Adt(def, _) if def.variants().is_empty() => {
return None;
}
ty::Adt(def, _) => {
let variant = ecx.read_discriminant(&op).discard_err()?;
let down =
ecx.project_downcast(&op, variant).discard_err()?;
(def.variants()[variant].fields.len(), Some(variant), down)
}
ty::Tuple(args) => (args.len(), None, op),
_ =>
::rustc_middle::util::bug::bug_fmt(format_args!("cannot destructure mir constant {0:?}",
val)),
};
let fields_iter =
(0..field_count).map(|i|
{
let field_op =
ecx.project_field(&down,
FieldIdx::from_usize(i)).discard_err()?;
let val = op_to_const(&ecx, &field_op, true);
Some((val, field_op.layout.ty))
}).collect::<Option<Vec<_>>>()?;
let fields = tcx.arena.alloc_from_iter(fields_iter);
Some(mir::DestructuredConstant { variant, fields })
}
}
}#[instrument(skip(tcx), level = "debug")]
31pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
32 tcx: TyCtxt<'tcx>,
33 val: mir::ConstValue,
34 ty: Ty<'tcx>,
35) -> Option<mir::DestructuredConstant<'tcx>> {
36 let typing_env = ty::TypingEnv::fully_monomorphized();
37 let (ecx, op) = mk_eval_cx_for_const_val(tcx.at(rustc_span::DUMMY_SP), typing_env, val, ty)?;
39
40 let (field_count, variant, down) = match ty.kind() {
42 ty::Array(_, len) => (len.try_to_target_usize(tcx)? as usize, None, op),
43 ty::Adt(def, _) if def.variants().is_empty() => {
44 return None;
45 }
46 ty::Adt(def, _) => {
47 let variant = ecx.read_discriminant(&op).discard_err()?;
48 let down = ecx.project_downcast(&op, variant).discard_err()?;
49 (def.variants()[variant].fields.len(), Some(variant), down)
50 }
51 ty::Tuple(args) => (args.len(), None, op),
52 _ => bug!("cannot destructure mir constant {:?}", val),
53 };
54
55 let fields_iter = (0..field_count)
56 .map(|i| {
57 let field_op = ecx.project_field(&down, FieldIdx::from_usize(i)).discard_err()?;
58 let val = op_to_const(&ecx, &field_op, true);
59 Some((val, field_op.layout.ty))
60 })
61 .collect::<Option<Vec<_>>>()?;
62 let fields = tcx.arena.alloc_from_iter(fields_iter);
63
64 Some(mir::DestructuredConstant { variant, fields })
65}
66
67#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::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("tag_for_variant_provider",
"rustc_const_eval::const_eval", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/const_eval/mod.rs"),
::tracing_core::__macro_support::Option::Some(68u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::const_eval"),
::tracing_core::field::FieldSet::new(&["key"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::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,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&key)
as &dyn Value))])
})
} 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: Option<ty::ScalarInt> = loop {};
return __tracing_attr_fake_return;
}
{
let (ty, variant_index) = key.value;
if !ty.is_enum() {
::core::panicking::panic("assertion failed: ty.is_enum()")
};
let ecx =
InterpCx::new(tcx, DUMMY_SP, key.typing_env,
crate::const_eval::DummyMachine);
let layout = ecx.layout_of(ty).unwrap();
ecx.tag_for_variant(layout,
variant_index).unwrap().map(|(tag, _tag_field)| tag)
}
}
}#[instrument(skip(tcx), level = "debug")]
69pub fn tag_for_variant_provider<'tcx>(
70 tcx: TyCtxt<'tcx>,
71 key: ty::PseudoCanonicalInput<'tcx, (Ty<'tcx>, VariantIdx)>,
72) -> Option<ty::ScalarInt> {
73 let (ty, variant_index) = key.value;
74 assert!(ty.is_enum());
75
76 let ecx = InterpCx::new(tcx, DUMMY_SP, key.typing_env, crate::const_eval::DummyMachine);
77
78 let layout = ecx.layout_of(ty).unwrap();
79 ecx.tag_for_variant(layout, variant_index).unwrap().map(|(tag, _tag_field)| tag)
80}