rustc_const_eval/const_eval/
mod.rs1use rustc_abi::{FieldIdx, VariantIdx};
4use rustc_middle::mir;
5use rustc_middle::ty::consts::ConstExt;
6use rustc_middle::ty::{self, Ty, TyCtxt};
7use rustc_span::{DUMMY_SP, bug};
8use tracing::instrument;
9
10use crate::interpret::InterpCx;
11
12mod dummy_machine;
13mod dyn_trait;
14mod error;
15mod eval_queries;
16mod fn_queries;
17mod machine;
18mod type_info;
19mod valtrees;
20
21pub use self::dummy_machine::*;
22pub use self::error::*;
23pub use self::eval_queries::*;
24pub use self::fn_queries::*;
25pub use self::machine::*;
26pub(crate) use self::valtrees::{eval_to_valtree, valtree_to_const_value};
27
28const VALTREE_MAX_NODES: usize = 100000;
30
31{}
#[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("/rustc-dev/7bbda45cb1ec0a378d91959646a8fa904be3192a/compiler/rustc_const_eval/src/const_eval/mod.rs"),
::tracing_core::__macro_support::Option::Some(31u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::const_eval"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("val")
}> =
::tracing::__macro_support::FieldName::new("val");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("ty")
}> =
::tracing::__macro_support::FieldName::new("ty");
NAME.as_str()
}], ::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&val)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
as &dyn ::tracing::field::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_span::macros::bug_impl(None,
format_args!("cannot destructure mir constant {0:?}", val),
Location::caller()),
};
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")]
32pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
33 tcx: TyCtxt<'tcx>,
34 val: mir::ConstValue,
35 ty: Ty<'tcx>,
36) -> Option<mir::DestructuredConstant<'tcx>> {
37 let typing_env = ty::TypingEnv::fully_monomorphized();
38 let (ecx, op) = mk_eval_cx_for_const_val(tcx.at(rustc_span::DUMMY_SP), typing_env, val, ty)?;
40
41 let (field_count, variant, down) = match ty.kind() {
43 ty::Array(_, len) => (len.try_to_target_usize(tcx)? as usize, None, op),
44 ty::Adt(def, _) if def.variants().is_empty() => {
45 return None;
46 }
47 ty::Adt(def, _) => {
48 let variant = ecx.read_discriminant(&op).discard_err()?;
49 let down = ecx.project_downcast(&op, variant).discard_err()?;
50 (def.variants()[variant].fields.len(), Some(variant), down)
51 }
52 ty::Tuple(args) => (args.len(), None, op),
53 _ => bug!("cannot destructure mir constant {:?}", val),
54 };
55
56 let fields_iter = (0..field_count)
57 .map(|i| {
58 let field_op = ecx.project_field(&down, FieldIdx::from_usize(i)).discard_err()?;
59 let val = op_to_const(&ecx, &field_op, true);
60 Some((val, field_op.layout.ty))
61 })
62 .collect::<Option<Vec<_>>>()?;
63 let fields = tcx.arena.alloc_from_iter(fields_iter);
64
65 Some(mir::DestructuredConstant { variant, fields })
66}
67
68{}
#[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("/rustc-dev/7bbda45cb1ec0a378d91959646a8fa904be3192a/compiler/rustc_const_eval/src/const_eval/mod.rs"),
::tracing_core::__macro_support::Option::Some(69u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::const_eval"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("key")
}> =
::tracing::__macro_support::FieldName::new("key");
NAME.as_str()
}], ::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&key)
as &dyn ::tracing::field::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")]
70pub fn tag_for_variant_provider<'tcx>(
71 tcx: TyCtxt<'tcx>,
72 key: ty::PseudoCanonicalInput<'tcx, (Ty<'tcx>, VariantIdx)>,
73) -> Option<ty::ScalarInt> {
74 let (ty, variant_index) = key.value;
75 assert!(ty.is_enum());
76
77 let ecx = InterpCx::new(tcx, DUMMY_SP, key.typing_env, crate::const_eval::DummyMachine);
78
79 let layout = ecx.layout_of(ty).unwrap();
80 ecx.tag_for_variant(layout, variant_index).unwrap().map(|(tag, _tag_field)| tag)
81}