1use std::any::Any;
2use std::backtrace::Backtrace;
3use std::borrow::Cow;
4use std::{convert, fmt, mem, ops};
56use either::Either;
7use rustc_abi::{Align, Size, VariantIdx};
8use rustc_data_structures::sync::Lock;
9use rustc_errors::{DiagArgValue, ErrorGuaranteed, IntoDiagArg};
10use rustc_macros::{HashStable, TyDecodable, TyEncodable};
11use rustc_session::CtfeBacktrace;
12use rustc_span::def_id::DefId;
13use rustc_span::{DUMMY_SP, Span, Symbol};
1415use super::{AllocId, AllocRange, ConstAllocation, Pointer, Scalar};
16use crate::error;
17use crate::mir::interpret::CtfeProvenance;
18use crate::mir::{ConstAlloc, ConstValue};
19use crate::ty::{self, Ty, TyCtxt, ValTree, layout, tls};
2021#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ErrorHandled {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ErrorHandled::Reported(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"Reported", __self_0, &__self_1),
ErrorHandled::TooGeneric(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"TooGeneric", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for ErrorHandled { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ErrorHandled {
#[inline]
fn clone(&self) -> ErrorHandled {
let _: ::core::clone::AssertParamIsClone<ReportedErrorInfo>;
let _: ::core::clone::AssertParamIsClone<Span>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for ErrorHandled {
#[inline]
fn eq(&self, other: &ErrorHandled) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(ErrorHandled::Reported(__self_0, __self_1),
ErrorHandled::Reported(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(ErrorHandled::TooGeneric(__self_0),
ErrorHandled::TooGeneric(__arg1_0)) => __self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ErrorHandled {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ReportedErrorInfo>;
let _: ::core::cmp::AssertParamIsEq<Span>;
}
}Eq, const _: () =
{
impl<'__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for ErrorHandled {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
ErrorHandled::Reported(ref __binding_0, ref __binding_1) =>
{
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
ErrorHandled::TooGeneric(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for ErrorHandled {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
ErrorHandled::Reported(ref __binding_0, ref __binding_1) =>
{
0usize
}
ErrorHandled::TooGeneric(ref __binding_0) => { 1usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
ErrorHandled::Reported(ref __binding_0, ref __binding_1) =>
{
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
ErrorHandled::TooGeneric(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for ErrorHandled {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
ErrorHandled::Reported(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
1usize => {
ErrorHandled::TooGeneric(::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `ErrorHandled`, expected 0..2, actual {0}",
n));
}
}
}
}
};TyDecodable)]
22pub enum ErrorHandled {
23/// Already reported an error for this evaluation, and the compilation is
24 /// *guaranteed* to fail. Warnings/lints *must not* produce `Reported`.
25Reported(ReportedErrorInfo, Span),
26/// Don't emit an error, the evaluation failed because the MIR was generic
27 /// and the args didn't fully monomorphize it.
28TooGeneric(Span),
29}
3031impl From<ReportedErrorInfo> for ErrorHandled {
32#[inline]
33fn from(error: ReportedErrorInfo) -> ErrorHandled {
34 ErrorHandled::Reported(error, DUMMY_SP)
35 }
36}
3738impl ErrorHandled {
39pub(crate) fn with_span(self, span: Span) -> Self {
40match self {
41 ErrorHandled::Reported(err, _span) => ErrorHandled::Reported(err, span),
42 ErrorHandled::TooGeneric(_span) => ErrorHandled::TooGeneric(span),
43 }
44 }
4546pub fn emit_note(&self, tcx: TyCtxt<'_>) {
47match self {
48&ErrorHandled::Reported(err, span) => {
49if !err.allowed_in_infallible && !span.is_dummy() {
50tcx.dcx().emit_note(error::ErroneousConstant { span });
51 }
52 }
53&ErrorHandled::TooGeneric(_) => {}
54 }
55 }
56}
5758#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ReportedErrorInfo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ReportedErrorInfo", "error", &self.error,
"allowed_in_infallible", &&self.allowed_in_infallible)
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for ReportedErrorInfo { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ReportedErrorInfo {
#[inline]
fn clone(&self) -> ReportedErrorInfo {
let _: ::core::clone::AssertParamIsClone<ErrorGuaranteed>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for ReportedErrorInfo {
#[inline]
fn eq(&self, other: &ReportedErrorInfo) -> bool {
self.allowed_in_infallible == other.allowed_in_infallible &&
self.error == other.error
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ReportedErrorInfo {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ErrorGuaranteed>;
let _: ::core::cmp::AssertParamIsEq<bool>;
}
}Eq, const _: () =
{
impl<'__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for ReportedErrorInfo {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
ReportedErrorInfo {
error: ref __binding_0,
allowed_in_infallible: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for ReportedErrorInfo {
fn encode(&self, __encoder: &mut __E) {
match *self {
ReportedErrorInfo {
error: ref __binding_0,
allowed_in_infallible: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for ReportedErrorInfo {
fn decode(__decoder: &mut __D) -> Self {
ReportedErrorInfo {
error: ::rustc_serialize::Decodable::decode(__decoder),
allowed_in_infallible: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable)]
59pub struct ReportedErrorInfo {
60 error: ErrorGuaranteed,
61/// Whether this error is allowed to show up even in otherwise "infallible" promoteds.
62 /// This is for things like overflows during size computation or resource exhaustion.
63allowed_in_infallible: bool,
64}
6566impl ReportedErrorInfo {
67#[inline]
68pub fn const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo {
69ReportedErrorInfo { allowed_in_infallible: false, error }
70 }
7172/// Use this when the error that led to this is *not* a const-eval error
73 /// (e.g., a layout or type checking error).
74#[inline]
75pub fn non_const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo {
76ReportedErrorInfo { allowed_in_infallible: true, error }
77 }
7879/// Use this when the error that led to this *is* a const-eval error, but
80 /// we do allow it to occur in infallible constants (e.g., resource exhaustion).
81#[inline]
82pub fn allowed_in_infallible(error: ErrorGuaranteed) -> ReportedErrorInfo {
83ReportedErrorInfo { allowed_in_infallible: true, error }
84 }
8586pub fn is_allowed_in_infallible(&self) -> bool {
87self.allowed_in_infallible
88 }
89}
9091impl From<ReportedErrorInfo> for ErrorGuaranteed {
92#[inline]
93fn from(val: ReportedErrorInfo) -> Self {
94val.error
95 }
96}
9798/// An error type for the `const_to_valtree` query. Some error should be reported with a "use-site span",
99/// which means the query cannot emit the error, so those errors are represented as dedicated variants here.
100#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ValTreeCreationError<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ValTreeCreationError::NodesOverflow =>
::core::fmt::Formatter::write_str(f, "NodesOverflow"),
ValTreeCreationError::InvalidConst =>
::core::fmt::Formatter::write_str(f, "InvalidConst"),
ValTreeCreationError::NonSupportedType(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"NonSupportedType", &__self_0),
ValTreeCreationError::ErrorHandled(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ErrorHandled", &__self_0),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ValTreeCreationError<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for ValTreeCreationError<'tcx> {
#[inline]
fn clone(&self) -> ValTreeCreationError<'tcx> {
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ErrorHandled>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for ValTreeCreationError<'tcx> {
#[inline]
fn eq(&self, other: &ValTreeCreationError<'tcx>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(ValTreeCreationError::NonSupportedType(__self_0),
ValTreeCreationError::NonSupportedType(__arg1_0)) =>
__self_0 == __arg1_0,
(ValTreeCreationError::ErrorHandled(__self_0),
ValTreeCreationError::ErrorHandled(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for ValTreeCreationError<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<ErrorHandled>;
}
}Eq, const _: () =
{
impl<'tcx, '__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for ValTreeCreationError<'tcx> {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
ValTreeCreationError::NodesOverflow => {}
ValTreeCreationError::InvalidConst => {}
ValTreeCreationError::NonSupportedType(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
ValTreeCreationError::ErrorHandled(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for ValTreeCreationError<'tcx> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
ValTreeCreationError::NodesOverflow => { 0usize }
ValTreeCreationError::InvalidConst => { 1usize }
ValTreeCreationError::NonSupportedType(ref __binding_0) => {
2usize
}
ValTreeCreationError::ErrorHandled(ref __binding_0) => {
3usize
}
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
ValTreeCreationError::NodesOverflow => {}
ValTreeCreationError::InvalidConst => {}
ValTreeCreationError::NonSupportedType(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
ValTreeCreationError::ErrorHandled(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for ValTreeCreationError<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { ValTreeCreationError::NodesOverflow }
1usize => { ValTreeCreationError::InvalidConst }
2usize => {
ValTreeCreationError::NonSupportedType(::rustc_serialize::Decodable::decode(__decoder))
}
3usize => {
ValTreeCreationError::ErrorHandled(::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `ValTreeCreationError`, expected 0..4, actual {0}",
n));
}
}
}
}
};TyDecodable)]
101pub enum ValTreeCreationError<'tcx> {
102/// The constant is too big to be valtree'd.
103NodesOverflow,
104/// The constant references mutable or external memory, so it cannot be valtree'd.
105InvalidConst,
106/// Values of this type, or this particular value, are not supported as valtrees.
107NonSupportedType(Ty<'tcx>),
108/// The error has already been handled by const evaluation.
109ErrorHandled(ErrorHandled),
110}
111112impl<'tcx> From<ErrorHandled> for ValTreeCreationError<'tcx> {
113fn from(err: ErrorHandled) -> Self {
114 ValTreeCreationError::ErrorHandled(err)
115 }
116}
117118impl<'tcx> From<InterpErrorInfo<'tcx>> for ValTreeCreationError<'tcx> {
119fn from(err: InterpErrorInfo<'tcx>) -> Self {
120// An error occurred outside the const-eval query, as part of constructing the valtree. We
121 // don't currently preserve the details of this error, since `InterpErrorInfo` cannot be put
122 // into a query result and it can only be access of some mutable or external memory.
123let (_kind, backtrace) = err.into_parts();
124backtrace.print_backtrace();
125 ValTreeCreationError::InvalidConst126 }
127}
128129impl<'tcx> ValTreeCreationError<'tcx> {
130pub(crate) fn with_span(self, span: Span) -> Self {
131use ValTreeCreationError::*;
132match self {
133ErrorHandled(handled) => ErrorHandled(handled.with_span(span)),
134 other => other,
135 }
136 }
137}
138139pub type EvalToAllocationRawResult<'tcx> = Result<ConstAlloc<'tcx>, ErrorHandled>;
140pub type EvalStaticInitializerRawResult<'tcx> = Result<ConstAllocation<'tcx>, ErrorHandled>;
141pub type EvalToConstValueResult<'tcx> = Result<ConstValue, ErrorHandled>;
142pub type EvalToValTreeResult<'tcx> = Result<ValTree<'tcx>, ValTreeCreationError<'tcx>>;
143144#[cfg(target_pointer_width = "64")]
145const _: [(); 8] = [(); ::std::mem::size_of::<InterpErrorInfo<'_>>()];rustc_data_structures::static_assert_size!(InterpErrorInfo<'_>, 8);
146147/// Packages the kind of error we got from the const code interpreter
148/// up with a Rust-level backtrace of where the error occurred.
149/// These should always be constructed by calling `.into()` on
150/// an `InterpError`. In `rustc_mir::interpret`, we have `throw_err_*`
151/// macros for this.
152///
153/// Interpreter errors must *not* be silently discarded (that will lead to a panic). Instead,
154/// explicitly call `discard_err` if this is really the right thing to do. Note that if
155/// this happens during const-eval or in Miri, it could lead to a UB error being lost!
156#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InterpErrorInfo<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InterpErrorInfo", &&self.0)
}
}Debug)]
157pub struct InterpErrorInfo<'tcx>(Box<InterpErrorInfoInner<'tcx>>);
158159#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InterpErrorInfoInner<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"InterpErrorInfoInner", "kind", &self.kind, "backtrace",
&&self.backtrace)
}
}Debug)]
160struct InterpErrorInfoInner<'tcx> {
161 kind: InterpErrorKind<'tcx>,
162 backtrace: InterpErrorBacktrace,
163}
164165#[derive(#[automatically_derived]
impl ::core::fmt::Debug for InterpErrorBacktrace {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"InterpErrorBacktrace", "backtrace", &&self.backtrace)
}
}Debug)]
166pub struct InterpErrorBacktrace {
167 backtrace: Option<Box<Backtrace>>,
168}
169170impl InterpErrorBacktrace {
171pub fn new() -> InterpErrorBacktrace {
172let capture_backtrace = tls::with_opt(|tcx| {
173if let Some(tcx) = tcx {
174*Lock::borrow(&tcx.sess.ctfe_backtrace)
175 } else {
176 CtfeBacktrace::Disabled177 }
178 });
179180let backtrace = match capture_backtrace {
181 CtfeBacktrace::Disabled => None,
182 CtfeBacktrace::Capture => Some(Box::new(Backtrace::force_capture())),
183 CtfeBacktrace::Immediate => {
184// Print it now.
185let backtrace = Backtrace::force_capture();
186print_backtrace(&backtrace);
187None188 }
189 };
190191InterpErrorBacktrace { backtrace }
192 }
193194pub fn print_backtrace(&self) {
195if let Some(backtrace) = self.backtrace.as_ref() {
196print_backtrace(backtrace);
197 }
198 }
199}
200201impl<'tcx> InterpErrorInfo<'tcx> {
202pub fn into_parts(self) -> (InterpErrorKind<'tcx>, InterpErrorBacktrace) {
203let InterpErrorInfo(box InterpErrorInfoInner { kind, backtrace }) = self;
204 (kind, backtrace)
205 }
206207pub fn into_kind(self) -> InterpErrorKind<'tcx> {
208self.0.kind
209 }
210211pub fn from_parts(kind: InterpErrorKind<'tcx>, backtrace: InterpErrorBacktrace) -> Self {
212Self(Box::new(InterpErrorInfoInner { kind, backtrace }))
213 }
214215#[inline]
216pub fn kind(&self) -> &InterpErrorKind<'tcx> {
217&self.0.kind
218 }
219}
220221fn print_backtrace(backtrace: &Backtrace) {
222{
::std::io::_eprint(format_args!("\n\nAn error occurred in the MIR interpreter:\n{0}\n",
backtrace));
};eprintln!("\n\nAn error occurred in the MIR interpreter:\n{backtrace}");
223}
224225impl From<ErrorHandled> for InterpErrorInfo<'_> {
226fn from(err: ErrorHandled) -> Self {
227 InterpErrorKind::InvalidProgram(match err {
228 ErrorHandled::Reported(r, _span) => InvalidProgramInfo::AlreadyReported(r),
229 ErrorHandled::TooGeneric(_span) => InvalidProgramInfo::TooGeneric,
230 })
231 .into()
232 }
233}
234235impl<'tcx> From<InterpErrorKind<'tcx>> for InterpErrorInfo<'tcx> {
236fn from(kind: InterpErrorKind<'tcx>) -> Self {
237InterpErrorInfo(Box::new(InterpErrorInfoInner {
238kind,
239 backtrace: InterpErrorBacktrace::new(),
240 }))
241 }
242}
243244/// Details of why a pointer had to be in-bounds.
245#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CheckInAllocMsg {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
CheckInAllocMsg::MemoryAccess => "MemoryAccess",
CheckInAllocMsg::InboundsPointerArithmetic =>
"InboundsPointerArithmetic",
CheckInAllocMsg::Dereferenceable => "Dereferenceable",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for CheckInAllocMsg { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CheckInAllocMsg {
#[inline]
fn clone(&self) -> CheckInAllocMsg { *self }
}Clone)]
246pub enum CheckInAllocMsg {
247/// We are accessing memory.
248MemoryAccess,
249/// We are doing pointer arithmetic.
250InboundsPointerArithmetic,
251/// None of the above -- generic/unspecific inbounds test.
252Dereferenceable,
253}
254255impl fmt::Displayfor CheckInAllocMsg {
256fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257use CheckInAllocMsg::*;
258match self {
259MemoryAccess => f.write_fmt(format_args!("memory access failed"))write!(f, "memory access failed"),
260InboundsPointerArithmetic => f.write_fmt(format_args!("in-bounds pointer arithmetic failed"))write!(f, "in-bounds pointer arithmetic failed"),
261Dereferenceable => f.write_fmt(format_args!("pointer not dereferenceable"))write!(f, "pointer not dereferenceable"),
262 }
263 }
264}
265266/// Details of which pointer is not aligned.
267#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CheckAlignMsg {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
CheckAlignMsg::AccessedPtr => "AccessedPtr",
CheckAlignMsg::BasedOn => "BasedOn",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for CheckAlignMsg { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CheckAlignMsg {
#[inline]
fn clone(&self) -> CheckAlignMsg { *self }
}Clone)]
268pub enum CheckAlignMsg {
269/// The accessed pointer did not have proper alignment.
270AccessedPtr,
271/// The access occurred with a place that was based on a misaligned pointer.
272BasedOn,
273}
274275#[derive(#[automatically_derived]
impl ::core::fmt::Debug for InvalidMetaKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
InvalidMetaKind::SliceTooBig => "SliceTooBig",
InvalidMetaKind::TooBig => "TooBig",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for InvalidMetaKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for InvalidMetaKind {
#[inline]
fn clone(&self) -> InvalidMetaKind { *self }
}Clone)]
276pub enum InvalidMetaKind {
277/// Size of a `[T]` is too big
278SliceTooBig,
279/// Size of a DST is too big
280TooBig,
281}
282283impl IntoDiagArgfor InvalidMetaKind {
284fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
285 DiagArgValue::Str(Cow::Borrowed(match self {
286 InvalidMetaKind::SliceTooBig => "slice_too_big",
287 InvalidMetaKind::TooBig => "too_big",
288 }))
289 }
290}
291292/// Details of an access to uninitialized bytes / bad pointer bytes where it is not allowed.
293#[derive(#[automatically_derived]
impl ::core::fmt::Debug for BadBytesAccess {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"BadBytesAccess", "access", &self.access, "bad", &&self.bad)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for BadBytesAccess {
#[inline]
fn clone(&self) -> BadBytesAccess {
let _: ::core::clone::AssertParamIsClone<AllocRange>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for BadBytesAccess { }Copy)]
294pub struct BadBytesAccess {
295/// Range of the original memory access.
296pub access: AllocRange,
297/// Range of the bad memory that was encountered. (Might not be maximal.)
298pub bad: AllocRange,
299}
300301/// Information about a size mismatch.
302#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ScalarSizeMismatch {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ScalarSizeMismatch", "target_size", &self.target_size,
"data_size", &&self.data_size)
}
}Debug)]
303pub struct ScalarSizeMismatch {
304pub target_size: u64,
305pub data_size: u64,
306}
307308/// Information about a misaligned pointer.
309#[derive(#[automatically_derived]
impl ::core::marker::Copy for Misalignment { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Misalignment {
#[inline]
fn clone(&self) -> Misalignment {
let _: ::core::clone::AssertParamIsClone<Align>;
*self
}
}Clone, #[automatically_derived]
impl ::core::hash::Hash for Misalignment {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.has, state);
::core::hash::Hash::hash(&self.required, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for Misalignment {
#[inline]
fn eq(&self, other: &Misalignment) -> bool {
self.has == other.has && self.required == other.required
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Misalignment {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Align>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for Misalignment {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Misalignment",
"has", &self.has, "required", &&self.required)
}
}Debug)]
310pub struct Misalignment {
311pub has: Align,
312pub required: Align,
313}
314315/// Error information for when the program caused Undefined Behavior.
316#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for UndefinedBehaviorInfo<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
UndefinedBehaviorInfo::Ub(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Ub",
&__self_0),
UndefinedBehaviorInfo::ValidationError {
orig_ty: __self_0,
path: __self_1,
msg: __self_2,
ptr_bytes_warning: __self_3 } =>
::core::fmt::Formatter::debug_struct_field4_finish(f,
"ValidationError", "orig_ty", __self_0, "path", __self_1,
"msg", __self_2, "ptr_bytes_warning", &__self_3),
UndefinedBehaviorInfo::Unreachable =>
::core::fmt::Formatter::write_str(f, "Unreachable"),
UndefinedBehaviorInfo::BoundsCheckFailed {
len: __self_0, index: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"BoundsCheckFailed", "len", __self_0, "index", &__self_1),
UndefinedBehaviorInfo::DivisionByZero =>
::core::fmt::Formatter::write_str(f, "DivisionByZero"),
UndefinedBehaviorInfo::RemainderByZero =>
::core::fmt::Formatter::write_str(f, "RemainderByZero"),
UndefinedBehaviorInfo::DivisionOverflow =>
::core::fmt::Formatter::write_str(f, "DivisionOverflow"),
UndefinedBehaviorInfo::RemainderOverflow =>
::core::fmt::Formatter::write_str(f, "RemainderOverflow"),
UndefinedBehaviorInfo::PointerArithOverflow =>
::core::fmt::Formatter::write_str(f, "PointerArithOverflow"),
UndefinedBehaviorInfo::ArithOverflow { intrinsic: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"ArithOverflow", "intrinsic", &__self_0),
UndefinedBehaviorInfo::ShiftOverflow {
intrinsic: __self_0, shift_amount: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ShiftOverflow", "intrinsic", __self_0, "shift_amount",
&__self_1),
UndefinedBehaviorInfo::InvalidMeta(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidMeta", &__self_0),
UndefinedBehaviorInfo::UnterminatedCString(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UnterminatedCString", &__self_0),
UndefinedBehaviorInfo::PointerUseAfterFree(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"PointerUseAfterFree", __self_0, &__self_1),
UndefinedBehaviorInfo::PointerOutOfBounds {
alloc_id: __self_0,
alloc_size: __self_1,
ptr_offset: __self_2,
inbounds_size: __self_3,
msg: __self_4 } =>
::core::fmt::Formatter::debug_struct_field5_finish(f,
"PointerOutOfBounds", "alloc_id", __self_0, "alloc_size",
__self_1, "ptr_offset", __self_2, "inbounds_size", __self_3,
"msg", &__self_4),
UndefinedBehaviorInfo::DanglingIntPointer {
addr: __self_0, inbounds_size: __self_1, msg: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"DanglingIntPointer", "addr", __self_0, "inbounds_size",
__self_1, "msg", &__self_2),
UndefinedBehaviorInfo::AlignmentCheckFailed(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"AlignmentCheckFailed", __self_0, &__self_1),
UndefinedBehaviorInfo::WriteToReadOnly(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WriteToReadOnly", &__self_0),
UndefinedBehaviorInfo::DerefFunctionPointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"DerefFunctionPointer", &__self_0),
UndefinedBehaviorInfo::DerefVTablePointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"DerefVTablePointer", &__self_0),
UndefinedBehaviorInfo::DerefVaListPointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"DerefVaListPointer", &__self_0),
UndefinedBehaviorInfo::DerefTypeIdPointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"DerefTypeIdPointer", &__self_0),
UndefinedBehaviorInfo::InvalidBool(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidBool", &__self_0),
UndefinedBehaviorInfo::InvalidChar(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidChar", &__self_0),
UndefinedBehaviorInfo::InvalidTag(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidTag", &__self_0),
UndefinedBehaviorInfo::InvalidFunctionPointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidFunctionPointer", &__self_0),
UndefinedBehaviorInfo::InvalidVaListPointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidVaListPointer", &__self_0),
UndefinedBehaviorInfo::InvalidVTablePointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidVTablePointer", &__self_0),
UndefinedBehaviorInfo::InvalidVTableTrait {
vtable_dyn_type: __self_0, expected_dyn_type: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"InvalidVTableTrait", "vtable_dyn_type", __self_0,
"expected_dyn_type", &__self_1),
UndefinedBehaviorInfo::InvalidStr(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidStr", &__self_0),
UndefinedBehaviorInfo::InvalidUninitBytes(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidUninitBytes", &__self_0),
UndefinedBehaviorInfo::DeadLocal =>
::core::fmt::Formatter::write_str(f, "DeadLocal"),
UndefinedBehaviorInfo::ScalarSizeMismatch(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ScalarSizeMismatch", &__self_0),
UndefinedBehaviorInfo::UninhabitedEnumVariantWritten(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UninhabitedEnumVariantWritten", &__self_0),
UndefinedBehaviorInfo::UninhabitedEnumVariantRead(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UninhabitedEnumVariantRead", &__self_0),
UndefinedBehaviorInfo::InvalidNichedEnumVariantWritten {
enum_ty: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"InvalidNichedEnumVariantWritten", "enum_ty", &__self_0),
UndefinedBehaviorInfo::AbiMismatchArgument {
arg_idx: __self_0, caller_ty: __self_1, callee_ty: __self_2 }
=>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"AbiMismatchArgument", "arg_idx", __self_0, "caller_ty",
__self_1, "callee_ty", &__self_2),
UndefinedBehaviorInfo::AbiMismatchReturn {
caller_ty: __self_0, callee_ty: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"AbiMismatchReturn", "caller_ty", __self_0, "callee_ty",
&__self_1),
UndefinedBehaviorInfo::VaArgOutOfBounds =>
::core::fmt::Formatter::write_str(f, "VaArgOutOfBounds"),
UndefinedBehaviorInfo::CVariadicMismatch {
caller_is_c_variadic: __self_0, callee_is_c_variadic: __self_1
} =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"CVariadicMismatch", "caller_is_c_variadic", __self_0,
"callee_is_c_variadic", &__self_1),
UndefinedBehaviorInfo::CVariadicFixedCountMismatch {
caller: __self_0, callee: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"CVariadicFixedCountMismatch", "caller", __self_0, "callee",
&__self_1),
}
}
}Debug)]
317pub enum UndefinedBehaviorInfo<'tcx> {
318/// Free-form case. Only for errors that are never caught! Used by miri
319Ub(String),
320/// Validation error.
321ValidationError {
322 orig_ty: Ty<'tcx>,
323 path: Option<String>,
324 msg: String,
325 ptr_bytes_warning: bool,
326 },
327328/// Unreachable code was executed.
329Unreachable,
330/// A slice/array index projection went out-of-bounds.
331BoundsCheckFailed { len: u64, index: u64 },
332/// Something was divided by 0 (x / 0).
333DivisionByZero,
334/// Something was "remainded" by 0 (x % 0).
335RemainderByZero,
336/// Signed division overflowed (INT_MIN / -1).
337DivisionOverflow,
338/// Signed remainder overflowed (INT_MIN % -1).
339RemainderOverflow,
340/// Overflowing inbounds pointer arithmetic.
341PointerArithOverflow,
342/// Overflow in arithmetic that may not overflow.
343ArithOverflow { intrinsic: Symbol },
344/// Shift by too much.
345ShiftOverflow { intrinsic: Symbol, shift_amount: Either<u128, i128> },
346/// Invalid metadata in a wide pointer
347InvalidMeta(InvalidMetaKind),
348/// Reading a C string that does not end within its allocation.
349UnterminatedCString(Pointer<AllocId>),
350/// Using a pointer after it got freed.
351PointerUseAfterFree(AllocId, CheckInAllocMsg),
352/// Used a pointer outside the bounds it is valid for.
353PointerOutOfBounds {
354 alloc_id: AllocId,
355 alloc_size: Size,
356 ptr_offset: i64,
357/// The size of the memory range that was expected to be in-bounds.
358inbounds_size: i64,
359 msg: CheckInAllocMsg,
360 },
361/// Using an integer as a pointer in the wrong way.
362DanglingIntPointer {
363 addr: u64,
364/// The size of the memory range that was expected to be in-bounds (or 0 if we need an
365 /// allocation but not any actual memory there, e.g. for function pointers).
366inbounds_size: i64,
367 msg: CheckInAllocMsg,
368 },
369/// Used a pointer with bad alignment.
370AlignmentCheckFailed(Misalignment, CheckAlignMsg),
371/// Writing to read-only memory.
372WriteToReadOnly(AllocId),
373/// Trying to access the data behind a function pointer.
374DerefFunctionPointer(AllocId),
375/// Trying to access the data behind a vtable pointer.
376DerefVTablePointer(AllocId),
377/// Trying to access the data behind a va_list pointer.
378DerefVaListPointer(AllocId),
379/// Trying to access the actual type id.
380DerefTypeIdPointer(AllocId),
381/// Using a non-boolean `u8` as bool.
382InvalidBool(u8),
383/// Using a non-character `u32` as character.
384InvalidChar(u32),
385/// The tag of an enum does not encode an actual discriminant.
386InvalidTag(Scalar<AllocId>),
387/// Using a pointer-not-to-a-function as function pointer.
388InvalidFunctionPointer(Pointer<AllocId>),
389/// Using a pointer-not-to-a-va-list as variable argument list pointer.
390InvalidVaListPointer(Pointer<AllocId>),
391/// Using a pointer-not-to-a-vtable as vtable pointer.
392InvalidVTablePointer(Pointer<AllocId>),
393/// Using a vtable for the wrong trait.
394InvalidVTableTrait {
395/// The vtable that was actually referenced by the wide pointer metadata.
396vtable_dyn_type: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
397/// The vtable that was expected at the point in MIR that it was accessed.
398expected_dyn_type: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
399 },
400/// Using a string that is not valid UTF-8,
401InvalidStr(std::str::Utf8Error),
402/// Using uninitialized data where it is not allowed.
403InvalidUninitBytes(Option<(AllocId, BadBytesAccess)>),
404/// Working with a local that is not currently live.
405DeadLocal,
406/// Data size is not equal to target size.
407ScalarSizeMismatch(ScalarSizeMismatch),
408/// A discriminant of an uninhabited enum variant is written.
409UninhabitedEnumVariantWritten(VariantIdx),
410/// An uninhabited enum variant is projected.
411UninhabitedEnumVariantRead(Option<VariantIdx>),
412/// Trying to set discriminant to the niched variant, but the value does not match.
413InvalidNichedEnumVariantWritten { enum_ty: Ty<'tcx> },
414/// ABI-incompatible argument types.
415AbiMismatchArgument {
416/// The index of the argument whose type is wrong.
417arg_idx: usize,
418 caller_ty: Ty<'tcx>,
419 callee_ty: Ty<'tcx>,
420 },
421/// ABI-incompatible return types.
422AbiMismatchReturn { caller_ty: Ty<'tcx>, callee_ty: Ty<'tcx> },
423/// `va_arg` was called on an exhausted `VaList`.
424VaArgOutOfBounds,
425/// The caller and callee disagree on whether they are c-variadic or not.
426CVariadicMismatch { caller_is_c_variadic: bool, callee_is_c_variadic: bool },
427/// The caller and callee disagree on the number of fixed (i.e. non-c-variadic) arguments.
428CVariadicFixedCountMismatch { caller: u32, callee: u32 },
429}
430431impl<'tcx> fmt::Displayfor UndefinedBehaviorInfo<'tcx> {
432fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
433use UndefinedBehaviorInfo::*;
434435fn fmt_in_alloc_attempt(
436 f: &mut fmt::Formatter<'_>,
437 msg: CheckInAllocMsg,
438 inbounds_size: i64,
439 ) -> fmt::Result {
440let inbounds_size_fmt = if inbounds_size == 1 {
441format_args!("1 byte")format_args!("1 byte")442 } else {
443format_args!("{0} bytes", inbounds_size)format_args!("{inbounds_size} bytes")444 };
445f.write_fmt(format_args!("{0}: ", msg))write!(f, "{msg}: ")?;
446match msg {
447 CheckInAllocMsg::MemoryAccess => {
448f.write_fmt(format_args!("attempting to access {0}", inbounds_size_fmt))write!(f, "attempting to access {inbounds_size_fmt}")449 }
450 CheckInAllocMsg::InboundsPointerArithmetic => {
451f.write_fmt(format_args!("attempting to offset pointer by {0}",
inbounds_size_fmt))write!(f, "attempting to offset pointer by {inbounds_size_fmt}")452 }
453 CheckInAllocMsg::Dereferenceableif inbounds_size == 0 => {
454f.write_fmt(format_args!("pointer must point to some allocation"))write!(f, "pointer must point to some allocation")455 }
456 CheckInAllocMsg::Dereferenceable => {
457f.write_fmt(format_args!("pointer must be dereferenceable for {0}",
inbounds_size_fmt))write!(f, "pointer must be dereferenceable for {inbounds_size_fmt}")458 }
459 }
460 }
461462match self {
463Ub(msg) => f.write_fmt(format_args!("{0}", msg))write!(f, "{msg}"),
464465ValidationError { orig_ty, path: None, msg, .. } => {
466f.write_fmt(format_args!("constructing invalid value of type {0}: {1}",
orig_ty, msg))write!(f, "constructing invalid value of type {orig_ty}: {msg}")467 }
468ValidationError { orig_ty, path: Some(path), msg, .. } => {
469f.write_fmt(format_args!("constructing invalid value of type {0}: at {1}, {2}",
orig_ty, path, msg))write!(f, "constructing invalid value of type {orig_ty}: at {path}, {msg}")470 }
471472Unreachable => f.write_fmt(format_args!("entering unreachable code"))write!(f, "entering unreachable code"),
473BoundsCheckFailed { len, index } => {
474f.write_fmt(format_args!("indexing out of bounds: the len is {0} but the index is {1}",
len, index))write!(f, "indexing out of bounds: the len is {len} but the index is {index}")475 }
476DivisionByZero => f.write_fmt(format_args!("dividing by zero"))write!(f, "dividing by zero"),
477RemainderByZero => f.write_fmt(format_args!("calculating the remainder with a divisor of zero"))write!(f, "calculating the remainder with a divisor of zero"),
478DivisionOverflow => f.write_fmt(format_args!("overflow in signed division (dividing MIN by -1)"))write!(f, "overflow in signed division (dividing MIN by -1)"),
479RemainderOverflow => f.write_fmt(format_args!("overflow in signed remainder (dividing MIN by -1)"))write!(f, "overflow in signed remainder (dividing MIN by -1)"),
480PointerArithOverflow => f.write_fmt(format_args!("overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`"))write!(
481f,
482"overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`"
483),
484ArithOverflow { intrinsic } => f.write_fmt(format_args!("arithmetic overflow in `{0}`", intrinsic))write!(f, "arithmetic overflow in `{intrinsic}`"),
485ShiftOverflow { shift_amount, intrinsic } => {
486f.write_fmt(format_args!("overflowing shift by {0} in `{1}`", shift_amount,
intrinsic))write!(f, "overflowing shift by {shift_amount} in `{intrinsic}`")487 }
488InvalidMeta(InvalidMetaKind::SliceTooBig) => f.write_fmt(format_args!("invalid metadata in wide pointer: slice is bigger than largest supported object"))write!(
489f,
490"invalid metadata in wide pointer: slice is bigger than largest supported object"
491),
492InvalidMeta(InvalidMetaKind::TooBig) => f.write_fmt(format_args!("invalid metadata in wide pointer: total size is bigger than largest supported object"))write!(
493f,
494"invalid metadata in wide pointer: total size is bigger than largest supported object"
495),
496UnterminatedCString(ptr) => f.write_fmt(format_args!("reading a null-terminated string starting at {0} with no null found before end of allocation",
ptr))write!(
497f,
498"reading a null-terminated string starting at {ptr} with no null found before end of allocation"
499),
500PointerUseAfterFree(alloc_id, msg) => {
501f.write_fmt(format_args!("{0}: {1} has been freed, so this pointer is dangling",
msg, alloc_id))write!(f, "{msg}: {alloc_id} has been freed, so this pointer is dangling")502 }
503&PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, inbounds_size, msg } => {
504fmt_in_alloc_attempt(f, msg, inbounds_size)?;
505f.write_fmt(format_args!(", but got "))write!(f, ", but got ")?;
506// Write pointer. Offset might be negative so we cannot use the normal `impl Display
507 // for Pointer`.
508f.write_fmt(format_args!("{0}", alloc_id))write!(f, "{}", alloc_id)?;
509if ptr_offset > 0 {
510f.write_fmt(format_args!("+{0:#x}", ptr_offset))write!(f, "+{:#x}", ptr_offset)?;
511 } else if ptr_offset < 0 {
512f.write_fmt(format_args!("-{0:#x}", ptr_offset.unsigned_abs()))write!(f, "-{:#x}", ptr_offset.unsigned_abs())?;
513 }
514// Write why it is invalid.
515f.write_fmt(format_args!(" which "))write!(f, " which ")?;
516if ptr_offset < 0 {
517f.write_fmt(format_args!("points to before the beginning of the allocation"))write!(f, "points to before the beginning of the allocation")518 } else if inbounds_size < 0 {
519// We expected the ptr to have memory to its left, but it does not.
520if ptr_offset == 0 {
521f.write_fmt(format_args!("is at the beginning of the allocation"))write!(f, "is at the beginning of the allocation")522 } else {
523f.write_fmt(format_args!("is only {0} bytes from the beginning of the allocation",
ptr_offset))write!(f, "is only {ptr_offset} bytes from the beginning of the allocation")524 }
525 } else {
526let ptr_offset = ptr_offsetas u64;
527let alloc_size = alloc_size.bytes();
528if ptr_offset >= alloc_size {
529let size = if alloc_size == 1 {
530format_args!("1 byte")format_args!("1 byte")531 } else {
532format_args!("{0} bytes", alloc_size)format_args!("{alloc_size} bytes")533 };
534f.write_fmt(format_args!("is at or beyond the end of the allocation of size {0}",
size))write!(f, "is at or beyond the end of the allocation of size {size}",)535 } else {
536let dist_to_end = alloc_size - ptr_offset;
537let dist = if dist_to_end == 1 {
538format_args!("1 byte")format_args!("1 byte")539 } else {
540format_args!("{0} bytes", dist_to_end)format_args!("{dist_to_end} bytes")541 };
542f.write_fmt(format_args!("is only {0} from the end of the allocation", dist))write!(f, "is only {dist} from the end of the allocation",)543 }
544 }
545 }
546&DanglingIntPointer { addr: 0, inbounds_size, msg } => {
547fmt_in_alloc_attempt(f, msg, inbounds_size)?;
548f.write_fmt(format_args!(", but got null pointer"))write!(f, ", but got null pointer")549 }
550&DanglingIntPointer { addr, inbounds_size, msg } => {
551fmt_in_alloc_attempt(f, msg, inbounds_size)?;
552f.write_fmt(format_args!(", but got {0} which is a dangling pointer (it has no provenance)",
Pointer::<Option<CtfeProvenance>>::without_provenance(addr)))write!(
553f,
554", but got {ptr} which is a dangling pointer (it has no provenance)",
555 ptr = Pointer::<Option<CtfeProvenance>>::without_provenance(addr),
556 )557 }
558AlignmentCheckFailed(misalign, msg) => {
559f.write_fmt(format_args!("{0} with alignment {1}, but alignment {2} is required",
match msg {
CheckAlignMsg::AccessedPtr => "accessing memory",
CheckAlignMsg::BasedOn => "accessing memory based on pointer",
}, misalign.has.bytes(), misalign.required.bytes()))write!(
560f,
561"{acc} with alignment {has}, but alignment {required} is required",
562 acc = match msg {
563 CheckAlignMsg::AccessedPtr => "accessing memory",
564 CheckAlignMsg::BasedOn => "accessing memory based on pointer",
565 },
566 has = misalign.has.bytes(),
567 required = misalign.required.bytes(),
568 )569 }
570WriteToReadOnly(alloc) => f.write_fmt(format_args!("writing to {0} which is read-only", alloc))write!(f, "writing to {alloc} which is read-only"),
571DerefFunctionPointer(alloc) => {
572f.write_fmt(format_args!("accessing {0} which contains a function", alloc))write!(f, "accessing {alloc} which contains a function")573 }
574DerefVTablePointer(alloc) => f.write_fmt(format_args!("accessing {0} which contains a vtable", alloc))write!(f, "accessing {alloc} which contains a vtable"),
575DerefVaListPointer(alloc) => {
576f.write_fmt(format_args!("accessing {0} which contains a variable argument list",
alloc))write!(f, "accessing {alloc} which contains a variable argument list")577 }
578DerefTypeIdPointer(alloc) => f.write_fmt(format_args!("accessing {0} which contains a `TypeId`", alloc))write!(f, "accessing {alloc} which contains a `TypeId`"),
579InvalidBool(value) => {
580f.write_fmt(format_args!("interpreting an invalid 8-bit value as a bool: 0x{0:02x}",
value))write!(f, "interpreting an invalid 8-bit value as a bool: 0x{value:02x}")581 }
582InvalidChar(value) => {
583f.write_fmt(format_args!("interpreting an invalid 32-bit value as a char: 0x{0:08x}",
value))write!(f, "interpreting an invalid 32-bit value as a char: 0x{value:08x}")584 }
585InvalidTag(tag) => f.write_fmt(format_args!("enum value has invalid tag: {0:x}", tag))write!(f, "enum value has invalid tag: {tag:x}"),
586InvalidFunctionPointer(ptr) => {
587f.write_fmt(format_args!("using {0} as function pointer but it does not point to a function",
ptr))write!(f, "using {ptr} as function pointer but it does not point to a function")588 }
589InvalidVaListPointer(ptr) => f.write_fmt(format_args!("using {0} as variable argument list pointer but it does not point to a variable argument list",
ptr))write!(
590f,
591"using {ptr} as variable argument list pointer but it does not point to a variable argument list"
592),
593InvalidVTablePointer(ptr) => {
594f.write_fmt(format_args!("using {0} as vtable pointer but it does not point to a vtable",
ptr))write!(f, "using {ptr} as vtable pointer but it does not point to a vtable")595 }
596InvalidVTableTrait { vtable_dyn_type, expected_dyn_type } => f.write_fmt(format_args!("using vtable for `{0}` but `{1}` was expected",
vtable_dyn_type, expected_dyn_type))write!(
597f,
598"using vtable for `{vtable_dyn_type}` but `{expected_dyn_type}` was expected"
599),
600InvalidStr(err) => f.write_fmt(format_args!("this string is not valid UTF-8: {0}", err))write!(f, "this string is not valid UTF-8: {err}"),
601InvalidUninitBytes(None) => {
602f.write_fmt(format_args!("using uninitialized data, but this operation requires initialized memory"))write!(
603f,
604"using uninitialized data, but this operation requires initialized memory"
605)606 }
607InvalidUninitBytes(Some((alloc, info))) => f.write_fmt(format_args!("reading memory at {2}{0}, but memory is uninitialized at {1}, and this operation requires initialized memory",
info.access, info.bad, alloc))write!(
608f,
609"reading memory at {alloc}{access}, but memory is uninitialized at {uninit}, and this operation requires initialized memory",
610 access = info.access,
611 uninit = info.bad,
612 ),
613DeadLocal => f.write_fmt(format_args!("accessing a dead local variable"))write!(f, "accessing a dead local variable"),
614ScalarSizeMismatch(mismatch) => f.write_fmt(format_args!("scalar size mismatch: expected {0} bytes but got {1} bytes instead",
mismatch.target_size, mismatch.data_size))write!(
615f,
616"scalar size mismatch: expected {target_size} bytes but got {data_size} bytes instead",
617 target_size = mismatch.target_size,
618 data_size = mismatch.data_size,
619 ),
620UninhabitedEnumVariantWritten(_) => {
621f.write_fmt(format_args!("writing discriminant of an uninhabited enum variant"))write!(f, "writing discriminant of an uninhabited enum variant")622 }
623UninhabitedEnumVariantRead(_) => {
624f.write_fmt(format_args!("read discriminant of an uninhabited enum variant"))write!(f, "read discriminant of an uninhabited enum variant")625 }
626InvalidNichedEnumVariantWritten { enum_ty } => {
627f.write_fmt(format_args!("trying to set discriminant of a {0} to the niched variant, but the value does not match",
enum_ty))write!(
628f,
629"trying to set discriminant of a {enum_ty} to the niched variant, but the value does not match"
630)631 }
632AbiMismatchArgument { arg_idx, caller_ty, callee_ty } => f.write_fmt(format_args!("calling a function whose parameter #{0} has type {1} passing argument of type {2}",
arg_idx + 1, callee_ty, caller_ty))write!(
633f,
634"calling a function whose parameter #{arg_idx} has type {callee_ty} passing argument of type {caller_ty}",
635 arg_idx = arg_idx + 1, // adjust for 1-indexed lists in output
636),
637AbiMismatchReturn { caller_ty, callee_ty } => f.write_fmt(format_args!("calling a function with return type {0} passing return place of type {1}",
callee_ty, caller_ty))write!(
638f,
639"calling a function with return type {callee_ty} passing return place of type {caller_ty}"
640),
641VaArgOutOfBounds => f.write_fmt(format_args!("more C-variadic arguments read than were passed"))write!(f, "more C-variadic arguments read than were passed"),
642CVariadicMismatch { .. } => f.write_fmt(format_args!("calling a function where the caller and callee disagree on whether the function is C-variadic"))write!(
643f,
644"calling a function where the caller and callee disagree on whether the function is C-variadic"
645),
646CVariadicFixedCountMismatch { caller, callee } => f.write_fmt(format_args!("calling a C-variadic function with {0} fixed arguments, but the function expects {1}",
caller, callee))write!(
647f,
648"calling a C-variadic function with {caller} fixed arguments, but the function expects {callee}"
649),
650 }
651 }
652}
653654/// Error information for when the program we executed turned out not to actually be a valid
655/// program. This cannot happen in stand-alone Miri (except for layout errors that are only detect
656/// during monomorphization), but it can happen during CTFE/ConstProp where we work on generic code
657/// or execution does not have all information available.
658#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InvalidProgramInfo<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
InvalidProgramInfo::TooGeneric =>
::core::fmt::Formatter::write_str(f, "TooGeneric"),
InvalidProgramInfo::AlreadyReported(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"AlreadyReported", &__self_0),
InvalidProgramInfo::Layout(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Layout",
&__self_0),
}
}
}Debug)]
659pub enum InvalidProgramInfo<'tcx> {
660/// Resolution can fail if we are in a too generic context.
661TooGeneric,
662/// Abort in case errors are already reported.
663AlreadyReported(ReportedErrorInfo),
664/// An error occurred during layout computation.
665Layout(layout::LayoutError<'tcx>),
666}
667668impl<'tcx> fmt::Displayfor InvalidProgramInfo<'tcx> {
669fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
670use InvalidProgramInfo::*;
671match self {
672TooGeneric => f.write_fmt(format_args!("encountered overly generic constant"))write!(f, "encountered overly generic constant"),
673AlreadyReported(_) => {
674f.write_fmt(format_args!("an error has already been reported elsewhere (this should not usually be printed)"))write!(
675f,
676"an error has already been reported elsewhere (this should not usually be printed)"
677)678 }
679Layout(e) => f.write_fmt(format_args!("{0}", e))write!(f, "{e}"),
680 }
681 }
682}
683684/// Error information for when the program did something that might (or might not) be correct
685/// to do according to the Rust spec, but due to limitations in the interpreter, the
686/// operation could not be carried out. These limitations can differ between CTFE and the
687/// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses.
688#[derive(#[automatically_derived]
impl ::core::fmt::Debug for UnsupportedOpInfo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
UnsupportedOpInfo::Unsupported(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Unsupported", &__self_0),
UnsupportedOpInfo::UnsizedLocal =>
::core::fmt::Formatter::write_str(f, "UnsizedLocal"),
UnsupportedOpInfo::ExternTypeField =>
::core::fmt::Formatter::write_str(f, "ExternTypeField"),
UnsupportedOpInfo::ReadPartialPointer(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ReadPartialPointer", &__self_0),
UnsupportedOpInfo::ReadPointerAsInt(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ReadPointerAsInt", &__self_0),
UnsupportedOpInfo::ThreadLocalStatic(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ThreadLocalStatic", &__self_0),
UnsupportedOpInfo::ExternStatic(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ExternStatic", &__self_0),
}
}
}Debug)]
689pub enum UnsupportedOpInfo {
690/// Free-form case. Only for errors that are never caught! Used by Miri.
691// FIXME still use translatable diagnostics
692Unsupported(String),
693/// Unsized local variables.
694UnsizedLocal,
695/// Extern type field with an indeterminate offset.
696ExternTypeField,
697//
698 // The variants below are only reachable from CTFE/const prop, miri will never emit them.
699 //
700/// Attempting to read or copy parts of a pointer to somewhere else; without knowing absolute
701 /// addresses, the resulting state cannot be represented by the CTFE interpreter.
702ReadPartialPointer(Pointer<AllocId>),
703/// Encountered a pointer where we needed an integer.
704ReadPointerAsInt(Option<(AllocId, BadBytesAccess)>),
705/// Accessing thread local statics
706ThreadLocalStatic(DefId),
707/// Accessing an unsupported extern static.
708ExternStatic(DefId),
709}
710711impl fmt::Displayfor UnsupportedOpInfo {
712fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
713use UnsupportedOpInfo::*;
714match self {
715Unsupported(s) => f.write_fmt(format_args!("{0}", s))write!(f, "{s}"),
716ExternTypeField => {
717f.write_fmt(format_args!("`extern type` field does not have a known offset"))write!(f, "`extern type` field does not have a known offset")718 }
719UnsizedLocal => f.write_fmt(format_args!("unsized locals are not supported"))write!(f, "unsized locals are not supported"),
720ReadPartialPointer(ptr) => {
721f.write_fmt(format_args!("unable to read parts of a pointer from memory at {0}",
ptr))write!(f, "unable to read parts of a pointer from memory at {ptr}")722 }
723ReadPointerAsInt(_) => f.write_fmt(format_args!("unable to turn pointer into integer"))write!(f, "unable to turn pointer into integer"),
724&ThreadLocalStatic(did) => {
725f.write_fmt(format_args!("cannot access thread local static `{0}`",
ty::tls::with(|tcx| tcx.def_path_str(did))))write!(
726f,
727"cannot access thread local static `{did}`",
728 did = ty::tls::with(|tcx| tcx.def_path_str(did))
729 )730 }
731&ExternStatic(did) => {
732f.write_fmt(format_args!("cannot access extern static `{0}`",
ty::tls::with(|tcx| tcx.def_path_str(did))))write!(
733f,
734"cannot access extern static `{did}`",
735 did = ty::tls::with(|tcx| tcx.def_path_str(did))
736 )737 }
738 }
739 }
740}
741742/// Error information for when the program exhausted the resources granted to it
743/// by the interpreter.
744#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ResourceExhaustionInfo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ResourceExhaustionInfo::StackFrameLimitReached =>
"StackFrameLimitReached",
ResourceExhaustionInfo::MemoryExhausted => "MemoryExhausted",
ResourceExhaustionInfo::AddressSpaceFull =>
"AddressSpaceFull",
ResourceExhaustionInfo::Interrupted => "Interrupted",
})
}
}Debug)]
745pub enum ResourceExhaustionInfo {
746/// The stack grew too big.
747StackFrameLimitReached,
748/// There is not enough memory (on the host) to perform an allocation.
749MemoryExhausted,
750/// The address space (of the target) is full.
751AddressSpaceFull,
752/// The compiler got an interrupt signal (a user ran out of patience).
753Interrupted,
754}
755756impl fmt::Displayfor ResourceExhaustionInfo {
757fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
758use ResourceExhaustionInfo::*;
759match self {
760StackFrameLimitReached => {
761f.write_fmt(format_args!("reached the configured maximum number of stack frames"))write!(f, "reached the configured maximum number of stack frames")762 }
763MemoryExhausted => {
764f.write_fmt(format_args!("tried to allocate more memory than available to compiler"))write!(f, "tried to allocate more memory than available to compiler")765 }
766AddressSpaceFull => {
767f.write_fmt(format_args!("there are no more free addresses in the address space"))write!(f, "there are no more free addresses in the address space")768 }
769Interrupted => f.write_fmt(format_args!("compilation was interrupted"))write!(f, "compilation was interrupted"),
770 }
771 }
772}
773774/// A trait for machine-specific errors (or other "machine stop" conditions).
775pub trait MachineStopType: Any + fmt::Display + fmt::Debug + Send {}
776777impl dyn MachineStopType {
778#[inline(always)]
779pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
780let x: &dyn Any = self;
781x.downcast_ref()
782 }
783}
784785#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InterpErrorKind<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
InterpErrorKind::UndefinedBehavior(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UndefinedBehavior", &__self_0),
InterpErrorKind::InvalidProgram(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidProgram", &__self_0),
InterpErrorKind::Unsupported(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Unsupported", &__self_0),
InterpErrorKind::ResourceExhaustion(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ResourceExhaustion", &__self_0),
InterpErrorKind::MachineStop(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MachineStop", &__self_0),
}
}
}Debug)]
786pub enum InterpErrorKind<'tcx> {
787/// The program caused undefined behavior.
788UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
789/// The program was invalid (ill-typed, bad MIR, not sufficiently monomorphized, ...).
790InvalidProgram(InvalidProgramInfo<'tcx>),
791/// The program did something the interpreter does not support (some of these *might* be UB
792 /// but the interpreter is not sure).
793Unsupported(UnsupportedOpInfo),
794/// The program exhausted the interpreter's resources (stack/heap too big,
795 /// execution takes too long, ...).
796ResourceExhaustion(ResourceExhaustionInfo),
797/// Stop execution for a machine-controlled reason. This is never raised by
798 /// the core engine itself.
799MachineStop(Box<dyn MachineStopType>),
800}
801802impl<'tcx> fmt::Displayfor InterpErrorKind<'tcx> {
803fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
804use InterpErrorKind::*;
805match self {
806Unsupported(msg) => f.write_fmt(format_args!("{0}", msg))write!(f, "{msg}"),
807InvalidProgram(msg) => f.write_fmt(format_args!("{0}", msg))write!(f, "{msg}"),
808UndefinedBehavior(msg) => f.write_fmt(format_args!("{0}", msg))write!(f, "{msg}"),
809ResourceExhaustion(msg) => f.write_fmt(format_args!("{0}", msg))write!(f, "{msg}"),
810MachineStop(msg) => f.write_fmt(format_args!("{0}", msg))write!(f, "{msg}"),
811 }
812 }
813}
814815impl InterpErrorKind<'_> {
816/// Some errors do string formatting even if the error is never printed.
817 /// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
818 /// so this method lets us detect them and `bug!` on unexpected errors.
819pub fn formatted_string(&self) -> bool {
820#[allow(non_exhaustive_omitted_patterns)] match self {
InterpErrorKind::Unsupported(UnsupportedOpInfo::Unsupported(_)) |
InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::ValidationError {
.. }) |
InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) =>
true,
_ => false,
}matches!(
821self,
822 InterpErrorKind::Unsupported(UnsupportedOpInfo::Unsupported(_))
823 | InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::ValidationError { .. })
824 | InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
825 )826 }
827}
828829// Macros for constructing / throwing `InterpErrorKind`
830#[macro_export]
831macro_rules!err_unsup {
832 ($($tt:tt)*) => {
833$crate::mir::interpret::InterpErrorKind::Unsupported(
834$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
835 )
836 };
837}
838839#[macro_export]
840macro_rules!err_unsup_format {
841 ($($tt:tt)*) => { $crate::err_unsup!(Unsupported(format!($($tt)*))) };
842}
843844#[macro_export]
845macro_rules!err_inval {
846 ($($tt:tt)*) => {
847$crate::mir::interpret::InterpErrorKind::InvalidProgram(
848$crate::mir::interpret::InvalidProgramInfo::$($tt)*
849 )
850 };
851}
852853#[macro_export]
854macro_rules!err_ub {
855 ($($tt:tt)*) => {
856$crate::mir::interpret::InterpErrorKind::UndefinedBehavior(
857$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
858 )
859 };
860}
861862#[macro_export]
863macro_rules!err_ub_format {
864 ($($tt:tt)*) => { $crate::err_ub!(Ub(format!($($tt)*))) };
865}
866867#[macro_export]
868macro_rules!err_exhaust {
869 ($($tt:tt)*) => {
870$crate::mir::interpret::InterpErrorKind::ResourceExhaustion(
871$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
872 )
873 };
874}
875876#[macro_export]
877macro_rules!err_machine_stop {
878 ($($tt:tt)*) => {
879$crate::mir::interpret::InterpErrorKind::MachineStop(Box::new($($tt)*))
880 };
881}
882883// In the `throw_*` macros, avoid `return` to make them work with `try {}`.
884#[macro_export]
885macro_rules!throw_unsup {
886 ($($tt:tt)*) => { do yeet $crate::err_unsup!($($tt)*) };
887}
888889#[macro_export]
890macro_rules!throw_unsup_format {
891 ($($tt:tt)*) => { do yeet $crate::err_unsup_format!($($tt)*) };
892}
893894#[macro_export]
895macro_rules!throw_inval {
896 ($($tt:tt)*) => { do yeet $crate::err_inval!($($tt)*) };
897}
898899#[macro_export]
900macro_rules!throw_ub {
901 ($($tt:tt)*) => { do yeet $crate::err_ub!($($tt)*) };
902}
903904#[macro_export]
905macro_rules!throw_ub_format {
906 ($($tt:tt)*) => { do yeet $crate::err_ub_format!($($tt)*) };
907}
908909#[macro_export]
910macro_rules!throw_exhaust {
911 ($($tt:tt)*) => { do yeet $crate::err_exhaust!($($tt)*) };
912}
913914#[macro_export]
915macro_rules!throw_machine_stop {
916 ($($tt:tt)*) => { do yeet $crate::err_machine_stop!($($tt)*) };
917}
918919/// Guard type that panics on drop.
920#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Guard {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "Guard")
}
}Debug)]
921struct Guard;
922923impl Dropfor Guard {
924fn drop(&mut self) {
925// We silence the guard if we are already panicking, to avoid double-panics.
926if !std::thread::panicking() {
927{
::core::panicking::panic_fmt(format_args!("an interpreter error got improperly discarded; use `discard_err()` if this is intentional"));
};panic!(
928"an interpreter error got improperly discarded; use `discard_err()` if this is intentional"
929);
930 }
931 }
932}
933934/// The result type used by the interpreter. This is a newtype around `Result`
935/// to block access to operations like `ok()` that discard UB errors.
936///
937/// We also make things panic if this type is ever implicitly dropped.
938#[derive(#[automatically_derived]
impl<'tcx, T: ::core::fmt::Debug> ::core::fmt::Debug for InterpResult<'tcx, T>
{
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "InterpResult",
"res", &self.res, "guard", &&self.guard)
}
}Debug)]
939#[must_use]
940pub struct InterpResult<'tcx, T = ()> {
941 res: Result<T, InterpErrorInfo<'tcx>>,
942 guard: Guard,
943}
944945impl<'tcx, T> ops::Tryfor InterpResult<'tcx, T> {
946type Output = T;
947type Residual = InterpResult<'tcx, convert::Infallible>;
948949#[inline]
950fn from_output(output: Self::Output) -> Self {
951InterpResult::new(Ok(output))
952 }
953954#[inline]
955fn branch(self) -> ops::ControlFlow<Self::Residual, Self::Output> {
956match self.disarm() {
957Ok(v) => ops::ControlFlow::Continue(v),
958Err(e) => ops::ControlFlow::Break(InterpResult::new(Err(e))),
959 }
960 }
961}
962963impl<'tcx, T> ops::Residual<T> for InterpResult<'tcx, convert::Infallible> {
964type TryType = InterpResult<'tcx, T>;
965}
966967impl<'tcx, T> ops::FromResidualfor InterpResult<'tcx, T> {
968#[inline]
969 #[track_caller]
970fn from_residual(residual: InterpResult<'tcx, convert::Infallible>) -> Self {
971match residual.disarm() {
972Err(e) => Self::new(Err(e)),
973 }
974 }
975}
976977// Allow `yeet`ing `InterpError` in functions returning `InterpResult_`.
978impl<'tcx, T> ops::FromResidual<ops::Yeet<InterpErrorKind<'tcx>>> for InterpResult<'tcx, T> {
979#[inline]
980fn from_residual(ops::Yeet(e): ops::Yeet<InterpErrorKind<'tcx>>) -> Self {
981Self::new(Err(e.into()))
982 }
983}
984985// Allow `?` on `Result<_, InterpError>` in functions returning `InterpResult_`.
986// This is useful e.g. for `option.ok_or_else(|| err_ub!(...))`.
987impl<'tcx, T, E: Into<InterpErrorInfo<'tcx>>> ops::FromResidual<Result<convert::Infallible, E>>
988for InterpResult<'tcx, T>
989{
990#[inline]
991fn from_residual(residual: Result<convert::Infallible, E>) -> Self {
992match residual {
993Err(e) => Self::new(Err(e.into())),
994 }
995 }
996}
997998impl<'tcx, T, E: Into<InterpErrorInfo<'tcx>>> From<Result<T, E>> for InterpResult<'tcx, T> {
999#[inline]
1000fn from(value: Result<T, E>) -> Self {
1001Self::new(value.map_err(|e| e.into()))
1002 }
1003}
10041005impl<'tcx, T, V: FromIterator<T>> FromIterator<InterpResult<'tcx, T>> for InterpResult<'tcx, V> {
1006fn from_iter<I: IntoIterator<Item = InterpResult<'tcx, T>>>(iter: I) -> Self {
1007Self::new(iter.into_iter().map(|x| x.disarm()).collect())
1008 }
1009}
10101011impl<'tcx, T> InterpResult<'tcx, T> {
1012#[inline(always)]
1013fn new(res: Result<T, InterpErrorInfo<'tcx>>) -> Self {
1014Self { res, guard: Guard }
1015 }
10161017#[inline(always)]
1018fn disarm(self) -> Result<T, InterpErrorInfo<'tcx>> {
1019 mem::forget(self.guard);
1020self.res
1021 }
10221023/// Discard the error information in this result. Only use this if ignoring Undefined Behavior is okay!
1024#[inline]
1025pub fn discard_err(self) -> Option<T> {
1026self.disarm().ok()
1027 }
10281029/// Look at the `Result` wrapped inside of this.
1030 /// Must only be used to report the error!
1031#[inline]
1032pub fn report_err(self) -> Result<T, InterpErrorInfo<'tcx>> {
1033self.disarm()
1034 }
10351036#[inline]
1037pub fn map<U>(self, f: impl FnOnce(T) -> U) -> InterpResult<'tcx, U> {
1038InterpResult::new(self.disarm().map(f))
1039 }
10401041#[inline]
1042pub fn map_err_info(
1043self,
1044 f: impl FnOnce(InterpErrorInfo<'tcx>) -> InterpErrorInfo<'tcx>,
1045 ) -> InterpResult<'tcx, T> {
1046InterpResult::new(self.disarm().map_err(f))
1047 }
10481049#[inline]
1050pub fn map_err_kind(
1051self,
1052 f: impl FnOnce(InterpErrorKind<'tcx>) -> InterpErrorKind<'tcx>,
1053 ) -> InterpResult<'tcx, T> {
1054InterpResult::new(self.disarm().map_err(|mut e| {
1055e.0.kind = f(e.0.kind);
1056e1057 }))
1058 }
10591060#[inline]
1061pub fn inspect_err_kind(self, f: impl FnOnce(&InterpErrorKind<'tcx>)) -> InterpResult<'tcx, T> {
1062InterpResult::new(self.disarm().inspect_err(|e| f(&e.0.kind)))
1063 }
10641065#[inline]
1066 #[track_caller]
1067pub fn unwrap(self) -> T {
1068self.disarm().unwrap()
1069 }
10701071#[inline]
1072 #[track_caller]
1073pub fn unwrap_or_else(self, f: impl FnOnce(InterpErrorInfo<'tcx>) -> T) -> T {
1074self.disarm().unwrap_or_else(f)
1075 }
10761077#[inline]
1078 #[track_caller]
1079pub fn expect(self, msg: &str) -> T {
1080self.disarm().expect(msg)
1081 }
10821083#[inline]
1084pub fn and_then<U>(self, f: impl FnOnce(T) -> InterpResult<'tcx, U>) -> InterpResult<'tcx, U> {
1085InterpResult::new(self.disarm().and_then(|t| f(t).disarm()))
1086 }
10871088/// Returns success if both `self` and `other` succeed, while ensuring we don't
1089 /// accidentally drop an error.
1090 ///
1091 /// If both are an error, `self` will be reported.
1092#[inline]
1093pub fn and<U>(self, other: InterpResult<'tcx, U>) -> InterpResult<'tcx, (T, U)> {
1094match self.disarm() {
1095Ok(t) => interp_ok((t, other?)),
1096Err(e) => {
1097// Discard the other error.
1098drop(other.disarm());
1099// Return `self`.
1100InterpResult::new(Err(e))
1101 }
1102 }
1103 }
1104}
11051106#[inline(always)]
1107pub fn interp_ok<'tcx, T>(x: T) -> InterpResult<'tcx, T> {
1108InterpResult::new(Ok(x))
1109}