Skip to content

Commit b4b6736

Browse files
zhassan-awstedinski
authored andcommitted
Override assert_eq and assert_ne macros (rust-lang#821)
1 parent 3e7867f commit b4b6736

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

library/std/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,34 @@ macro_rules! assert {
4545
kani::assert($cond, concat!(stringify!($($arg)+)));
4646
};
4747
}
48+
49+
// Override the assert_eq and assert_ne macros to
50+
// 1. Bypass the formatting-related code in the standard library implementation,
51+
// which is not relevant for verification (see
52+
// https://github.com/model-checking/kani/issues/14)
53+
// 2. Generate a suitable message for the assert of the form:
54+
// assertion failed: $left == $right
55+
// instead of the uninformative:
56+
// a panicking function core::panicking::assert_failed is invoked
57+
// (see https://github.com/model-checking/kani/issues/13)
58+
// 3. Call kani::assert so that any instrumentation that it does (e.g. injecting
59+
// reachability checks) is done for assert_eq and assert_ne
60+
#[macro_export]
61+
macro_rules! assert_eq {
62+
($left:expr, $right:expr $(,)?) => ({
63+
assert!($left == $right);
64+
});
65+
($left:expr, $right:expr, $($arg:tt)+) => ({
66+
assert!($left == $right, $($arg)+);
67+
});
68+
}
69+
70+
#[macro_export]
71+
macro_rules! assert_ne {
72+
($left:expr, $right:expr $(,)?) => ({
73+
assert!($left != $right);
74+
});
75+
($left:expr, $right:expr, $($arg:tt)+) => ({
76+
assert!($left != $right, $($arg)+);
77+
});
78+
}

tests/expected/assert-eq/expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
a panicking function core::panicking::assert_failed is invoked: SUCCESS
2-
a panicking function core::panicking::assert_failed is invoked: FAILURE
3-
a panicking function core::panicking::assert_failed is invoked: SUCCESS
1+
line 15 assertion failed: x + 1 == y: SUCCESS
2+
line 16 assertion failed: x == y: FAILURE
3+
line 17 assertion failed: x != y: SUCCESS

0 commit comments

Comments
 (0)