2024-08-05 17:34:44 +02:00
|
|
|
use either::{Left, Right};
|
2024-11-02 19:32:52 -07:00
|
|
|
use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
|
2024-06-18 10:35:56 +00:00
|
|
|
use rustc_errors::DiagCtxtHandle;
|
2020-12-09 10:50:34 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2024-07-17 11:48:22 +02:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
|
|
|
use rustc_infer::infer::at::ToTrace;
|
2024-10-31 12:41:50 +01:00
|
|
|
use rustc_infer::traits::{ObligationCause, Reveal};
|
2024-08-05 17:34:44 +02:00
|
|
|
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
|
2023-05-16 01:53:21 +02:00
|
|
|
use rustc_middle::query::TyCtxtAt;
|
2021-11-28 19:35:50 -05:00
|
|
|
use rustc_middle::ty::layout::{
|
2024-08-05 17:34:44 +02:00
|
|
|
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
|
2021-11-28 19:35:50 -05:00
|
|
|
};
|
2024-11-15 13:53:31 +01:00
|
|
|
use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypingEnv, Variance};
|
2024-08-05 17:34:44 +02:00
|
|
|
use rustc_middle::{mir, span_bug};
|
2021-06-25 18:48:26 -05:00
|
|
|
use rustc_session::Limit;
|
2022-11-15 15:44:33 +00:00
|
|
|
use rustc_span::Span;
|
2024-11-02 19:32:52 -07:00
|
|
|
use rustc_target::callconv::FnAbi;
|
2024-07-17 11:48:22 +02:00
|
|
|
use rustc_trait_selection::traits::ObligationCtxt;
|
2024-09-11 13:32:53 -04:00
|
|
|
use tracing::{debug, instrument, trace};
|
2018-06-08 03:47:26 +01:00
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
use super::{
|
2024-10-19 08:49:13 +02:00
|
|
|
Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
|
2024-09-29 11:53:23 +02:00
|
|
|
MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
|
|
|
|
err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
|
2018-08-13 16:14:22 +02:00
|
|
|
};
|
2024-08-05 17:34:44 +02:00
|
|
|
use crate::{ReportErrorExt, fluent_generated as fluent, util};
|
2016-06-01 17:05:20 +02:00
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
pub struct InterpCx<'tcx, M: Machine<'tcx>> {
|
2017-12-06 14:23:32 +02:00
|
|
|
/// Stores the `Machine` instance.
|
2020-04-14 14:40:08 -07:00
|
|
|
///
|
|
|
|
/// Note: the stack is provided by the machine.
|
2017-12-06 14:23:32 +02:00
|
|
|
pub machine: M,
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2016-03-14 21:18:39 -06:00
|
|
|
/// The results of the type checker, from rustc.
|
2020-06-14 15:02:51 +02:00
|
|
|
/// The span in this is the "root" of the evaluation, i.e., the const
|
2020-06-01 10:15:17 +02:00
|
|
|
/// we are evaluating (if this is CTFE).
|
2019-06-14 00:48:52 +03:00
|
|
|
pub tcx: TyCtxtAt<'tcx>,
|
2016-03-14 21:18:39 -06:00
|
|
|
|
2017-12-06 14:12:05 +02:00
|
|
|
/// Bounds in scope for polymorphic evaluations.
|
2018-09-20 10:22:11 +02:00
|
|
|
pub(crate) param_env: ty::ParamEnv<'tcx>,
|
2017-12-06 14:12:05 +02:00
|
|
|
|
2018-06-22 12:36:54 -07:00
|
|
|
/// The virtual memory system.
|
2024-05-27 08:24:23 +02:00
|
|
|
pub memory: Memory<'tcx, M>,
|
2021-06-25 18:48:26 -05:00
|
|
|
|
|
|
|
/// The recursion limit (cached from `tcx.recursion_limit(())`)
|
|
|
|
pub recursion_limit: Limit,
|
2016-05-09 18:21:21 -06:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'tcx, M: Machine<'tcx>> HasDataLayout for InterpCx<'tcx, M> {
|
2017-12-06 09:25:29 +01:00
|
|
|
#[inline]
|
2020-03-31 18:16:47 +02:00
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
2017-12-06 09:25:29 +01:00
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
2024-05-27 08:24:23 +02:00
|
|
|
M: Machine<'tcx>,
|
2018-08-22 16:59:14 -03:00
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
#[inline]
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2018-02-06 18:33:59 +01:00
|
|
|
*self.tcx
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
2024-05-27 08:24:23 +02:00
|
|
|
M: Machine<'tcx>,
|
2019-05-04 15:02:22 +05:30
|
|
|
{
|
2024-11-15 13:53:31 +01:00
|
|
|
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
|
|
|
|
self.typing_env()
|
2019-05-04 15:02:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
|
2024-10-19 08:49:13 +02:00
|
|
|
type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2018-08-19 17:01:31 +02:00
|
|
|
#[inline]
|
2021-08-30 18:01:58 +03:00
|
|
|
fn layout_tcx_at_span(&self) -> Span {
|
2022-06-04 15:29:51 -04:00
|
|
|
// Using the cheap root span for performance.
|
2021-08-30 18:01:58 +03:00
|
|
|
self.tcx.span
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-10-19 08:49:13 +02:00
|
|
|
fn handle_layout_err(
|
|
|
|
&self,
|
|
|
|
err: LayoutError<'tcx>,
|
|
|
|
_: Span,
|
|
|
|
_: Ty<'tcx>,
|
|
|
|
) -> InterpErrorKind<'tcx> {
|
2024-09-29 11:53:23 +02:00
|
|
|
err_inval!(Layout(err))
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
|
2024-10-19 08:49:13 +02:00
|
|
|
type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
|
2021-11-28 19:35:50 -05:00
|
|
|
|
|
|
|
fn handle_fn_abi_err(
|
|
|
|
&self,
|
|
|
|
err: FnAbiError<'tcx>,
|
|
|
|
_span: Span,
|
|
|
|
_fn_abi_request: FnAbiRequest<'tcx>,
|
2024-10-19 08:49:13 +02:00
|
|
|
) -> InterpErrorKind<'tcx> {
|
2021-11-28 19:35:50 -05:00
|
|
|
match err {
|
2024-09-29 11:53:23 +02:00
|
|
|
FnAbiError::Layout(err) => err_inval!(Layout(err)),
|
2021-12-11 18:45:03 -05:00
|
|
|
FnAbiError::AdjustForForeignAbi(err) => {
|
2024-09-29 11:53:23 +02:00
|
|
|
err_inval!(FnAbiAdjustForForeignAbi(err))
|
2021-12-11 18:45:03 -05:00
|
|
|
}
|
2021-11-28 19:35:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:43:36 +02:00
|
|
|
/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
|
|
|
|
/// This test should be symmetric, as it is primarily about layout compatibility.
|
2020-04-02 22:49:38 +02:00
|
|
|
pub(super) fn mir_assign_valid_types<'tcx>(
|
2020-04-05 11:55:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-11-15 13:53:31 +01:00
|
|
|
typing_env: TypingEnv<'tcx>,
|
2020-04-02 22:49:38 +02:00
|
|
|
src: TyAndLayout<'tcx>,
|
|
|
|
dest: TyAndLayout<'tcx>,
|
|
|
|
) -> bool {
|
2020-06-22 09:17:33 +02:00
|
|
|
// Type-changing assignments can happen when subtyping is used. While
|
|
|
|
// all normal lifetimes are erased, higher-ranked types with their
|
|
|
|
// late-bound lifetimes are still around and can lead to type
|
2022-11-14 17:42:46 +01:00
|
|
|
// differences.
|
2024-11-15 13:53:31 +01:00
|
|
|
if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
|
2020-06-24 09:03:01 +02:00
|
|
|
// Make sure the layout is equal, too -- just to be safe. Miri really
|
|
|
|
// needs layout equality. For performance reason we skip this check when
|
|
|
|
// the types are equal. Equal types *can* have different layouts when
|
|
|
|
// enum downcast is involved (as enum variants carry the type of the
|
|
|
|
// enum), but those should never occur in assignments.
|
|
|
|
if cfg!(debug_assertions) || src.ty != dest.ty {
|
|
|
|
assert_eq!(src.layout, dest.layout);
|
|
|
|
}
|
2020-06-22 11:07:39 +02:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2020-04-02 22:49:38 +02:00
|
|
|
}
|
2020-03-29 15:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Use the already known layout if given (but sanity check in debug mode),
|
|
|
|
/// or compute the layout.
|
|
|
|
#[cfg_attr(not(debug_assertions), inline(always))]
|
|
|
|
pub(super) fn from_known_layout<'tcx>(
|
2020-04-05 08:35:31 +02:00
|
|
|
tcx: TyCtxtAt<'tcx>,
|
2024-11-15 13:53:31 +01:00
|
|
|
typing_env: TypingEnv<'tcx>,
|
2020-03-29 15:43:36 +02:00
|
|
|
known_layout: Option<TyAndLayout<'tcx>>,
|
|
|
|
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
|
|
|
|
match known_layout {
|
|
|
|
None => compute(),
|
|
|
|
Some(known_layout) => {
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
let check_layout = compute()?;
|
2024-11-15 13:53:31 +01:00
|
|
|
if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
|
2020-04-05 08:35:31 +02:00
|
|
|
span_bug!(
|
|
|
|
tcx.span,
|
2023-09-20 22:25:09 +02:00
|
|
|
"expected type differs from actual type.\nexpected: {}\nactual: {}",
|
2020-04-05 08:35:31 +02:00
|
|
|
known_layout.ty,
|
|
|
|
check_layout.ty,
|
|
|
|
);
|
|
|
|
}
|
2020-03-29 15:43:36 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(known_layout)
|
2020-03-29 15:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 13:48:48 +01:00
|
|
|
/// Turn the given error into a human-readable string. Expects the string to be printed, so if
|
|
|
|
/// `RUSTC_CTFE_BACKTRACE` is set this will show a backtrace of the rustc internals that
|
|
|
|
/// triggered the error.
|
|
|
|
///
|
|
|
|
/// This is NOT the preferred way to render an error; use `report` from `const_eval` instead.
|
|
|
|
/// However, this is useful when error messages appear in ICEs.
|
2024-06-18 10:35:56 +00:00
|
|
|
pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
|
2024-01-06 13:48:48 +01:00
|
|
|
let (e, backtrace) = e.into_parts();
|
|
|
|
backtrace.print_backtrace();
|
|
|
|
// FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the
|
|
|
|
// label and arguments from the InterpError.
|
|
|
|
#[allow(rustc::untranslatable_diagnostic)]
|
|
|
|
let mut diag = dcx.struct_allow("");
|
|
|
|
let msg = e.diagnostic_message();
|
2024-02-12 15:18:18 +11:00
|
|
|
e.add_args(&mut diag);
|
2024-02-16 06:07:49 +11:00
|
|
|
let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
|
2024-01-06 13:48:48 +01:00
|
|
|
diag.cancel();
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
2019-06-26 13:13:19 -05:00
|
|
|
pub fn new(
|
2020-06-01 10:15:17 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
root_span: Span,
|
2019-06-26 13:13:19 -05:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
machine: M,
|
|
|
|
) -> Self {
|
2019-06-27 11:36:01 +02:00
|
|
|
InterpCx {
|
2017-12-06 14:23:32 +02:00
|
|
|
machine,
|
2020-06-14 15:02:51 +02:00
|
|
|
tcx: tcx.at(root_span),
|
2017-12-06 14:12:05 +02:00
|
|
|
param_env,
|
2022-04-03 13:05:49 -04:00
|
|
|
memory: Memory::new(),
|
2021-07-04 13:02:51 -05:00
|
|
|
recursion_limit: tcx.recursion_limit(),
|
2016-03-06 04:23:24 -06:00
|
|
|
}
|
|
|
|
}
|
2016-06-01 14:33:37 +02:00
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
/// During CTFE we're always in `PostAnalysis` mode.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn typing_env(&self) -> ty::TypingEnv<'tcx> {
|
2024-10-31 12:41:50 +01:00
|
|
|
debug_assert_eq!(self.param_env.reveal(), Reveal::All);
|
2024-11-15 13:53:31 +01:00
|
|
|
ty::TypingEnv { typing_mode: ty::TypingMode::PostAnalysis, param_env: self.param_env }
|
2024-10-31 12:41:50 +01:00
|
|
|
}
|
|
|
|
|
2024-06-13 08:59:08 +02:00
|
|
|
/// Returns the span of the currently executed statement/terminator.
|
|
|
|
/// This is the span typically used for error reporting.
|
2020-04-05 09:27:14 +02:00
|
|
|
#[inline(always)]
|
2020-06-01 10:15:17 +02:00
|
|
|
pub fn cur_span(&self) -> Span {
|
2022-06-26 14:42:26 -04:00
|
|
|
// This deliberately does *not* honor `requires_caller_location` since it is used for much
|
|
|
|
// more than just panics.
|
|
|
|
self.stack().last().map_or(self.tcx.span, |f| f.current_span())
|
2020-04-05 09:27:14 +02:00
|
|
|
}
|
|
|
|
|
2024-05-26 20:20:43 +02:00
|
|
|
pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
|
2020-03-16 15:12:42 -07:00
|
|
|
M::stack(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-05-26 20:20:43 +02:00
|
|
|
pub(crate) fn stack_mut(&mut self) -> &mut Vec<Frame<'tcx, M::Provenance, M::FrameExtra>> {
|
2020-03-16 15:12:42 -07:00
|
|
|
M::stack_mut(self)
|
2016-06-10 13:01:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2020-04-14 14:40:08 -07:00
|
|
|
pub fn frame_idx(&self) -> usize {
|
2020-03-16 15:12:42 -07:00
|
|
|
let stack = self.stack();
|
|
|
|
assert!(!stack.is_empty());
|
|
|
|
stack.len() - 1
|
2017-06-16 17:58:18 -07:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2024-05-26 20:20:43 +02:00
|
|
|
pub fn frame(&self) -> &Frame<'tcx, M::Provenance, M::FrameExtra> {
|
2020-03-16 15:12:42 -07:00
|
|
|
self.stack().last().expect("no call frames exist")
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2024-05-26 20:20:43 +02:00
|
|
|
pub fn frame_mut(&mut self) -> &mut Frame<'tcx, M::Provenance, M::FrameExtra> {
|
2020-03-16 15:12:42 -07:00
|
|
|
self.stack_mut().last_mut().expect("no call frames exist")
|
2018-10-16 15:31:04 +02:00
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2024-05-27 08:24:23 +02:00
|
|
|
pub fn body(&self) -> &'tcx mir::Body<'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
self.frame().body
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2019-07-02 10:49:02 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
|
2022-10-27 14:45:02 +04:00
|
|
|
ty.is_freeze(*self.tcx, self.param_env)
|
2019-07-02 10:49:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
pub fn load_mir(
|
|
|
|
&self,
|
2024-06-16 21:35:16 -04:00
|
|
|
instance: ty::InstanceKind<'tcx>,
|
2019-08-05 20:01:59 -04:00
|
|
|
promoted: Option<mir::Promoted>,
|
2020-04-12 10:31:00 -07:00
|
|
|
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
|
2019-08-05 20:01:59 -04:00
|
|
|
trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
|
2022-02-07 22:21:23 -08:00
|
|
|
let body = if let Some(promoted) = promoted {
|
2022-05-08 15:53:19 +02:00
|
|
|
let def = instance.def_id();
|
|
|
|
&self.tcx.promoted_mir(def)[promoted]
|
2022-02-07 22:21:23 -08:00
|
|
|
} else {
|
|
|
|
M::load_mir(self, instance)?
|
|
|
|
};
|
|
|
|
// do not continue if typeck errors occurred (can only occur in local crate)
|
|
|
|
if let Some(err) = body.tainted_by_errors {
|
2023-05-15 00:00:00 +00:00
|
|
|
throw_inval!(AlreadyReported(ReportedErrorInfo::tainted_by_errors(err)));
|
2019-08-05 20:01:59 -04:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(body)
|
2016-06-06 15:22:33 +02:00
|
|
|
}
|
2016-06-08 11:11:08 +02:00
|
|
|
|
2019-08-11 10:12:26 +02:00
|
|
|
/// Call this on things you got out of the MIR (so it is as generic as the current
|
2019-08-12 16:32:48 +03:00
|
|
|
/// stack frame), to bring it into the proper environment for this interpreter.
|
2024-02-12 15:39:32 +09:00
|
|
|
pub(super) fn instantiate_from_current_frame_and_normalize_erasing_regions<
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
|
|
|
>(
|
2020-03-11 19:36:07 +00:00
|
|
|
&self,
|
|
|
|
value: T,
|
2023-09-11 09:52:45 +02:00
|
|
|
) -> Result<T, ErrorHandled> {
|
2024-02-12 15:39:32 +09:00
|
|
|
self.instantiate_from_frame_and_normalize_erasing_regions(self.frame(), value)
|
2020-03-11 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Call this on things you got out of the MIR (so it is as generic as the provided
|
|
|
|
/// stack frame), to bring it into the proper environment for this interpreter.
|
2024-02-12 15:39:32 +09:00
|
|
|
pub(super) fn instantiate_from_frame_and_normalize_erasing_regions<
|
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
|
|
|
>(
|
2019-01-14 17:54:35 +01:00
|
|
|
&self,
|
2024-05-26 20:20:43 +02:00
|
|
|
frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
|
2019-08-12 16:32:48 +03:00
|
|
|
value: T,
|
2023-09-11 09:52:45 +02:00
|
|
|
) -> Result<T, ErrorHandled> {
|
2021-12-05 11:13:51 +01:00
|
|
|
frame
|
|
|
|
.instance
|
2023-09-25 15:46:38 +02:00
|
|
|
.try_instantiate_mir_and_normalize_erasing_regions(
|
2023-04-14 09:59:03 -06:00
|
|
|
*self.tcx,
|
2024-11-15 13:53:31 +01:00
|
|
|
self.typing_env(),
|
2023-05-29 13:46:10 +02:00
|
|
|
ty::EarlyBinder::bind(value),
|
2023-04-14 09:59:03 -06:00
|
|
|
)
|
2023-09-11 09:52:45 +02:00
|
|
|
.map_err(|_| ErrorHandled::TooGeneric(self.cur_span()))
|
2019-01-14 17:54:35 +01:00
|
|
|
}
|
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
/// The `args` are assumed to already be in our interpreter "universe".
|
2019-08-11 10:12:26 +02:00
|
|
|
pub(super) fn resolve(
|
2018-08-16 00:18:09 +02:00
|
|
|
&self,
|
2022-05-08 15:53:19 +02:00
|
|
|
def: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: GenericArgsRef<'tcx>,
|
2019-08-11 10:12:26 +02:00
|
|
|
) -> InterpResult<'tcx, ty::Instance<'tcx>> {
|
2023-07-11 22:35:29 +01:00
|
|
|
trace!("resolve: {:?}, {:#?}", def, args);
|
2019-08-11 10:12:26 +02:00
|
|
|
trace!("param_env: {:#?}", self.param_env);
|
2023-07-11 22:35:29 +01:00
|
|
|
trace!("args: {:#?}", args);
|
2024-11-15 13:53:31 +01:00
|
|
|
match ty::Instance::try_resolve(*self.tcx, self.typing_env(), def, args) {
|
2024-09-29 11:53:23 +02:00
|
|
|
Ok(Some(instance)) => interp_ok(instance),
|
2020-04-10 05:13:29 +03:00
|
|
|
Ok(None) => throw_inval!(TooGeneric),
|
|
|
|
|
2020-11-04 22:23:43 +05:30
|
|
|
// FIXME(eddyb) this could be a bit more specific than `AlreadyReported`.
|
2023-05-15 00:00:00 +00:00
|
|
|
Err(error_reported) => throw_inval!(AlreadyReported(error_reported.into())),
|
2020-04-10 05:13:29 +03:00
|
|
|
}
|
2017-03-14 11:12:59 +01:00
|
|
|
}
|
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
/// Check if the two things are equal in the current param_env, using an infcx to get proper
|
2024-07-17 11:48:22 +02:00
|
|
|
/// equality checks.
|
2024-09-11 13:32:53 -04:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
2024-07-17 11:48:22 +02:00
|
|
|
pub(super) fn eq_in_param_env<T>(&self, a: T, b: T) -> bool
|
|
|
|
where
|
|
|
|
T: PartialEq + TypeFoldable<TyCtxt<'tcx>> + ToTrace<'tcx>,
|
|
|
|
{
|
|
|
|
// Fast path: compare directly.
|
|
|
|
if a == b {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Slow path: spin up an inference context to check if these traits are sufficiently equal.
|
2024-11-15 13:53:31 +01:00
|
|
|
let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env());
|
2024-07-17 11:48:22 +02:00
|
|
|
let ocx = ObligationCtxt::new(&infcx);
|
|
|
|
let cause = ObligationCause::dummy_with_span(self.cur_span());
|
|
|
|
// equate the two trait refs after normalization
|
2024-11-15 13:53:31 +01:00
|
|
|
let a = ocx.normalize(&cause, param_env, a);
|
|
|
|
let b = ocx.normalize(&cause, param_env, b);
|
2024-09-11 13:32:53 -04:00
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
if let Err(terr) = ocx.eq(&cause, param_env, a, b) {
|
2024-09-11 13:32:53 -04:00
|
|
|
trace!(?terr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let errors = ocx.select_all_or_error();
|
|
|
|
if !errors.is_empty() {
|
|
|
|
trace!(?errors);
|
|
|
|
return false;
|
2024-07-17 11:48:22 +02:00
|
|
|
}
|
2024-09-11 13:32:53 -04:00
|
|
|
|
|
|
|
// All good.
|
|
|
|
true
|
2024-07-17 11:48:22 +02:00
|
|
|
}
|
|
|
|
|
2023-10-28 15:39:54 +02:00
|
|
|
/// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a
|
2024-06-13 08:59:08 +02:00
|
|
|
/// frame which is not `#[track_caller]`. This matches the `caller_location` intrinsic,
|
|
|
|
/// and is primarily intended for the panic machinery.
|
2023-10-28 15:39:54 +02:00
|
|
|
pub(crate) fn find_closest_untracked_caller_location(&self) -> Span {
|
|
|
|
for frame in self.stack().iter().rev() {
|
|
|
|
debug!("find_closest_untracked_caller_location: checking frame {:?}", frame.instance);
|
|
|
|
|
|
|
|
// Assert that the frame we look at is actually executing code currently
|
|
|
|
// (`loc` is `Right` when we are unwinding and the frame does not require cleanup).
|
|
|
|
let loc = frame.loc.left().unwrap();
|
|
|
|
|
|
|
|
// This could be a non-`Call` terminator (such as `Drop`), or not a terminator at all
|
|
|
|
// (such as `box`). Use the normal span by default.
|
|
|
|
let mut source_info = *frame.body.source_info(loc);
|
|
|
|
|
|
|
|
// If this is a `Call` terminator, use the `fn_span` instead.
|
|
|
|
let block = &frame.body.basic_blocks[loc.block];
|
|
|
|
if loc.statement_index == block.statements.len() {
|
|
|
|
debug!(
|
|
|
|
"find_closest_untracked_caller_location: got terminator {:?} ({:?})",
|
|
|
|
block.terminator(),
|
|
|
|
block.terminator().kind,
|
|
|
|
);
|
|
|
|
if let mir::TerminatorKind::Call { fn_span, .. } = block.terminator().kind {
|
|
|
|
source_info.span = fn_span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 16:16:15 +02:00
|
|
|
let caller_location = if frame.instance.def.requires_caller_location(*self.tcx) {
|
|
|
|
// We use `Err(())` as indication that we should continue up the call stack since
|
|
|
|
// this is a `#[track_caller]` function.
|
|
|
|
Some(Err(()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Ok(span) =
|
|
|
|
frame.body.caller_location_span(source_info, caller_location, *self.tcx, Ok)
|
|
|
|
{
|
|
|
|
return span;
|
2023-10-28 15:39:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
span_bug!(self.cur_span(), "no non-`#[track_caller]` frame found")
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns the actual dynamic size and alignment of the place at the given type.
|
2018-10-09 22:41:41 +02:00
|
|
|
/// Only the "meta" (metadata) part of the place matters.
|
|
|
|
/// This can fail to provide an answer for extern types.
|
2018-08-25 14:36:24 +02:00
|
|
|
pub(super) fn size_and_align_of(
|
2018-08-03 11:21:44 +02:00
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
metadata: &MemPlaceMeta<M::Provenance>,
|
2021-02-15 00:00:00 +00:00
|
|
|
layout: &TyAndLayout<'tcx>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, Option<(Size, Align)>> {
|
2022-11-13 12:14:59 +01:00
|
|
|
if layout.is_sized() {
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(Some((layout.size, layout.align.abi)));
|
2018-10-09 22:41:41 +02:00
|
|
|
}
|
2020-08-03 00:49:11 +02:00
|
|
|
match layout.ty.kind() {
|
2018-08-25 14:36:24 +02:00
|
|
|
ty::Adt(..) | ty::Tuple(..) => {
|
|
|
|
// First get the size of all statically known fields.
|
|
|
|
// Don't use type_of::sizing_type_of because that expects t to be sized,
|
|
|
|
// and it also rounds up to alignment, which we want to avoid,
|
|
|
|
// as the unsized field's alignment could be smaller.
|
|
|
|
assert!(!layout.ty.is_simd());
|
2020-03-21 13:49:02 +01:00
|
|
|
assert!(layout.fields.count() > 0);
|
2018-10-17 12:36:18 +02:00
|
|
|
trace!("DST layout: {:?}", layout);
|
2018-08-25 14:36:24 +02:00
|
|
|
|
2023-12-02 13:40:24 +01:00
|
|
|
let unsized_offset_unadjusted = layout.fields.offset(layout.fields.count() - 1);
|
2018-09-09 01:16:45 +03:00
|
|
|
let sized_align = layout.align.abi;
|
2018-08-25 14:36:24 +02:00
|
|
|
|
|
|
|
// Recurse to get the size of the dynamically sized field (must be
|
2022-11-16 20:34:16 +00:00
|
|
|
// the last field). Can't have foreign types here, how would we
|
2018-10-09 22:41:41 +02:00
|
|
|
// adjust alignment and size for them?
|
2021-08-25 18:05:10 +03:00
|
|
|
let field = layout.field(self, layout.fields.count() - 1);
|
2022-10-29 15:38:00 +02:00
|
|
|
let Some((unsized_size, mut unsized_align)) =
|
|
|
|
self.size_and_align_of(metadata, &field)?
|
|
|
|
else {
|
2022-02-19 00:47:43 +01:00
|
|
|
// A field with an extern type. We don't know the actual dynamic size
|
|
|
|
// or the alignment.
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(None);
|
2022-02-19 00:47:43 +01:00
|
|
|
};
|
2018-08-25 14:36:24 +02:00
|
|
|
|
2023-12-02 13:40:24 +01:00
|
|
|
// # First compute the dynamic alignment
|
2018-08-25 14:36:24 +02:00
|
|
|
|
2023-12-02 13:40:24 +01:00
|
|
|
// Packed type alignment needs to be capped.
|
2022-10-29 15:38:00 +02:00
|
|
|
if let ty::Adt(def, _) = layout.ty.kind() {
|
2023-12-02 13:40:24 +01:00
|
|
|
if let Some(packed) = def.repr().pack {
|
|
|
|
unsized_align = unsized_align.min(packed);
|
2022-10-29 15:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 14:36:24 +02:00
|
|
|
// Choose max of two known alignments (combined value must
|
|
|
|
// be aligned according to more restrictive of the two).
|
2023-12-02 13:40:24 +01:00
|
|
|
let full_align = sized_align.max(unsized_align);
|
|
|
|
|
|
|
|
// # Then compute the dynamic size
|
2018-08-25 14:36:24 +02:00
|
|
|
|
2023-12-02 13:40:24 +01:00
|
|
|
let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
|
|
|
|
let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
|
|
|
|
|
|
|
|
// Just for our sanitiy's sake, assert that this is equal to what codegen would compute.
|
|
|
|
assert_eq!(
|
|
|
|
full_size,
|
|
|
|
(unsized_offset_unadjusted + unsized_size).align_to(full_align)
|
|
|
|
);
|
2019-08-27 12:54:46 +02:00
|
|
|
|
|
|
|
// Check if this brought us over the size limit.
|
2023-12-02 13:40:24 +01:00
|
|
|
if full_size > self.max_size_of_val() {
|
2023-05-17 10:30:14 +00:00
|
|
|
throw_ub!(InvalidMeta(InvalidMetaKind::TooBig));
|
2019-08-27 12:54:46 +02:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(Some((full_size, full_align)))
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
2024-06-11 07:47:58 +02:00
|
|
|
ty::Dynamic(expected_trait, _, ty::Dyn) => {
|
2022-07-23 10:36:57 -04:00
|
|
|
let vtable = metadata.unwrap_meta().to_pointer(self)?;
|
2019-08-27 12:54:46 +02:00
|
|
|
// Read size and align from vtable (already checks size).
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(Some(self.get_vtable_size_and_align(vtable, Some(expected_trait))?))
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::Slice(_) | ty::Str => {
|
2023-02-14 14:31:26 +00:00
|
|
|
let len = metadata.unwrap_meta().to_target_usize(self)?;
|
2021-08-25 18:05:10 +03:00
|
|
|
let elem = layout.field(self, 0);
|
2019-08-27 12:54:46 +02:00
|
|
|
|
|
|
|
// Make sure the slice is not too big.
|
2022-03-27 20:02:11 -04:00
|
|
|
let size = elem.size.bytes().saturating_mul(len); // we rely on `max_size_of_val` being smaller than `u64::MAX`.
|
|
|
|
let size = Size::from_bytes(size);
|
2022-03-27 19:34:16 -04:00
|
|
|
if size > self.max_size_of_val() {
|
2023-05-17 10:30:14 +00:00
|
|
|
throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig));
|
2022-03-27 19:34:16 -04:00
|
|
|
}
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(Some((size, elem.align.abi)))
|
2018-10-09 22:41:41 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 11:53:23 +02:00
|
|
|
ty::Foreign(_) => interp_ok(None),
|
2018-08-25 14:36:24 +02:00
|
|
|
|
2023-09-20 22:25:09 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty),
|
2017-07-28 10:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 14:36:24 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn size_and_align_of_mplace(
|
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
mplace: &MPlaceTy<'tcx, M::Provenance>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, Option<(Size, Align)>> {
|
2023-09-04 17:53:38 +02:00
|
|
|
self.size_and_align_of(&mplace.meta(), &mplace.layout)
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
2017-07-28 10:16:36 +02:00
|
|
|
|
2019-11-22 22:17:15 +01:00
|
|
|
/// Jump to the given block.
|
|
|
|
#[inline]
|
|
|
|
pub fn go_to_block(&mut self, target: mir::BasicBlock) {
|
2022-11-18 10:18:32 +01:00
|
|
|
self.frame_mut().loc = Left(mir::Location { block: target, statement_index: 0 });
|
2019-11-22 22:17:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// *Return* to the given `target` basic block.
|
|
|
|
/// Do *not* use for unwinding! Use `unwind_to_block` instead.
|
|
|
|
///
|
|
|
|
/// If `target` is `None`, that indicates the function cannot return, so we raise UB.
|
|
|
|
pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
|
|
|
|
if let Some(target) = target {
|
2020-03-21 14:24:57 +01:00
|
|
|
self.go_to_block(target);
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2019-11-22 22:17:15 +01:00
|
|
|
} else {
|
|
|
|
throw_ub!(Unreachable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// *Unwind* to the given `target` basic block.
|
|
|
|
/// Do *not* use for returning! Use `return_to_block` instead.
|
|
|
|
///
|
2022-10-10 19:50:49 +01:00
|
|
|
/// If `target` is `UnwindAction::Continue`, that indicates the function does not need cleanup
|
2021-05-25 20:43:54 +08:00
|
|
|
/// during unwinding, and we will just keep propagating that upwards.
|
|
|
|
///
|
2022-10-10 19:50:49 +01:00
|
|
|
/// If `target` is `UnwindAction::Unreachable`, that indicates the function does not allow
|
2021-05-25 20:43:54 +08:00
|
|
|
/// unwinding, and doing so is UB.
|
2023-08-21 09:57:10 +02:00
|
|
|
#[cold] // usually we have normal returns, not unwinding
|
2022-10-10 19:50:49 +01:00
|
|
|
pub fn unwind_to_block(&mut self, target: mir::UnwindAction) -> InterpResult<'tcx> {
|
2020-08-11 14:54:02 +02:00
|
|
|
self.frame_mut().loc = match target {
|
2022-10-10 19:50:49 +01:00
|
|
|
mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }),
|
|
|
|
mir::UnwindAction::Continue => Right(self.frame_mut().body.span),
|
|
|
|
mir::UnwindAction::Unreachable => {
|
2023-05-17 10:30:14 +00:00
|
|
|
throw_ub_custom!(fluent::const_eval_unreachable_unwind);
|
2021-05-25 20:43:54 +08:00
|
|
|
}
|
2023-08-21 09:57:10 +02:00
|
|
|
mir::UnwindAction::Terminate(reason) => {
|
2022-10-10 23:17:07 +01:00
|
|
|
self.frame_mut().loc = Right(self.frame_mut().body.span);
|
2023-08-21 09:57:10 +02:00
|
|
|
M::unwind_terminate(self, reason)?;
|
2023-08-19 13:21:41 +02:00
|
|
|
// This might have pushed a new stack frame, or it terminated execution.
|
|
|
|
// Either way, `loc` will not be updated.
|
2024-09-29 11:53:23 +02:00
|
|
|
return interp_ok(());
|
2022-10-10 22:40:40 +01:00
|
|
|
}
|
2020-08-11 14:54:02 +02:00
|
|
|
};
|
2024-09-29 11:53:23 +02:00
|
|
|
interp_ok(())
|
2019-11-22 22:17:15 +01:00
|
|
|
}
|
|
|
|
|
2023-09-29 22:38:52 +02:00
|
|
|
/// Call a query that can return `ErrorHandled`. Should be used for statics and other globals.
|
|
|
|
/// (`mir::Const`/`ty::Const` have `eval` methods that can be used directly instead.)
|
2022-11-15 12:06:20 +01:00
|
|
|
pub fn ctfe_query<T>(
|
|
|
|
&self,
|
|
|
|
query: impl FnOnce(TyCtxtAt<'tcx>) -> Result<T, ErrorHandled>,
|
2023-09-11 09:52:45 +02:00
|
|
|
) -> Result<T, ErrorHandled> {
|
2022-11-15 12:06:20 +01:00
|
|
|
// Use a precise span for better cycle errors.
|
2023-09-29 22:38:52 +02:00
|
|
|
query(self.tcx.at(self.cur_span())).map_err(|err| {
|
2023-09-11 23:09:11 +02:00
|
|
|
err.emit_note(*self.tcx);
|
2023-09-11 09:52:45 +02:00
|
|
|
err
|
2022-11-15 12:06:20 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eval_global(
|
2018-11-06 16:16:27 +01:00
|
|
|
&self,
|
2023-09-11 09:52:45 +02:00
|
|
|
instance: ty::Instance<'tcx>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
2023-09-11 09:52:45 +02:00
|
|
|
let gid = GlobalId { instance, promoted: None };
|
2023-10-17 08:50:41 +00:00
|
|
|
let val = if self.tcx.is_static(gid.instance.def_id()) {
|
|
|
|
let alloc_id = self.tcx.reserve_and_set_static_alloc(gid.instance.def_id());
|
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
let ty = instance.ty(self.tcx.tcx, self.typing_env());
|
2023-10-17 08:50:41 +00:00
|
|
|
mir::ConstAlloc { alloc_id, ty }
|
2018-01-31 15:06:45 +01:00
|
|
|
} else {
|
2023-10-17 08:50:41 +00:00
|
|
|
self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.param_env.and(gid)))?
|
2018-01-31 15:06:45 +01:00
|
|
|
};
|
2018-11-06 16:16:27 +01:00
|
|
|
self.raw_const_to_mplace(val)
|
2018-01-31 15:06:45 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 09:52:45 +02:00
|
|
|
pub fn eval_mir_constant(
|
|
|
|
&self,
|
2023-09-20 20:51:14 +02:00
|
|
|
val: &mir::Const<'tcx>,
|
2024-03-14 09:10:28 +00:00
|
|
|
span: Span,
|
2023-09-11 09:52:45 +02:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2023-11-26 17:06:13 -05:00
|
|
|
M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
|
2024-11-15 13:53:31 +01:00
|
|
|
let const_val = val.eval(*ecx.tcx, ecx.typing_env(), span).map_err(|err| {
|
2024-04-29 11:53:23 +00:00
|
|
|
if M::ALL_CONSTS_ARE_PRECHECKED {
|
|
|
|
match err {
|
|
|
|
ErrorHandled::TooGeneric(..) => {},
|
|
|
|
ErrorHandled::Reported(reported, span) => {
|
|
|
|
if reported.is_tainted_by_errors() {
|
|
|
|
// const-eval will return "tainted" errors if e.g. the layout cannot
|
|
|
|
// be computed as the type references non-existing names.
|
|
|
|
// See <https://github.com/rust-lang/rust/issues/124348>.
|
|
|
|
} else {
|
2024-09-02 07:42:38 +02:00
|
|
|
// Looks like the const is not captured by `required_consts`, that's bad.
|
2024-04-29 11:53:23 +00:00
|
|
|
span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-14 07:28:25 +01:00
|
|
|
}
|
2023-11-26 17:06:13 -05:00
|
|
|
err.emit_note(*ecx.tcx);
|
|
|
|
err
|
|
|
|
})?;
|
|
|
|
ecx.const_val_to_op(const_val, val.ty(), layout)
|
|
|
|
})
|
2023-09-11 09:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 16:15:40 +02:00
|
|
|
#[must_use]
|
2024-05-27 08:24:23 +02:00
|
|
|
pub fn dump_place(&self, place: &PlaceTy<'tcx, M::Provenance>) -> PlacePrinter<'_, 'tcx, M> {
|
2023-09-04 17:53:38 +02:00
|
|
|
PlacePrinter { ecx: self, place: *place.place() }
|
2020-07-28 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 19:08:14 +02:00
|
|
|
#[must_use]
|
|
|
|
pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
|
2024-03-11 13:20:12 +00:00
|
|
|
Frame::generate_stacktrace_from_stack(self.stack())
|
2022-09-18 19:08:14 +02:00
|
|
|
}
|
2024-10-14 20:58:32 +02:00
|
|
|
|
|
|
|
pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
|
|
|
|
where
|
|
|
|
F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
|
|
|
|
F2: rustc_apfloat::Float,
|
|
|
|
{
|
|
|
|
if f.is_nan() { M::generate_nan(self, inputs) } else { f }
|
|
|
|
}
|
2020-07-28 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
/// Helper struct for the `dump_place` function.
|
2024-05-27 08:24:23 +02:00
|
|
|
pub struct PlacePrinter<'a, 'tcx, M: Machine<'tcx>> {
|
|
|
|
ecx: &'a InterpCx<'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
place: Place<M::Provenance>,
|
2020-07-28 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:24:23 +02:00
|
|
|
impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debug for PlacePrinter<'a, 'tcx, M> {
|
2020-07-28 16:15:40 +02:00
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self.place {
|
2024-03-09 18:13:50 +01:00
|
|
|
Place::Local { local, offset, locals_addr } => {
|
|
|
|
debug_assert_eq!(locals_addr, self.ecx.frame().locals_addr());
|
2017-08-08 16:29:47 +02:00
|
|
|
let mut allocs = Vec::new();
|
2023-07-25 23:17:39 +02:00
|
|
|
write!(fmt, "{local:?}")?;
|
2023-07-23 21:35:54 +02:00
|
|
|
if let Some(offset) = offset {
|
|
|
|
write!(fmt, "+{:#x}", offset.bytes())?;
|
|
|
|
}
|
2020-07-28 16:15:40 +02:00
|
|
|
write!(fmt, ":")?;
|
2017-08-08 16:29:47 +02:00
|
|
|
|
2024-08-05 17:34:44 +02:00
|
|
|
self.ecx.frame().locals[local].print(&mut allocs, fmt)?;
|
2017-08-08 16:29:47 +02:00
|
|
|
|
2022-05-26 13:14:24 +02:00
|
|
|
write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs.into_iter().flatten().collect()))
|
2017-08-08 16:29:47 +02:00
|
|
|
}
|
2022-05-13 12:30:25 -05:00
|
|
|
Place::Ptr(mplace) => match mplace.ptr.provenance.and_then(Provenance::get_alloc_id) {
|
2022-07-03 10:21:47 -04:00
|
|
|
Some(alloc_id) => {
|
|
|
|
write!(fmt, "by ref {:?}: {:?}", mplace.ptr, self.ecx.dump_alloc(alloc_id))
|
|
|
|
}
|
2023-07-25 23:17:39 +02:00
|
|
|
ptr => write!(fmt, " integral by ref: {ptr:?}"),
|
2016-10-18 21:45:48 -06:00
|
|
|
},
|
2017-02-07 00:39:40 -08:00
|
|
|
}
|
2016-10-18 21:45:48 -06:00
|
|
|
}
|
2016-10-15 19:48:30 -06:00
|
|
|
}
|