2018-08-24 16:39:25 +02:00
|
|
|
// Not in interpret to make sure we do not use private implementation details
|
|
|
|
|
|
2024-03-23 10:44:28 +01:00
|
|
|
use rustc_middle::query::{Key, TyCtxtAt};
|
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
|
|
|
|
use rustc_middle::{bug, mir};
|
|
|
|
|
use rustc_target::abi::VariantIdx;
|
2024-05-22 14:20:23 +10:00
|
|
|
use tracing::instrument;
|
2017-12-12 17:14:49 +01:00
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
use crate::interpret::InterpCx;
|
2024-01-06 13:48:48 +01:00
|
|
|
|
2024-03-20 17:45:14 +00:00
|
|
|
mod dummy_machine;
|
2019-12-25 01:04:32 +01:00
|
|
|
mod error;
|
2019-12-23 12:55:16 +01:00
|
|
|
mod eval_queries;
|
2020-01-01 18:06:00 +01:00
|
|
|
mod fn_queries;
|
2019-12-25 01:28:30 +01:00
|
|
|
mod machine;
|
2022-04-05 16:33:42 +02:00
|
|
|
mod valtrees;
|
2019-12-25 01:04:32 +01:00
|
|
|
|
2024-09-23 11:57:12 +02:00
|
|
|
pub use self::dummy_machine::*;
|
|
|
|
|
pub use self::error::*;
|
|
|
|
|
pub use self::eval_queries::*;
|
|
|
|
|
pub use self::fn_queries::*;
|
|
|
|
|
pub use self::machine::*;
|
|
|
|
|
pub(crate) use self::valtrees::{eval_to_valtree, valtree_to_const_value};
|
2018-09-20 11:57:45 +02:00
|
|
|
|
2022-06-02 19:42:29 +02:00
|
|
|
// We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.
|
|
|
|
|
const VALTREE_MAX_NODES: usize = 100000;
|
|
|
|
|
|
2024-07-13 16:13:55 +02:00
|
|
|
pub(crate) enum ValTreeCreationError<'tcx> {
|
2022-06-02 19:42:29 +02:00
|
|
|
NodesOverflow,
|
2024-01-05 12:18:11 +01:00
|
|
|
/// Values of this type, or this particular value, are not supported as valtrees.
|
2024-07-13 16:13:55 +02:00
|
|
|
NonSupportedType(Ty<'tcx>),
|
2022-06-02 19:42:29 +02:00
|
|
|
}
|
2024-07-13 16:13:55 +02:00
|
|
|
pub(crate) type ValTreeCreationResult<'tcx> = Result<ty::ValTree<'tcx>, ValTreeCreationError<'tcx>>;
|
2022-06-02 19:42:29 +02:00
|
|
|
|
2022-04-12 18:14:28 +02:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
2023-10-31 17:38:41 +00:00
|
|
|
pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
|
2023-09-22 09:14:39 +00:00
|
|
|
tcx: TyCtxtAt<'tcx>,
|
2023-09-16 09:36:22 +02:00
|
|
|
val: mir::ConstValue<'tcx>,
|
2023-06-28 09:23:34 +00:00
|
|
|
ty: Ty<'tcx>,
|
2023-06-28 13:22:02 +00:00
|
|
|
) -> Option<mir::DestructuredConstant<'tcx>> {
|
2023-06-28 09:23:34 +00:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2024-02-21 14:18:06 +01:00
|
|
|
let (ecx, op) = mk_eval_cx_for_const_val(tcx, param_env, val, ty)?;
|
2022-04-12 18:14:28 +02:00
|
|
|
|
|
|
|
|
// We go to `usize` as we cannot allocate anything bigger anyway.
|
2023-06-28 09:23:34 +00:00
|
|
|
let (field_count, variant, down) = match ty.kind() {
|
2024-09-27 12:56:51 -04:00
|
|
|
ty::Array(_, len) => (len.try_to_target_usize(tcx.tcx)? as usize, None, op),
|
2022-04-12 18:14:28 +02:00
|
|
|
ty::Adt(def, _) if def.variants().is_empty() => {
|
2023-06-28 13:22:02 +00:00
|
|
|
return None;
|
2022-04-12 18:14:28 +02:00
|
|
|
}
|
|
|
|
|
ty::Adt(def, _) => {
|
2024-09-29 11:53:23 +02:00
|
|
|
let variant = ecx.read_discriminant(&op).discard_err()?;
|
|
|
|
|
let down = ecx.project_downcast(&op, variant).discard_err()?;
|
2022-04-12 18:14:28 +02:00
|
|
|
(def.variants()[variant].fields.len(), Some(variant), down)
|
|
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Tuple(args) => (args.len(), None, op),
|
2022-04-12 18:14:28 +02:00
|
|
|
_ => bug!("cannot destructure mir constant {:?}", val),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let fields_iter = (0..field_count)
|
|
|
|
|
.map(|i| {
|
2024-09-29 11:53:23 +02:00
|
|
|
let field_op = ecx.project_field(&down, i).discard_err()?;
|
2023-11-01 18:36:38 +00:00
|
|
|
let val = op_to_const(&ecx, &field_op, /* for diagnostics */ true);
|
2023-06-28 13:22:02 +00:00
|
|
|
Some((val, field_op.layout.ty))
|
2022-04-12 18:14:28 +02:00
|
|
|
})
|
2023-06-28 13:22:02 +00:00
|
|
|
.collect::<Option<Vec<_>>>()?;
|
2022-04-12 18:14:28 +02:00
|
|
|
let fields = tcx.arena.alloc_from_iter(fields_iter);
|
|
|
|
|
|
2023-06-28 13:22:02 +00:00
|
|
|
Some(mir::DestructuredConstant { variant, fields })
|
2022-04-12 18:14:28 +02:00
|
|
|
}
|
2024-03-23 10:44:28 +01:00
|
|
|
|
|
|
|
|
/// Computes the tag (if any) for a given type and variant.
|
|
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
|
|
|
|
pub fn tag_for_variant_provider<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
(ty, variant_index): (Ty<'tcx>, VariantIdx),
|
|
|
|
|
) -> Option<ty::ScalarInt> {
|
|
|
|
|
assert!(ty.is_enum());
|
|
|
|
|
|
|
|
|
|
let ecx = InterpCx::new(
|
|
|
|
|
tcx,
|
|
|
|
|
ty.default_span(tcx),
|
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
|
crate::const_eval::DummyMachine,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ecx.tag_for_variant(ty, variant_index).unwrap().map(|(tag, _tag_field)| tag)
|
|
|
|
|
}
|