1use either::{Left, Right};
2use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
3use rustc_hir::def_id::DefId;
4use rustc_hir::limit::Limit;
5use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
6use rustc_middle::query::TyCtxtAt;
7use rustc_middle::ty::layout::{
8self, FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf,
9LayoutOfHelpers, TyAndLayout,
10};
11use rustc_middle::ty::{
12self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingEnv, TypingMode,
13Variance,
14};
15use rustc_middle::{bug, mir, span_bug};
16use rustc_span::Span;
17use rustc_target::callconv::FnAbi;
18use tracing::{debug, trace};
1920use super::{
21Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
22MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
23err_inval, interp_ok, throw_inval, throw_ub, throw_ub_format,
24};
25use crate::{enter_trace_span, util};
2627pub struct InterpCx<'tcx, M: Machine<'tcx>> {
28/// Stores the `Machine` instance.
29 ///
30 /// Note: the stack is provided by the machine.
31pub machine: M,
3233/// The results of the type checker, from rustc.
34 /// The span in this is the "root" of the evaluation, i.e., the const
35 /// we are evaluating (if this is CTFE).
36pub tcx: TyCtxtAt<'tcx>,
3738/// The current context in case we're evaluating in a
39 /// polymorphic context. This always uses `ty::TypingMode::PostAnalysis`.
40pub(super) typing_env: ty::TypingEnv<'tcx>,
4142/// The virtual memory system.
43pub memory: Memory<'tcx, M>,
4445/// The recursion limit (cached from `tcx.recursion_limit(())`)
46pub recursion_limit: Limit,
47}
4849impl<'tcx, M: Machine<'tcx>> HasDataLayoutfor InterpCx<'tcx, M> {
50#[inline]
51fn data_layout(&self) -> &TargetDataLayout {
52&self.tcx.data_layout
53 }
54}
5556impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
57where
58M: Machine<'tcx>,
59{
60#[inline]
61fn tcx(&self) -> TyCtxt<'tcx> {
62*self.tcx
63 }
64}
6566impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
67where
68M: Machine<'tcx>,
69{
70fn typing_env(&self) -> ty::TypingEnv<'tcx> {
71self.typing_env
72 }
73}
7475impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
76type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
7778#[inline]
79fn layout_tcx_at_span(&self) -> Span {
80// Using the cheap root span for performance.
81self.tcx.span
82 }
8384#[inline]
85fn handle_layout_err(
86&self,
87mut err: LayoutError<'tcx>,
88_: Span,
89_: Ty<'tcx>,
90 ) -> InterpErrorKind<'tcx> {
91// FIXME(#149283): This is really hacky and is only used to hide type
92 // system bugs. We use it as a temporary fix for #149081.
93 //
94 // While it's expected that we sometimes get ambiguity errors when
95 // entering another generic environment while the current environment
96 // itself is still generic, we should never fail to entirely prove
97 // something.
98match err {
99 LayoutError::NormalizationFailure(ty, _) => {
100if ty.has_non_region_param() {
101err = LayoutError::TooGeneric(ty);
102 }
103 }
104105 LayoutError::Unknown(_)
106 | LayoutError::SizeOverflow(_)
107 | LayoutError::InvalidSimd { .. }
108 | LayoutError::TooGeneric(_)
109 | LayoutError::ReferencesError(_) => {}
110 }
111::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::Layout(err))err_inval!(Layout(err))112 }
113}
114115impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
116type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
117118fn handle_fn_abi_err(
119&self,
120 err: FnAbiError<'tcx>,
121 _span: Span,
122 _fn_abi_request: FnAbiRequest<'tcx>,
123 ) -> InterpErrorKind<'tcx> {
124match err {
125 FnAbiError::Layout(err) => ::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::Layout(err))err_inval!(Layout(err)),
126 }
127 }
128}
129130impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
131/// This inherent method takes priority over the trait method with the same name in LayoutOf,
132 /// and allows wrapping the actual [LayoutOf::layout_of] with a tracing span.
133 /// See [LayoutOf::layout_of] for the original documentation.
134#[inline(always)]
135pub fn layout_of(&self, ty: Ty<'tcx>) -> Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>> {
136let _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(136u32),
::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());
137 LayoutOf::layout_of(self, ty)
138 }
139140/// This inherent method takes priority over the trait method with the same name in FnAbiOf,
141 /// and allows wrapping the actual [FnAbiOf::fn_abi_of_fn_ptr] with a tracing span.
142 /// See [FnAbiOf::fn_abi_of_fn_ptr] for the original documentation.
143#[inline(always)]
144pub fn fn_abi_of_fn_ptr(
145&self,
146 sig: ty::PolyFnSig<'tcx>,
147 extra_args: &'tcx ty::List<Ty<'tcx>>,
148 ) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
149let _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(149u32),
::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);
150 FnAbiOf::fn_abi_of_fn_ptr(self, sig, extra_args)
151 }
152153/// This inherent method takes priority over the trait method with the same name in FnAbiOf,
154 /// and allows wrapping the actual [FnAbiOf::fn_abi_of_instance_no_deduced_attrs] with a tracing span.
155 /// See [FnAbiOf::fn_abi_of_instance_no_deduced_attrs] for the original documentation.
156#[inline(always)]
157pub fn fn_abi_of_instance_no_deduced_attrs(
158&self,
159 instance: ty::Instance<'tcx>,
160 extra_args: &'tcx ty::List<Ty<'tcx>>,
161 ) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
162let _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(162u32),
::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);
163 FnAbiOf::fn_abi_of_instance_no_deduced_attrs(self, instance, extra_args)
164 }
165}
166167/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
168/// This test should be symmetric, as it is primarily about layout compatibility.
169pub(super) fn mir_assign_valid_types<'tcx>(
170 tcx: TyCtxt<'tcx>,
171 typing_env: TypingEnv<'tcx>,
172 src: TyAndLayout<'tcx>,
173 dest: TyAndLayout<'tcx>,
174) -> bool {
175// Type-changing assignments can happen when subtyping is used. While
176 // all normal lifetimes are erased, higher-ranked types with their
177 // late-bound lifetimes are still around and can lead to type
178 // differences.
179if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
180// Make sure the layout is equal, too -- just to be safe. Miri really
181 // needs layout equality. For performance reason we skip this check when
182 // the types are equal. Equal types *can* have different layouts when
183 // enum downcast is involved (as enum variants carry the type of the
184 // enum), but those should never occur in assignments.
185if truecfg!(debug_assertions) || src.ty != dest.ty {
186match (&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);
187 }
188true
189} else {
190false
191}
192}
193194/// Use the already known layout if given (but sanity check in debug mode),
195/// or compute the layout.
196#[cfg_attr(not(debug_assertions), inline(always))]
197pub(super) fn from_known_layout<'tcx>(
198 tcx: TyCtxtAt<'tcx>,
199 typing_env: TypingEnv<'tcx>,
200 known_layout: Option<TyAndLayout<'tcx>>,
201 compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
202) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
203match known_layout {
204None => compute(),
205Some(known_layout) => {
206if truecfg!(debug_assertions) {
207let check_layout = compute()?;
208if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
209::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!(
210tcx.span,
211"expected type differs from actual type.\nexpected: {}\nactual: {}",
212 known_layout.ty,
213 check_layout.ty,
214 );
215 }
216 }
217interp_ok(known_layout)
218 }
219 }
220}
221222/// Turn the given error into a human-readable string. Expects the string to be printed, so if
223/// `RUSTC_CTFE_BACKTRACE` is set this will show a backtrace of the rustc internals that
224/// triggered the error.
225///
226/// This is NOT the preferred way to render an error; use `report` from `const_eval` instead.
227/// However, this is useful when error messages appear in ICEs.
228pub fn format_interp_error<'tcx>(e: InterpErrorInfo<'tcx>) -> String {
229let (e, backtrace) = e.into_parts();
230backtrace.print_backtrace();
231e.to_string()
232}
233234impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
235pub fn new(
236 tcx: TyCtxt<'tcx>,
237 root_span: Span,
238 typing_env: ty::TypingEnv<'tcx>,
239 machine: M,
240 ) -> Self {
241// Const eval always happens in post analysis mode in order to be able to use the hidden types of
242 // opaque types. This is needed for trivial things like `size_of`, but also for using associated
243 // types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
244 // already been revealed, so we'd be able to at least partially observe the hidden types anyways.
245if truecfg!(debug_assertions) {
246match typing_env.typing_mode() {
247TypingMode::PostAnalysis => {}
248TypingMode::Coherence249 | TypingMode::Analysis { .. }
250 | TypingMode::Borrowck { .. }
251 | TypingMode::PostBorrowckAnalysis { .. } => {
252::rustc_middle::util::bug::bug_fmt(format_args!("Const eval should always happens in PostAnalysis mode."));bug!("Const eval should always happens in PostAnalysis mode.");
253 }
254 }
255 }
256257InterpCx {
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::Ub(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unwinding past a stack frame that does not allow unwinding"))
})));throw_ub_format!("unwinding past a stack frame that does not allow unwinding");
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(), *self.tcx)
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}