Skip to content

Commit 55a7f3e

Browse files
authored
Fix std overrides when crate has extern std (rust-lang#2989)
I tried applying model-checking/kani#2983 fix, however, this would require user to import `__kani_workaround_core_assert`. To fix that, I moved the definition to be under `kani` crate. I replaced the existing fixme test. Initially I didn't check we had one, and I created a second one which is simpler (no cargo needed) but that also includes other cases. Resolves rust-lang#2187
1 parent 8d667a1 commit 55a7f3e

File tree

7 files changed

+48
-48
lines changed

7 files changed

+48
-48
lines changed

library/kani/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@ macro_rules! cover {
288288
};
289289
}
290290

291+
// Used to bind `core::assert` to a different name to avoid possible name conflicts if a
292+
// crate uses `extern crate std as core`. See
293+
// https://github.com/model-checking/kani/issues/1949 and https://github.com/model-checking/kani/issues/2187
294+
#[doc(hidden)]
295+
#[cfg(not(feature = "concrete_playback"))]
296+
pub use core::assert as __kani__workaround_core_assert;
297+
291298
// Kani proc macros must be in a separate crate
292299
pub use kani_macros::*;
293300

library/std/src/lib.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
// re-export all std symbols
1313
pub use std::*;
1414

15-
// Bind `core::assert` to a different name to avoid possible name conflicts if a
16-
// crate uses `extern crate std as core`. See
17-
// https://github.com/model-checking/kani/issues/1949
18-
#[cfg(not(feature = "concrete_playback"))]
19-
#[allow(unused_imports)]
20-
use core::assert as __kani__workaround_core_assert;
21-
2215
#[cfg(not(feature = "concrete_playback"))]
2316
// Override process calls with stubs.
2417
pub mod process;
@@ -64,7 +57,7 @@ macro_rules! assert {
6457
// strategy, which is tracked in
6558
// https://github.com/model-checking/kani/issues/692
6659
if false {
67-
__kani__workaround_core_assert!(true, $($arg)+);
60+
kani::__kani__workaround_core_assert!(true, $($arg)+);
6861
}
6962
}};
7063
}
@@ -178,7 +171,7 @@ macro_rules! unreachable {
178171
// handle.
179172
($fmt:expr, $($arg:tt)*) => {{
180173
if false {
181-
__kani__workaround_core_assert!(true, $fmt, $($arg)+);
174+
kani::__kani__workaround_core_assert!(true, $fmt, $($arg)+);
182175
}
183176
kani::panic(concat!("internal error: entered unreachable code: ",
184177
stringify!($fmt, $($arg)*)))}};
@@ -195,7 +188,7 @@ macro_rules! panic {
195188
// `panic!("Error message")`
196189
($msg:literal $(,)?) => ({
197190
if false {
198-
__kani__workaround_core_assert!(true, $msg);
191+
kani::__kani__workaround_core_assert!(true, $msg);
199192
}
200193
kani::panic(concat!($msg))
201194
});
@@ -214,7 +207,7 @@ macro_rules! panic {
214207
// `panic!("Error: {}", code);`
215208
($($arg:tt)+) => {{
216209
if false {
217-
__kani__workaround_core_assert!(true, $($arg)+);
210+
kani::__kani__workaround_core_assert!(true, $($arg)+);
218211
}
219212
kani::panic(stringify!($($arg)+));
220213
}};

tests/cargo-kani/no_std/Cargo.toml

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/cargo-kani/no_std/foo.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/cargo-kani/no_std/src/main.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Checking harness foo...
2+
3+
Status: SUCCESS\
4+
Description: ""debug_assert""\
5+
macro_override.rs:
6+
7+
Status: SUCCESS\
8+
Description: "panic"\
9+
macro_override.rs:
10+
11+
Status: SUCCESS\
12+
Description: "internal error: entered unreachable code: unreachable"\
13+
macro_override.rs:
14+
15+
Complete - 1 successfully verified harnesses, 0 failures, 1 total.

tests/ui/extern_std/macro_override.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! Test if Kani can correctly identify assertions in a no_std crate that re-exports `std` library.
5+
//! Issue previously reported here: <https://github.com/model-checking/kani/issues/2187>
6+
//
7+
// compile-flags: --cfg=feature="std"
8+
#![no_std]
9+
10+
#[cfg(feature = "std")]
11+
extern crate std;
12+
13+
#[kani::proof]
14+
fn foo() {
15+
std::debug_assert!(true, "debug_assert");
16+
if kani::any_where(|b| !b) {
17+
std::unreachable!("unreachable")
18+
}
19+
if kani::any_where(|val: &u8| *val > 10) < 10 {
20+
std::panic!("panic")
21+
}
22+
}

0 commit comments

Comments
 (0)