rustc_const_eval::interpret::validity

Macro try_validation

source
macro_rules! try_validation {
    ($e:expr, $where:expr,
    $( $( $p:pat_param )|+ => $kind: expr ),+ $(,)?
    ) => { ... };
}
Expand description

If $e throws an error matching the pattern, throw a validation failure. Other errors are passed back to the caller, unchanged – and if they reach the root of the visitor, we make sure only validation errors and InvalidProgram errors are left. This lets you use the patterns as a kind of validation list, asserting which errors can possibly happen:

let v = try_validation!(some_fn(), some_path, {
    Foo | Bar | Baz => { "some failure" },
});

The patterns must be of type UndefinedBehaviorInfo. An additional expected parameter can also be added to the failure message:

let v = try_validation!(some_fn(), some_path, {
    Foo | Bar | Baz => { "some failure" } expected { "something that wasn't a failure" },
});

An additional nicety is that both parameters actually take format args, so you can just write the format string in directly:

let v = try_validation!(some_fn(), some_path, {
    Foo | Bar | Baz => { "{:?}", some_failure } expected { "{}", expected_value },
});