Skip to content

Commit

Permalink
Auto merge of rust-lang#125061 - RalfJung:interpret-error, r=cjgillot
Browse files Browse the repository at this point in the history
interpret: move error macros into error.rs
  • Loading branch information
bors committed May 13, 2024
2 parents abb9563 + b15d09a commit 421f7ca
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 132 deletions.
121 changes: 118 additions & 3 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
}

/// Error information for when the program we executed turned out not to actually be a valid
/// program. This cannot happen in stand-alone Miri, but it can happen during CTFE/ConstProp
/// where we work on generic code or execution does not have all information available.
/// program. This cannot happen in stand-alone Miri (except for layout errors that are only detect
/// during monomorphization), but it can happen during CTFE/ConstProp where we work on generic code
/// or execution does not have all information available.
#[derive(Debug)]
pub enum InvalidProgramInfo<'tcx> {
/// Resolution can fail if we are in a too generic context.
Expand Down Expand Up @@ -507,7 +508,7 @@ pub enum ValidationErrorKind<'tcx> {
/// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses.
#[derive(Debug)]
pub enum UnsupportedOpInfo {
/// Free-form case. Only for errors that are never caught!
/// Free-form case. Only for errors that are never caught! Used by Miri.
// FIXME still use translatable diagnostics
Unsupported(String),
/// Unsized local variables.
Expand Down Expand Up @@ -592,3 +593,117 @@ impl InterpError<'_> {
)
}
}

// Macros for constructing / throwing `InterpError`
#[macro_export]
macro_rules! err_unsup {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::Unsupported(
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
)
};
}

#[macro_export]
macro_rules! err_unsup_format {
($($tt:tt)*) => { $crate::err_unsup!(Unsupported(format!($($tt)*))) };
}

#[macro_export]
macro_rules! err_inval {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::InvalidProgram(
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
)
};
}

#[macro_export]
macro_rules! err_ub {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::UndefinedBehavior(
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
)
};
}

#[macro_export]
macro_rules! err_ub_format {
($($tt:tt)*) => { $crate::err_ub!(Ub(format!($($tt)*))) };
}

#[macro_export]
macro_rules! err_ub_custom {
($msg:expr $(, $($name:ident = $value:expr),* $(,)?)?) => {{
$(
let ($($name,)*) = ($($value,)*);
)?
$crate::err_ub!(Custom(
$crate::error::CustomSubdiagnostic {
msg: || $msg,
add_args: Box::new(move |mut set_arg| {
$($(
set_arg(stringify!($name).into(), rustc_errors::IntoDiagArg::into_diag_arg($name));
)*)?
})
}
))
}};
}

#[macro_export]
macro_rules! err_exhaust {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::ResourceExhaustion(
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
)
};
}

#[macro_export]
macro_rules! err_machine_stop {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
};
}

// In the `throw_*` macros, avoid `return` to make them work with `try {}`.
#[macro_export]
macro_rules! throw_unsup {
($($tt:tt)*) => { do yeet $crate::err_unsup!($($tt)*) };
}

#[macro_export]
macro_rules! throw_unsup_format {
($($tt:tt)*) => { do yeet $crate::err_unsup_format!($($tt)*) };
}

#[macro_export]
macro_rules! throw_inval {
($($tt:tt)*) => { do yeet $crate::err_inval!($($tt)*) };
}

#[macro_export]
macro_rules! throw_ub {
($($tt:tt)*) => { do yeet $crate::err_ub!($($tt)*) };
}

#[macro_export]
macro_rules! throw_ub_format {
($($tt:tt)*) => { do yeet $crate::err_ub_format!($($tt)*) };
}

#[macro_export]
macro_rules! throw_ub_custom {
($($tt:tt)*) => { do yeet $crate::err_ub_custom!($($tt)*) };
}

#[macro_export]
macro_rules! throw_exhaust {
($($tt:tt)*) => { do yeet $crate::err_exhaust!($($tt)*) };
}

#[macro_export]
macro_rules! throw_machine_stop {
($($tt:tt)*) => { do yeet $crate::err_machine_stop!($($tt)*) };
}
137 changes: 8 additions & 129 deletions compiler/rustc_middle/src/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
@@ -1,136 +1,9 @@
//! An interpreter for MIR used in CTFE and by miri.
#[macro_export]
macro_rules! err_unsup {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::Unsupported(
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
)
};
}
pub use err_unsup;

#[macro_export]
macro_rules! err_unsup_format {
($($tt:tt)*) => { $crate::err_unsup!(Unsupported(format!($($tt)*))) };
}
pub use err_unsup_format;

#[macro_export]
macro_rules! err_inval {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::InvalidProgram(
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
)
};
}
pub use err_inval;

#[macro_export]
macro_rules! err_ub {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::UndefinedBehavior(
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
)
};
}
pub use err_ub;

#[macro_export]
macro_rules! err_ub_format {
($($tt:tt)*) => { err_ub!(Ub(format!($($tt)*))) };
}
pub use err_ub_format;

#[macro_export]
macro_rules! err_exhaust {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::ResourceExhaustion(
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
)
};
}
pub use err_exhaust;

#[macro_export]
macro_rules! err_machine_stop {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
};
}
pub use err_machine_stop;

// In the `throw_*` macros, avoid `return` to make them work with `try {}`.
#[macro_export]
macro_rules! throw_unsup {
($($tt:tt)*) => { do yeet $crate::err_unsup!($($tt)*) };
}
pub use throw_unsup;

#[macro_export]
macro_rules! throw_unsup_format {
($($tt:tt)*) => { $crate::throw_unsup!(Unsupported(format!($($tt)*))) };
}
pub use throw_unsup_format;

#[macro_export]
macro_rules! throw_inval {
($($tt:tt)*) => { do yeet $crate::err_inval!($($tt)*) };
}
pub use throw_inval;

#[macro_export]
macro_rules! throw_ub {
($($tt:tt)*) => { do yeet $crate::err_ub!($($tt)*) };
}
pub use throw_ub;

#[macro_export]
macro_rules! throw_ub_format {
($($tt:tt)*) => { $crate::throw_ub!(Ub(format!($($tt)*))) };
}
pub use throw_ub_format;

#[macro_export]
macro_rules! throw_exhaust {
($($tt:tt)*) => { do yeet $crate::err_exhaust!($($tt)*) };
}
pub use throw_exhaust;

#[macro_export]
macro_rules! throw_machine_stop {
($($tt:tt)*) => { do yeet $crate::err_machine_stop!($($tt)*) };
}
pub use throw_machine_stop;

#[macro_export]
macro_rules! err_ub_custom {
($msg:expr $(, $($name:ident = $value:expr),* $(,)?)?) => {{
$(
let ($($name,)*) = ($($value,)*);
)?
$crate::err_ub!(Custom(
$crate::error::CustomSubdiagnostic {
msg: || $msg,
add_args: Box::new(move |mut set_arg| {
$($(
set_arg(stringify!($name).into(), rustc_errors::IntoDiagArg::into_diag_arg($name));
)*)?
})
}
))
}};
}
pub use err_ub_custom;

#[macro_export]
macro_rules! throw_ub_custom {
($($tt:tt)*) => { do yeet $crate::err_ub_custom!($($tt)*) };
}
pub use throw_ub_custom;
#[macro_use]
mod error;

mod allocation;
mod error;
mod pointer;
mod queries;
mod value;
Expand Down Expand Up @@ -166,6 +39,12 @@ pub use self::error::{
ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
ValidationErrorKind,
};
// Also make the error macros available from this module.
pub use {
err_exhaust, err_inval, err_machine_stop, err_ub, err_ub_custom, err_ub_format, err_unsup,
err_unsup_format, throw_exhaust, throw_inval, throw_machine_stop, throw_ub, throw_ub_custom,
throw_ub_format, throw_unsup, throw_unsup_format,
};

pub use self::value::Scalar;

Expand Down

0 comments on commit 421f7ca

Please sign in to comment.