Skip to content

Commit 421f7ca

Browse files
committed
Auto merge of rust-lang#125061 - RalfJung:interpret-error, r=cjgillot
interpret: move error macros into error.rs
2 parents abb9563 + b15d09a commit 421f7ca

File tree

2 files changed

+126
-132
lines changed

2 files changed

+126
-132
lines changed

Diff for: compiler/rustc_middle/src/mir/interpret/error.rs

+118-3
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
188188
}
189189

190190
/// Error information for when the program we executed turned out not to actually be a valid
191-
/// program. This cannot happen in stand-alone Miri, but it can happen during CTFE/ConstProp
192-
/// where we work on generic code or execution does not have all information available.
191+
/// program. This cannot happen in stand-alone Miri (except for layout errors that are only detect
192+
/// during monomorphization), but it can happen during CTFE/ConstProp where we work on generic code
193+
/// or execution does not have all information available.
193194
#[derive(Debug)]
194195
pub enum InvalidProgramInfo<'tcx> {
195196
/// Resolution can fail if we are in a too generic context.
@@ -507,7 +508,7 @@ pub enum ValidationErrorKind<'tcx> {
507508
/// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses.
508509
#[derive(Debug)]
509510
pub enum UnsupportedOpInfo {
510-
/// Free-form case. Only for errors that are never caught!
511+
/// Free-form case. Only for errors that are never caught! Used by Miri.
511512
// FIXME still use translatable diagnostics
512513
Unsupported(String),
513514
/// Unsized local variables.
@@ -592,3 +593,117 @@ impl InterpError<'_> {
592593
)
593594
}
594595
}
596+
597+
// Macros for constructing / throwing `InterpError`
598+
#[macro_export]
599+
macro_rules! err_unsup {
600+
($($tt:tt)*) => {
601+
$crate::mir::interpret::InterpError::Unsupported(
602+
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
603+
)
604+
};
605+
}
606+
607+
#[macro_export]
608+
macro_rules! err_unsup_format {
609+
($($tt:tt)*) => { $crate::err_unsup!(Unsupported(format!($($tt)*))) };
610+
}
611+
612+
#[macro_export]
613+
macro_rules! err_inval {
614+
($($tt:tt)*) => {
615+
$crate::mir::interpret::InterpError::InvalidProgram(
616+
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
617+
)
618+
};
619+
}
620+
621+
#[macro_export]
622+
macro_rules! err_ub {
623+
($($tt:tt)*) => {
624+
$crate::mir::interpret::InterpError::UndefinedBehavior(
625+
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
626+
)
627+
};
628+
}
629+
630+
#[macro_export]
631+
macro_rules! err_ub_format {
632+
($($tt:tt)*) => { $crate::err_ub!(Ub(format!($($tt)*))) };
633+
}
634+
635+
#[macro_export]
636+
macro_rules! err_ub_custom {
637+
($msg:expr $(, $($name:ident = $value:expr),* $(,)?)?) => {{
638+
$(
639+
let ($($name,)*) = ($($value,)*);
640+
)?
641+
$crate::err_ub!(Custom(
642+
$crate::error::CustomSubdiagnostic {
643+
msg: || $msg,
644+
add_args: Box::new(move |mut set_arg| {
645+
$($(
646+
set_arg(stringify!($name).into(), rustc_errors::IntoDiagArg::into_diag_arg($name));
647+
)*)?
648+
})
649+
}
650+
))
651+
}};
652+
}
653+
654+
#[macro_export]
655+
macro_rules! err_exhaust {
656+
($($tt:tt)*) => {
657+
$crate::mir::interpret::InterpError::ResourceExhaustion(
658+
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
659+
)
660+
};
661+
}
662+
663+
#[macro_export]
664+
macro_rules! err_machine_stop {
665+
($($tt:tt)*) => {
666+
$crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
667+
};
668+
}
669+
670+
// In the `throw_*` macros, avoid `return` to make them work with `try {}`.
671+
#[macro_export]
672+
macro_rules! throw_unsup {
673+
($($tt:tt)*) => { do yeet $crate::err_unsup!($($tt)*) };
674+
}
675+
676+
#[macro_export]
677+
macro_rules! throw_unsup_format {
678+
($($tt:tt)*) => { do yeet $crate::err_unsup_format!($($tt)*) };
679+
}
680+
681+
#[macro_export]
682+
macro_rules! throw_inval {
683+
($($tt:tt)*) => { do yeet $crate::err_inval!($($tt)*) };
684+
}
685+
686+
#[macro_export]
687+
macro_rules! throw_ub {
688+
($($tt:tt)*) => { do yeet $crate::err_ub!($($tt)*) };
689+
}
690+
691+
#[macro_export]
692+
macro_rules! throw_ub_format {
693+
($($tt:tt)*) => { do yeet $crate::err_ub_format!($($tt)*) };
694+
}
695+
696+
#[macro_export]
697+
macro_rules! throw_ub_custom {
698+
($($tt:tt)*) => { do yeet $crate::err_ub_custom!($($tt)*) };
699+
}
700+
701+
#[macro_export]
702+
macro_rules! throw_exhaust {
703+
($($tt:tt)*) => { do yeet $crate::err_exhaust!($($tt)*) };
704+
}
705+
706+
#[macro_export]
707+
macro_rules! throw_machine_stop {
708+
($($tt:tt)*) => { do yeet $crate::err_machine_stop!($($tt)*) };
709+
}

Diff for: compiler/rustc_middle/src/mir/interpret/mod.rs

+8-129
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,9 @@
11
//! An interpreter for MIR used in CTFE and by miri.
22
3-
#[macro_export]
4-
macro_rules! err_unsup {
5-
($($tt:tt)*) => {
6-
$crate::mir::interpret::InterpError::Unsupported(
7-
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
8-
)
9-
};
10-
}
11-
pub use err_unsup;
12-
13-
#[macro_export]
14-
macro_rules! err_unsup_format {
15-
($($tt:tt)*) => { $crate::err_unsup!(Unsupported(format!($($tt)*))) };
16-
}
17-
pub use err_unsup_format;
18-
19-
#[macro_export]
20-
macro_rules! err_inval {
21-
($($tt:tt)*) => {
22-
$crate::mir::interpret::InterpError::InvalidProgram(
23-
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
24-
)
25-
};
26-
}
27-
pub use err_inval;
28-
29-
#[macro_export]
30-
macro_rules! err_ub {
31-
($($tt:tt)*) => {
32-
$crate::mir::interpret::InterpError::UndefinedBehavior(
33-
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
34-
)
35-
};
36-
}
37-
pub use err_ub;
38-
39-
#[macro_export]
40-
macro_rules! err_ub_format {
41-
($($tt:tt)*) => { err_ub!(Ub(format!($($tt)*))) };
42-
}
43-
pub use err_ub_format;
44-
45-
#[macro_export]
46-
macro_rules! err_exhaust {
47-
($($tt:tt)*) => {
48-
$crate::mir::interpret::InterpError::ResourceExhaustion(
49-
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
50-
)
51-
};
52-
}
53-
pub use err_exhaust;
54-
55-
#[macro_export]
56-
macro_rules! err_machine_stop {
57-
($($tt:tt)*) => {
58-
$crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
59-
};
60-
}
61-
pub use err_machine_stop;
62-
63-
// In the `throw_*` macros, avoid `return` to make them work with `try {}`.
64-
#[macro_export]
65-
macro_rules! throw_unsup {
66-
($($tt:tt)*) => { do yeet $crate::err_unsup!($($tt)*) };
67-
}
68-
pub use throw_unsup;
69-
70-
#[macro_export]
71-
macro_rules! throw_unsup_format {
72-
($($tt:tt)*) => { $crate::throw_unsup!(Unsupported(format!($($tt)*))) };
73-
}
74-
pub use throw_unsup_format;
75-
76-
#[macro_export]
77-
macro_rules! throw_inval {
78-
($($tt:tt)*) => { do yeet $crate::err_inval!($($tt)*) };
79-
}
80-
pub use throw_inval;
81-
82-
#[macro_export]
83-
macro_rules! throw_ub {
84-
($($tt:tt)*) => { do yeet $crate::err_ub!($($tt)*) };
85-
}
86-
pub use throw_ub;
87-
88-
#[macro_export]
89-
macro_rules! throw_ub_format {
90-
($($tt:tt)*) => { $crate::throw_ub!(Ub(format!($($tt)*))) };
91-
}
92-
pub use throw_ub_format;
93-
94-
#[macro_export]
95-
macro_rules! throw_exhaust {
96-
($($tt:tt)*) => { do yeet $crate::err_exhaust!($($tt)*) };
97-
}
98-
pub use throw_exhaust;
99-
100-
#[macro_export]
101-
macro_rules! throw_machine_stop {
102-
($($tt:tt)*) => { do yeet $crate::err_machine_stop!($($tt)*) };
103-
}
104-
pub use throw_machine_stop;
105-
106-
#[macro_export]
107-
macro_rules! err_ub_custom {
108-
($msg:expr $(, $($name:ident = $value:expr),* $(,)?)?) => {{
109-
$(
110-
let ($($name,)*) = ($($value,)*);
111-
)?
112-
$crate::err_ub!(Custom(
113-
$crate::error::CustomSubdiagnostic {
114-
msg: || $msg,
115-
add_args: Box::new(move |mut set_arg| {
116-
$($(
117-
set_arg(stringify!($name).into(), rustc_errors::IntoDiagArg::into_diag_arg($name));
118-
)*)?
119-
})
120-
}
121-
))
122-
}};
123-
}
124-
pub use err_ub_custom;
125-
126-
#[macro_export]
127-
macro_rules! throw_ub_custom {
128-
($($tt:tt)*) => { do yeet $crate::err_ub_custom!($($tt)*) };
129-
}
130-
pub use throw_ub_custom;
3+
#[macro_use]
4+
mod error;
1315

1326
mod allocation;
133-
mod error;
1347
mod pointer;
1358
mod queries;
1369
mod value;
@@ -166,6 +39,12 @@ pub use self::error::{
16639
ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
16740
ValidationErrorKind,
16841
};
42+
// Also make the error macros available from this module.
43+
pub use {
44+
err_exhaust, err_inval, err_machine_stop, err_ub, err_ub_custom, err_ub_format, err_unsup,
45+
err_unsup_format, throw_exhaust, throw_inval, throw_machine_stop, throw_ub, throw_ub_custom,
46+
throw_ub_format, throw_unsup, throw_unsup_format,
47+
};
16948

17049
pub use self::value::Scalar;
17150

0 commit comments

Comments
 (0)