Skip to content

Extend execution-error assertion macros with patter and any arms#2897

Open
marijamijailovic wants to merge 2 commits into
0xMiden:nextfrom
walnuthq:pr/execution-error-assert-macros
Open

Extend execution-error assertion macros with patter and any arms#2897
marijamijailovic wants to merge 2 commits into
0xMiden:nextfrom
walnuthq:pr/execution-error-assert-macros

Conversation

@marijamijailovic
Copy link
Copy Markdown
Contributor

Closes #2807

Adds two new arms to assert_execution_error! and assert_transaction_executor_error!:

  • matches <pat> [if <guard>] - pattern-match the inner ExecutionError directly, with an optional guard. Useful for variants other than FailedAssertion, or to assert on a specific err_code.
  • any - accept any Err.

The existing $expected arm still works as before; tests passing MasmError constants don't change.

New tests/assertion_macros.rs:

  • Macro grammar - each arm exercised against hand-built errors, plus #[should_panic] checks for the failure paths.
  • VM-driven - small MASM programs run on FastProcessor to trigger and assert real ExecutionError variants end-to-end (DivideByZero, CycleLimitExceeded, MemoryError::UnalignedWordAccess, AdviceError::StackReadFailed, EventError, etc).

@Keinberger Keinberger requested a review from mmagician May 12, 2026 09:32
@Keinberger
Copy link
Copy Markdown
Collaborator

@marijamijailovic Would you mind fixing the broken lint workflow? Seems like something is ill-formatted.

@marijamijailovic marijamijailovic force-pushed the pr/execution-error-assert-macros branch from 969c4de to 0702a68 Compare May 12, 2026 09:58
@marijamijailovic
Copy link
Copy Markdown
Contributor Author

@marijamijailovic Would you mind fixing the broken lint workflow? Seems like something is ill-formatted.
Thanks @Keinberger, formatting should be fixed

@marijamijailovic
Copy link
Copy Markdown
Contributor Author

Changelog check is failing now – should we add the ‎no-changelog flag to this PR, or include an entry in the changelog?

@mmagician mmagician added the no changelog This PR does not require an entry in the `CHANGELOG.md` file label May 15, 2026
Copy link
Copy Markdown
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I left a few small comments.

Err($crate::ExecError(actual)) => {
if !$crate::expected_error::ExpectedExecutionError::matches(&$expected, &actual) {
::core::panic!(
"Execution error did not match expected `{}`: {}",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Execution error did not match expected `{}`: {}",
"Execution error did not match:\nexpected: {}\nactual: {}",

Nit: Newlines for better readability. Would be nice to update this across all panic messages in the macros.

assert_eq!(msg.as_ref(), $expected_err.message(), "error messages did not match");
}
Err($crate::ExecError($pat)) $(if $guard)? => {},
Ok(_) => ::core::panic!("Execution was unexpectedly successful"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(_) => ::core::panic!("Execution was unexpectedly successful"),
Ok(_) => ::core::panic!("Execution was unexpectedly successful\nexpected error: {}", err),

I'd include the error in the unexpectedly successful case, which helps find the issue. Applies to other unexpectedly successful output as well.

Comment on lines +85 to +90
($execution_result:expr, any $(,)?) => {
match $execution_result {
Err(_) => {},
Ok(_) => ::core::panic!("Execution was unexpectedly successful"),
}
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asserting that some error occurred is basically always too coarse in my opinion. The error can silently change from A to B and the test would happily pass. A good test, imo, is precise about its assertions. So anything that nudges users to write precise assertions is great, and so I would suggest removing this functionality to match any error.

If users really, really want to do this, it is very simple with assert!(res.is_err()).

Comment on lines +67 to +82
let core_lib = CoreLibrary::default();
let assembler = Assembler::new(Arc::new(DefaultSourceManager::default()))
.with_dynamic_library(&core_lib)
.expect("CoreLibrary should load");
let program = assembler.assemble_program(src).expect("MASM should assemble");

let mut host = DefaultHost::default();
host.load_library(core_lib.mast_forest())
.expect("CoreLibrary mast forest should load");

let stack = StackInputs::new(&[]).expect("empty stack inputs are valid");
FastProcessor::new(stack)
.with_options(options)
.execute(&program, &mut host)
.await
.map_err(ExecError::new)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace this with CodeExecutor, if possible. It would be one less place to maintain a FastProcessor invocation.

Comment on lines +21 to +22
impl ExpectedExecutionError for MasmError {
fn matches(&self, actual: &ExecutionError) -> bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could move this to MasmError itself in miden-protocol, so we don't need to add the public trait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no changelog This PR does not require an entry in the `CHANGELOG.md` file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend execution-error assertion macros

4 participants