1use std::assert_matches::debug_assert_matches;
23use either::{Left, Right};
4use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
5use rustc_errors::DiagCtxtHandle;
6use rustc_hir::def_id::DefId;
7use rustc_hir::limit::Limit;
8use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
9use rustc_middle::query::TyCtxtAt;
10use rustc_middle::ty::layout::{
11self, FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf,
12LayoutOfHelpers, TyAndLayout,
13};
14use rustc_middle::ty::{
15self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingEnv, Variance,
16};
17use rustc_middle::{mir, span_bug};
18use rustc_span::Span;
19use rustc_target::callconv::FnAbi;
20use tracing::{debug, trace};
2122use super::{
23Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
24MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
25err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
26};
27use crate::{ReportErrorExt, enter_trace_span, fluent_generatedas fluent, util};
2829pub struct InterpCx<'tcx, M: Machine<'tcx>> {
30/// Stores the `Machine` instance.
31 ///
32 /// Note: the stack is provided by the machine.
33pub machine: M,
3435/// The results of the type checker, from rustc.
36 /// The span in this is the "root" of the evaluation, i.e., the const
37 /// we are evaluating (if this is CTFE).
38pub tcx: TyCtxtAt<'tcx>,
3940/// The current context in case we're evaluating in a
41 /// polymorphic context. This always uses `ty::TypingMode::PostAnalysis`.
42pub(super) typing_env: ty::TypingEnv<'tcx>,
4344/// The virtual memory system.
45pub memory: Memory<'tcx, M>,
4647/// The recursion limit (cached from `tcx.recursion_limit(())`)
48pub recursion_limit: Limit,
49}
5051impl<'tcx, M: Machine<'tcx>> HasDataLayoutfor InterpCx<'tcx, M> {
52#[inline]
53fn data_layout(&self) -> &TargetDataLayout {
54&self.tcx.data_layout
55 }
56}
5758impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
59where
60M: Machine<'tcx>,
61{
62#[inline]
63fn tcx(&self) -> TyCtxt<'tcx> {
64*self.tcx
65 }
66}
6768impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
69where
70M: Machine<'tcx>,
71{
72fn typing_env(&self) -> ty::TypingEnv<'tcx> {
73self.typing_env
74 }
75}
7677impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
78type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
7980#[inline]
81fn layout_tcx_at_span(&self) -> Span {
82// Using the cheap root span for performance.
83self.tcx.span
84 }
8586#[inline]
87fn handle_layout_err(
88&self,
89mut err: LayoutError<'tcx>,
90_: Span,
91_: Ty<'tcx>,
92 ) -> InterpErrorKind<'tcx> {
93// FIXME(#149283): This is really hacky and is only used to hide type
94 // system bugs. We use it as a temporary fix for #149081.
95 //
96 // While it's expected that we sometimes get ambiguity errors when
97 // entering another generic environment while the current environment
98 // itself is still generic, we should never fail to entirely prove
99 // something.
100match err {
101 LayoutError::NormalizationFailure(ty, _) => {
102if ty.has_non_region_param() {
103err = LayoutError::TooGeneric(ty);
104 }
105 }
106107 LayoutError::Unknown(_)
108 | LayoutError::SizeOverflow(_)
109 | LayoutError::InvalidSimd { .. }
110 | LayoutError::TooGeneric(_)
111 | LayoutError::ReferencesError(_)
112 | LayoutError::Cycle(_) => {}
113 }
114::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::Layout(err))err_inval!(Layout(err))115 }
116}
117118impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
119type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
120121fn handle_fn_abi_err(
122&self,
123 err: FnAbiError<'tcx>,
124 _span: Span,
125 _fn_abi_request: FnAbiRequest<'tcx>,
126 ) -> InterpErrorKind<'tcx> {
127match err {
128 FnAbiError::Layout(err) => ::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::Layout(err))err_inval!(Layout(err)),
129 }
130 }
131}
132133impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
134/// This inherent method takes priority over the trait method with the same name in LayoutOf,
135 /// and allows wrapping the actual [LayoutOf::layout_of] with a tracing span.
136 /// See [LayoutOf::layout_of] for the original documentation.
137#[inline(always)]
138pub fn layout_of(&self, ty: Ty<'tcx>) -> Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>> {
139let _trace = <M as
crate::interpret::Machine>::enter_trace_span(||
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("layouting",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(139u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["layouting", "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::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,
&{
#[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(&display(&"layout_of")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ty.kind())
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
})enter_trace_span!(M, layouting::layout_of, ty = ?ty.kind());
140 LayoutOf::layout_of(self, ty)
141 }
142143/// This inherent method takes priority over the trait method with the same name in FnAbiOf,
144 /// and allows wrapping the actual [FnAbiOf::fn_abi_of_fn_ptr] with a tracing span.
145 /// See [FnAbiOf::fn_abi_of_fn_ptr] for the original documentation.
146#[inline(always)]
147pub fn fn_abi_of_fn_ptr(
148&self,
149 sig: ty::PolyFnSig<'tcx>,
150 extra_args: &'tcx ty::List<Ty<'tcx>>,
151 ) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
152let _trace = <M as
crate::interpret::Machine>::enter_trace_span(||
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("layouting",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(152u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["layouting", "sig",
"extra_args"],
::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,
&{
#[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(&display(&"fn_abi_of_fn_ptr")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&sig) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&extra_args)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
})enter_trace_span!(M, layouting::fn_abi_of_fn_ptr, ?sig, ?extra_args);
153 FnAbiOf::fn_abi_of_fn_ptr(self, sig, extra_args)
154 }
155156/// This inherent method takes priority over the trait method with the same name in FnAbiOf,
157 /// and allows wrapping the actual [FnAbiOf::fn_abi_of_instance] with a tracing span.
158 /// See [FnAbiOf::fn_abi_of_instance] for the original documentation.
159#[inline(always)]
160pub fn fn_abi_of_instance(
161&self,
162 instance: ty::Instance<'tcx>,
163 extra_args: &'tcx ty::List<Ty<'tcx>>,
164 ) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
165let _trace = <M as
crate::interpret::Machine>::enter_trace_span(||
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("layouting",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(165u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["layouting",
"instance", "extra_args"],
::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,
&{
#[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(&display(&"fn_abi_of_instance")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&instance)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&extra_args)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
})enter_trace_span!(M, layouting::fn_abi_of_instance, ?instance, ?extra_args);
166 FnAbiOf::fn_abi_of_instance(self, instance, extra_args)
167 }
168}
169170/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
171/// This test should be symmetric, as it is primarily about layout compatibility.
172pub(super) fn mir_assign_valid_types<'tcx>(
173 tcx: TyCtxt<'tcx>,
174 typing_env: TypingEnv<'tcx>,
175 src: TyAndLayout<'tcx>,
176 dest: TyAndLayout<'tcx>,
177) -> bool {
178// Type-changing assignments can happen when subtyping is used. While
179 // all normal lifetimes are erased, higher-ranked types with their
180 // late-bound lifetimes are still around and can lead to type
181 // differences.
182if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
183// Make sure the layout is equal, too -- just to be safe. Miri really
184 // needs layout equality. For performance reason we skip this check when
185 // the types are equal. Equal types *can* have different layouts when
186 // enum downcast is involved (as enum variants carry the type of the
187 // enum), but those should never occur in assignments.
188if truecfg!(debug_assertions) || src.ty != dest.ty {
189match (&src.layout, &dest.layout) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(src.layout, dest.layout);
190 }
191true
192} else {
193false
194}
195}
196197/// Use the already known layout if given (but sanity check in debug mode),
198/// or compute the layout.
199#[cfg_attr(not(debug_assertions), inline(always))]
200pub(super) fn from_known_layout<'tcx>(
201 tcx: TyCtxtAt<'tcx>,
202 typing_env: TypingEnv<'tcx>,
203 known_layout: Option<TyAndLayout<'tcx>>,
204 compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
205) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
206match known_layout {
207None => compute(),
208Some(known_layout) => {
209if truecfg!(debug_assertions) {
210let check_layout = compute()?;
211if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
212::rustc_middle::util::bug::span_bug_fmt(tcx.span,
format_args!("expected type differs from actual type.\nexpected: {0}\nactual: {1}",
known_layout.ty, check_layout.ty));span_bug!(
213tcx.span,
214"expected type differs from actual type.\nexpected: {}\nactual: {}",
215 known_layout.ty,
216 check_layout.ty,
217 );
218 }
219 }
220interp_ok(known_layout)
221 }
222 }
223}
224225/// Turn the given error into a human-readable string. Expects the string to be printed, so if
226/// `RUSTC_CTFE_BACKTRACE` is set this will show a backtrace of the rustc internals that
227/// triggered the error.
228///
229/// This is NOT the preferred way to render an error; use `report` from `const_eval` instead.
230/// However, this is useful when error messages appear in ICEs.
231pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
232let (e, backtrace) = e.into_parts();
233backtrace.print_backtrace();
234// FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the
235 // label and arguments from the InterpError.
236#[allow(rustc::untranslatable_diagnostic)]
237let mut diag = dcx.struct_allow("");
238let msg = e.diagnostic_message();
239e.add_args(&mut diag);
240let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
241diag.cancel();
242s243}
244245impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
246pub fn new(
247 tcx: TyCtxt<'tcx>,
248 root_span: Span,
249 typing_env: ty::TypingEnv<'tcx>,
250 machine: M,
251 ) -> Self {
252// Const eval always happens in post analysis mode in order to be able to use the hidden types of
253 // opaque types. This is needed for trivial things like `size_of`, but also for using associated
254 // types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
255 // already been revealed, so we'd be able to at least partially observe the hidden types anyways.
256if true {
match typing_env.typing_mode {
ty::TypingMode::PostAnalysis => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"ty::TypingMode::PostAnalysis", ::core::option::Option::None);
}
};
};debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
257InterpCx {
258machine,
259 tcx: tcx.at(root_span),
260typing_env,
261 memory: Memory::new(),
262 recursion_limit: tcx.recursion_limit(),
263 }
264 }
265266/// Returns the span of the currently executed statement/terminator.
267 /// This is the span typically used for error reporting.
268#[inline(always)]
269pub fn cur_span(&self) -> Span {
270// This deliberately does *not* honor `requires_caller_location` since it is used for much
271 // more than just panics.
272self.stack().last().map_or(self.tcx.span, |f| f.current_span())
273 }
274275pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
276 M::stack(self)
277 }
278279#[inline(always)]
280pub(crate) fn stack_mut(&mut self) -> &mut Vec<Frame<'tcx, M::Provenance, M::FrameExtra>> {
281 M::stack_mut(self)
282 }
283284#[inline(always)]
285pub fn frame_idx(&self) -> usize {
286let stack = self.stack();
287if !!stack.is_empty() {
::core::panicking::panic("assertion failed: !stack.is_empty()")
};assert!(!stack.is_empty());
288stack.len() - 1
289}
290291#[inline(always)]
292pub fn frame(&self) -> &Frame<'tcx, M::Provenance, M::FrameExtra> {
293self.stack().last().expect("no call frames exist")
294 }
295296#[inline(always)]
297pub fn frame_mut(&mut self) -> &mut Frame<'tcx, M::Provenance, M::FrameExtra> {
298self.stack_mut().last_mut().expect("no call frames exist")
299 }
300301#[inline(always)]
302pub fn body(&self) -> &'tcx mir::Body<'tcx> {
303self.frame().body
304 }
305306#[inline]
307pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
308ty.is_freeze(*self.tcx, self.typing_env)
309 }
310311pub fn load_mir(
312&self,
313 instance: ty::InstanceKind<'tcx>,
314 promoted: Option<mir::Promoted>,
315 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
316{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:316",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(316u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::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!("load mir(instance={0:?}, promoted={1:?})",
instance, promoted) as &dyn Value))])
});
} else { ; }
};trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
317let body = if let Some(promoted) = promoted {
318let def = instance.def_id();
319&self.tcx.promoted_mir(def)[promoted]
320 } else {
321 M::load_mir(self, instance)
322 };
323// do not continue if typeck errors occurred (can only occur in local crate)
324if let Some(err) = body.tainted_by_errors {
325do yeet ::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));
326 }
327interp_ok(body)
328 }
329330/// Call this on things you got out of the MIR (so it is as generic as the current
331 /// stack frame), to bring it into the proper environment for this interpreter.
332pub fn instantiate_from_current_frame_and_normalize_erasing_regions<
333 T: TypeFoldable<TyCtxt<'tcx>>,
334 >(
335&self,
336 value: T,
337 ) -> Result<T, ErrorHandled> {
338self.instantiate_from_frame_and_normalize_erasing_regions(self.frame(), value)
339 }
340341/// Call this on things you got out of the MIR (so it is as generic as the provided
342 /// stack frame), to bring it into the proper environment for this interpreter.
343pub fn instantiate_from_frame_and_normalize_erasing_regions<T: TypeFoldable<TyCtxt<'tcx>>>(
344&self,
345 frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
346 value: T,
347 ) -> Result<T, ErrorHandled> {
348let _trace = <M as
crate::interpret::Machine>::enter_trace_span(||
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("instantiate_from_frame_and_normalize_erasing_regions",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(348u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["frame.instance"],
::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,
&{
#[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(&display(&frame.instance)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
})enter_trace_span!(
349 M,
350"instantiate_from_frame_and_normalize_erasing_regions",
351 %frame.instance
352 );
353frame354 .instance
355 .try_instantiate_mir_and_normalize_erasing_regions(
356*self.tcx,
357self.typing_env,
358 ty::EarlyBinder::bind(value),
359 )
360 .map_err(|_| ErrorHandled::TooGeneric(self.cur_span()))
361 }
362363/// The `args` are assumed to already be in our interpreter "universe".
364pub(super) fn resolve(
365&self,
366 def: DefId,
367 args: GenericArgsRef<'tcx>,
368 ) -> InterpResult<'tcx, ty::Instance<'tcx>> {
369let _trace = <M as
crate::interpret::Machine>::enter_trace_span(||
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("resolve",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(369u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["resolve", "def"],
::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,
&{
#[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(&display(&"try_resolve")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&def) as
&dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
})enter_trace_span!(M, resolve::try_resolve, def = ?def);
370{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:370",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(370u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::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!("resolve: {0:?}, {1:#?}",
def, args) as &dyn Value))])
});
} else { ; }
};trace!("resolve: {:?}, {:#?}", def, args);
371{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:371",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(371u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::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!("typing_env: {0:#?}",
self.typing_env) as &dyn Value))])
});
} else { ; }
};trace!("typing_env: {:#?}", self.typing_env);
372{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:372",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(372u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::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!("args: {0:#?}",
args) as &dyn Value))])
});
} else { ; }
};trace!("args: {:#?}", args);
373match ty::Instance::try_resolve(*self.tcx, self.typing_env, def, args) {
374Ok(Some(instance)) => interp_ok(instance),
375Ok(None) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::TooGeneric)throw_inval!(TooGeneric),
376377// FIXME(eddyb) this could be a bit more specific than `AlreadyReported`.
378Err(error_guaranteed) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::AlreadyReported(ReportedErrorInfo::non_const_eval_error(error_guaranteed)))throw_inval!(AlreadyReported(
379 ReportedErrorInfo::non_const_eval_error(error_guaranteed)
380 )),
381 }
382 }
383384/// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a
385 /// frame which is not `#[track_caller]`. This matches the `caller_location` intrinsic,
386 /// and is primarily intended for the panic machinery.
387pub(crate) fn find_closest_untracked_caller_location(&self) -> Span {
388for frame in self.stack().iter().rev() {
389{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:389",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(389u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::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!("find_closest_untracked_caller_location: checking frame {0:?}",
frame.instance) as &dyn Value))])
});
} else { ; }
};debug!("find_closest_untracked_caller_location: checking frame {:?}", frame.instance);
390391// Assert that the frame we look at is actually executing code currently
392 // (`loc` is `Right` when we are unwinding and the frame does not require cleanup).
393let loc = frame.loc.left().unwrap();
394395// This could be a non-`Call` terminator (such as `Drop`), or not a terminator at all
396 // (such as `box`). Use the normal span by default.
397let mut source_info = *frame.body.source_info(loc);
398399// If this is a `Call` terminator, use the `fn_span` instead.
400let block = &frame.body.basic_blocks[loc.block];
401if loc.statement_index == block.statements.len() {
402{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:402",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(402u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::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!("find_closest_untracked_caller_location: got terminator {0:?} ({1:?})",
block.terminator(), block.terminator().kind) as
&dyn Value))])
});
} else { ; }
};debug!(
403"find_closest_untracked_caller_location: got terminator {:?} ({:?})",
404 block.terminator(),
405 block.terminator().kind,
406 );
407if let mir::TerminatorKind::Call { fn_span, .. } = block.terminator().kind {
408 source_info.span = fn_span;
409 }
410 }
411412let caller_location = if frame.instance.def.requires_caller_location(*self.tcx) {
413// We use `Err(())` as indication that we should continue up the call stack since
414 // this is a `#[track_caller]` function.
415Some(Err(()))
416 } else {
417None
418};
419if let Ok(span) =
420 frame.body.caller_location_span(source_info, caller_location, *self.tcx, Ok)
421 {
422return span;
423 }
424 }
425426::rustc_middle::util::bug::span_bug_fmt(self.cur_span(),
format_args!("no non-`#[track_caller]` frame found"))span_bug!(self.cur_span(), "no non-`#[track_caller]` frame found")427 }
428429/// Returns the actual dynamic size and alignment of the place at the given type.
430 /// Only the "meta" (metadata) part of the place matters.
431 /// This can fail to provide an answer for extern types.
432pub(super) fn size_and_align_from_meta(
433&self,
434 metadata: &MemPlaceMeta<M::Provenance>,
435 layout: &TyAndLayout<'tcx>,
436 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
437if layout.is_sized() {
438return interp_ok(Some((layout.size, layout.align.abi)));
439 }
440match layout.ty.kind() {
441 ty::Adt(..) | ty::Tuple(..) => {
442// First get the size of all statically known fields.
443 // Don't use type_of::sizing_type_of because that expects t to be sized,
444 // and it also rounds up to alignment, which we want to avoid,
445 // as the unsized field's alignment could be smaller.
446if !!layout.ty.is_simd() {
::core::panicking::panic("assertion failed: !layout.ty.is_simd()")
};assert!(!layout.ty.is_simd());
447if !(layout.fields.count() > 0) {
::core::panicking::panic("assertion failed: layout.fields.count() > 0")
};assert!(layout.fields.count() > 0);
448{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/eval_context.rs:448",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(448u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::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!("DST layout: {0:?}",
layout) as &dyn Value))])
});
} else { ; }
};trace!("DST layout: {:?}", layout);
449450let unsized_offset_unadjusted = layout.fields.offset(layout.fields.count() - 1);
451let sized_align = layout.align.abi;
452453// Recurse to get the size of the dynamically sized field (must be
454 // the last field). Can't have foreign types here, how would we
455 // adjust alignment and size for them?
456let field = layout.field(self, layout.fields.count() - 1);
457let Some((unsized_size, mut unsized_align)) =
458self.size_and_align_from_meta(metadata, &field)?
459else {
460// A field with an extern type. We don't know the actual dynamic size
461 // or the alignment.
462return interp_ok(None);
463 };
464465// # First compute the dynamic alignment
466467 // Packed type alignment needs to be capped.
468if let ty::Adt(def, _) = layout.ty.kind()
469 && let Some(packed) = def.repr().pack
470 {
471unsized_align = unsized_align.min(packed);
472 }
473474// Choose max of two known alignments (combined value must
475 // be aligned according to more restrictive of the two).
476let full_align = sized_align.max(unsized_align);
477478// # Then compute the dynamic size
479480let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
481let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
482483// Just for our sanitiy's sake, assert that this is equal to what codegen would compute.
484match (&full_size,
&(unsized_offset_unadjusted + unsized_size).align_to(full_align)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(
485 full_size,
486 (unsized_offset_unadjusted + unsized_size).align_to(full_align)
487 );
488489// Check if this brought us over the size limit.
490if full_size > self.max_size_of_val() {
491do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidMeta(InvalidMetaKind::TooBig));throw_ub!(InvalidMeta(InvalidMetaKind::TooBig));
492 }
493interp_ok(Some((full_size, full_align)))
494 }
495 ty::Dynamic(expected_trait, _) => {
496let vtable = metadata.unwrap_meta().to_pointer(self)?;
497// Read size and align from vtable (already checks size).
498interp_ok(Some(self.get_vtable_size_and_align(vtable, Some(expected_trait))?))
499 }
500501 ty::Slice(_) | ty::Str => {
502let len = metadata.unwrap_meta().to_target_usize(self)?;
503let elem = layout.field(self, 0);
504505// Make sure the slice is not too big.
506let size = elem.size.bytes().saturating_mul(len); // we rely on `max_size_of_val` being smaller than `u64::MAX`.
507let size = Size::from_bytes(size);
508if size > self.max_size_of_val() {
509do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidMeta(InvalidMetaKind::SliceTooBig));throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig));
510 }
511interp_ok(Some((size, elem.align.abi)))
512 }
513514 ty::Foreign(_) => interp_ok(None),
515516_ => ::rustc_middle::util::bug::span_bug_fmt(self.cur_span(),
format_args!("size_and_align_of::<{0}> not supported", layout.ty))span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty),
517 }
518 }
519#[inline]
520pub fn size_and_align_of_val(
521&self,
522 val: &impl Projectable<'tcx, M::Provenance>,
523 ) -> InterpResult<'tcx, Option<(Size, Align)>> {
524self.size_and_align_from_meta(&val.meta(), &val.layout())
525 }
526527/// Jump to the given block.
528#[inline]
529pub fn go_to_block(&mut self, target: mir::BasicBlock) {
530self.frame_mut().loc = Left(mir::Location { block: target, statement_index: 0 });
531 }
532533/// *Return* to the given `target` basic block.
534 /// Do *not* use for unwinding! Use `unwind_to_block` instead.
535 ///
536 /// If `target` is `None`, that indicates the function cannot return, so we raise UB.
537pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
538if let Some(target) = target {
539self.go_to_block(target);
540interp_ok(())
541 } else {
542do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Unreachable)throw_ub!(Unreachable)543 }
544 }
545546/// *Unwind* to the given `target` basic block.
547 /// Do *not* use for returning! Use `return_to_block` instead.
548 ///
549 /// If `target` is `UnwindAction::Continue`, that indicates the function does not need cleanup
550 /// during unwinding, and we will just keep propagating that upwards.
551 ///
552 /// If `target` is `UnwindAction::Unreachable`, that indicates the function does not allow
553 /// unwinding, and doing so is UB.
554#[cold] // usually we have normal returns, not unwinding
555pub fn unwind_to_block(&mut self, target: mir::UnwindAction) -> InterpResult<'tcx> {
556self.frame_mut().loc = match target {
557 mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }),
558 mir::UnwindAction::Continue => Right(self.frame_mut().body.span),
559 mir::UnwindAction::Unreachable => {
560do yeet {
::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
msg: || fluent::const_eval_unreachable_unwind,
add_args: Box::new(move |mut set_arg| {}),
}))
};throw_ub_custom!(fluent::const_eval_unreachable_unwind);
561 }
562 mir::UnwindAction::Terminate(reason) => {
563self.frame_mut().loc = Right(self.frame_mut().body.span);
564 M::unwind_terminate(self, reason)?;
565// This might have pushed a new stack frame, or it terminated execution.
566 // Either way, `loc` will not be updated.
567return interp_ok(());
568 }
569 };
570interp_ok(())
571 }
572573/// Call a query that can return `ErrorHandled`. Should be used for statics and other globals.
574 /// (`mir::Const`/`ty::Const` have `eval` methods that can be used directly instead.)
575pub fn ctfe_query<T>(
576&self,
577 query: impl FnOnce(TyCtxtAt<'tcx>) -> Result<T, ErrorHandled>,
578 ) -> Result<T, ErrorHandled> {
579// Use a precise span for better cycle errors.
580query(self.tcx.at(self.cur_span())).map_err(|err| {
581err.emit_note(*self.tcx);
582err583 })
584 }
585586pub fn eval_global(
587&self,
588 instance: ty::Instance<'tcx>,
589 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
590let gid = GlobalId { instance, promoted: None };
591let val = if self.tcx.is_static(gid.instance.def_id()) {
592let alloc_id = self.tcx.reserve_and_set_static_alloc(gid.instance.def_id());
593594let ty = instance.ty(self.tcx.tcx, self.typing_env);
595 mir::ConstAlloc { alloc_id, ty }
596 } else {
597self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.typing_env.as_query_input(gid)))?
598};
599self.raw_const_to_mplace(val)
600 }
601602pub fn eval_mir_constant(
603&self,
604 val: &mir::Const<'tcx>,
605 span: Span,
606 layout: Option<TyAndLayout<'tcx>>,
607 ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
608let _trace = <M as
crate::interpret::Machine>::enter_trace_span(||
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("const_eval",
"rustc_const_eval::interpret::eval_context",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/eval_context.rs"),
::tracing_core::__macro_support::Option::Some(608u32),
::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::eval_context"),
::tracing_core::field::FieldSet::new(&["const_eval", "val"],
::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,
&{
#[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(&display(&"eval_mir_constant")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&val) as
&dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
})enter_trace_span!(M, const_eval::eval_mir_constant, ?val);
609let const_val = val.eval(*self.tcx, self.typing_env, span).map_err(|err| {
610if M::ALL_CONSTS_ARE_PRECHECKED {
611match err {
612 ErrorHandled::TooGeneric(..) => {},
613 ErrorHandled::Reported(reported, span) => {
614if reported.is_allowed_in_infallible() {
615// These errors can just sometimes happen, even when the expression
616 // is nominally "infallible", e.g. when running out of memory
617 // or when some layout could not be computed.
618} else {
619// Looks like the const is not captured by `required_consts`, that's bad.
620::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("interpret const eval failure of {0:?} which is not in required_consts",
val));span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
621 }
622 }
623 }
624 }
625err.emit_note(*self.tcx);
626err627 })?;
628self.const_val_to_op(const_val, val.ty(), layout)
629 }
630631#[must_use]
632pub fn dump_place(&self, place: &PlaceTy<'tcx, M::Provenance>) -> PlacePrinter<'_, 'tcx, M> {
633PlacePrinter { ecx: self, place: *place.place() }
634 }
635636#[must_use]
637pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
638Frame::generate_stacktrace_from_stack(self.stack())
639 }
640641pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
642where
643F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
644 F2: rustc_apfloat::Float,
645 {
646if f.is_nan() { M::generate_nan(self, inputs) } else { f }
647 }
648}
649650#[doc(hidden)]
651/// Helper struct for the `dump_place` function.
652pub struct PlacePrinter<'a, 'tcx, M: Machine<'tcx>> {
653 ecx: &'a InterpCx<'tcx, M>,
654 place: Place<M::Provenance>,
655}
656657impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debugfor PlacePrinter<'a, 'tcx, M> {
658fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
659match self.place {
660 Place::Local { local, offset, locals_addr } => {
661if true {
match (&locals_addr, &self.ecx.frame().locals_addr()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(locals_addr, self.ecx.frame().locals_addr());
662let mut allocs = Vec::new();
663fmt.write_fmt(format_args!("{0:?}", local))write!(fmt, "{local:?}")?;
664if let Some(offset) = offset {
665fmt.write_fmt(format_args!("+{0:#x}", offset.bytes()))write!(fmt, "+{:#x}", offset.bytes())?;
666 }
667fmt.write_fmt(format_args!(":"))write!(fmt, ":")?;
668669self.ecx.frame().locals[local].print(&mut allocs, fmt)?;
670671fmt.write_fmt(format_args!(": {0:?}",
self.ecx.dump_allocs(allocs.into_iter().flatten().collect())))write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs.into_iter().flatten().collect()))672 }
673 Place::Ptr(mplace) => match mplace.ptr.provenance.and_then(Provenance::get_alloc_id) {
674Some(alloc_id) => {
675fmt.write_fmt(format_args!("by ref {0:?}: {1:?}", mplace.ptr,
self.ecx.dump_alloc(alloc_id)))write!(fmt, "by ref {:?}: {:?}", mplace.ptr, self.ecx.dump_alloc(alloc_id))676 }
677 ptr => fmt.write_fmt(format_args!(" integral by ref: {0:?}", ptr))write!(fmt, " integral by ref: {ptr:?}"),
678 },
679 }
680 }
681}