Skip to content

Commit 6f70c7d

Browse files
authored
Use fully-qualified name for size_of (model-checking#3838)
Add `core::mem::` prefix to all uses of `size_of`. Background: in some cases, I get errors of the form: ``` Compiling kani_core v0.58.0 (https://github.com/model-checking/kani#35015dce) error[E0425]: cannot find function `size_of` in this scope --> /home/ubuntu/.cargo/git/checkouts/kani-0ce0dacf5e98886d/35015dc/library/kani/src/lib.rs:54:1 | 54 | kani_core::kani_lib!(kani); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope | = help: consider importing one of these items: core::mem::size_of crate::core_path::intrinsics::size_of crate::core_path::mem::size_of std::mem::size_of = note: this error originates in the macro `kani_core::ptr_generator` which comes from the expansion of the macro `kani_core::kani_lib` (in Nightly builds, run with -Z macro-backtrace for more info) ``` when adding the Kani library as a dependency. Adding `core::mem::` fixes those issues. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 35015dc commit 6f70c7d

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ exclude = [
7272
"tests/script-based-pre/build-cache-bin/target/new_dep",
7373
"tests/script-based-pre/build-cache-dirty/target/new_dep",
7474
"tests/script-based-pre/verify_std_cmd/tmp_dir/target/kani_verify_std",
75+
"tests/script-based-pre/kani_lib_dep",
7576
]
7677

7778
[workspace.lints.clippy]

library/kani_core/src/arbitrary/pointer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ macro_rules! ptr_generator {
265265
let ptr = match status {
266266
AllocationStatus::Dangling => {
267267
// Generate potentially unaligned pointer.
268-
let offset = kani::any_where(|b: &usize| *b < size_of::<T>());
268+
let offset = kani::any_where(|b: &usize| *b < core::mem::size_of::<T>());
269269
crate::ptr::NonNull::<T>::dangling().as_ptr().wrapping_add(offset)
270270
}
271271
AllocationStatus::DeadObject => {
@@ -279,7 +279,7 @@ macro_rules! ptr_generator {
279279
AllocationStatus::OutOfBounds => {
280280
// Generate potentially unaligned pointer.
281281
let buf_ptr = addr_of_mut!(self.buf) as *mut u8;
282-
let offset = kani::any_where(|b: &usize| *b < size_of::<T>());
282+
let offset = kani::any_where(|b: &usize| *b < core::mem::size_of::<T>());
283283
unsafe { buf_ptr.add(Self::BUF_LEN - offset) as *mut T }
284284
}
285285
};
@@ -331,7 +331,7 @@ macro_rules! ptr_generator {
331331
"Cannot generate in-bounds object of the requested type. Buffer is not big enough."
332332
);
333333
let buf_ptr = addr_of_mut!(self.buf) as *mut u8;
334-
let offset = kani::any_where(|b: &usize| *b <= Self::BUF_LEN - size_of::<T>());
334+
let offset = kani::any_where(|b: &usize| *b <= Self::BUF_LEN - core::mem::size_of::<T>());
335335
let ptr = unsafe { buf_ptr.add(offset) as *mut T };
336336
let is_initialized = kani::any();
337337
if is_initialized {
@@ -356,8 +356,8 @@ macro_rules! ptr_generator_fn {
356356
() => {
357357
/// Create a pointer generator that fits at least `N` elements of type `T`.
358358
pub fn pointer_generator<T, const NUM_ELTS: usize>()
359-
-> PointerGenerator<{ size_of::<T>() * NUM_ELTS }> {
360-
PointerGenerator::<{ size_of::<T>() * NUM_ELTS }>::new()
359+
-> PointerGenerator<{ core::mem::size_of::<T>() * NUM_ELTS }> {
360+
PointerGenerator::<{ core::mem::size_of::<T>() * NUM_ELTS }>::new()
361361
}
362362
};
363363
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
[package]
4+
name = "kani_lib_dep"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
[dependencies]
9+
kani_core = { path = "../../../library/kani_core" }
10+
kani = { path = "../../../library/kani" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# Copyright Kani Contributors
3+
# SPDX-License-Identifier: Apache-2.0 OR MIT
4+
# Test building a crate that has the Kani library as a dependency
5+
6+
set -e
7+
8+
rm -rf target
9+
10+
set -e
11+
cargo build
12+
13+
rm -rf target
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
script: build.sh
4+
expected: expected
5+
exit_code: 0

tests/script-based-pre/kani_lib_dep/expected

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
use kani::Arbitrary;
5+
6+
struct Foo {
7+
x: i32,
8+
y: i32,
9+
z: i32,
10+
}
11+
12+
impl Arbitrary for Foo {
13+
fn any() -> Self {
14+
Foo { x: 3, y: 4, z: 5 }
15+
}
16+
}
17+
18+
fn main() {
19+
let f: Foo = kani::any();
20+
assert_eq!(f.x, 3);
21+
assert_eq!(f.y, 4);
22+
assert_eq!(f.z, 5);
23+
}

0 commit comments

Comments
 (0)