Skip to content

Commit 94e6982

Browse files
adpaco-awstedinski
authored andcommitted
Add tests for constant-evaluated intrinsics (rust-lang#1042)
* Move test for `needs_drop` * Minor change to description in `needs_drop` test * Add test for `type_name` * Add test for `type_id` * Add test for `size_of` * Add test for `pref_align_of` * Add test for `min_align_of` * Include a comment for `codegen_intrinsic_const` * In `type_id` test, assert no duplicates
1 parent b628a10 commit 94e6982

File tree

7 files changed

+203
-1
lines changed

7 files changed

+203
-1
lines changed

src/kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ impl<'tcx> GotocCtx<'tcx> {
244244
}};
245245
}
246246

247+
// Intrinsics which encode a value known during compilation (e.g., `size_of`)
247248
macro_rules! codegen_intrinsic_const {
248249
() => {{
249250
let value = self
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that we get the expected results for the `min_align_of` intrinsic
5+
// with common data types
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::min_align_of;
8+
9+
struct MyStruct {}
10+
11+
enum MyEnum {}
12+
13+
#[kani::proof]
14+
fn main() {
15+
// Scalar types
16+
assert!(min_align_of::<i8>() == 1);
17+
assert!(min_align_of::<i16>() == 2);
18+
assert!(min_align_of::<i32>() == 4);
19+
assert!(min_align_of::<i64>() == 8);
20+
assert!(min_align_of::<i128>() == 8);
21+
assert!(min_align_of::<isize>() == 8);
22+
assert!(min_align_of::<u8>() == 1);
23+
assert!(min_align_of::<u16>() == 2);
24+
assert!(min_align_of::<u32>() == 4);
25+
assert!(min_align_of::<u64>() == 8);
26+
assert!(min_align_of::<u128>() == 8);
27+
assert!(min_align_of::<usize>() == 8);
28+
assert!(min_align_of::<f32>() == 4);
29+
assert!(min_align_of::<f64>() == 8);
30+
assert!(min_align_of::<bool>() == 1);
31+
assert!(min_align_of::<char>() == 4);
32+
// Compound types (tuple and array)
33+
assert!(min_align_of::<(i32, i32)>() == 4);
34+
assert!(min_align_of::<[i32; 5]>() == 4);
35+
// Custom data types (struct and enum)
36+
assert!(min_align_of::<MyStruct>() == 1);
37+
assert!(min_align_of::<MyEnum>() == 1);
38+
}

tests/kani/Intrinsics/needs_drop.rs renamed to tests/kani/Intrinsics/ConstEval/needs_drop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
// Check that we get the expected results for needs_drop intrinsic
4+
// Check that we get the expected results for the `needs_drop` intrinsic
55

66
use std::mem;
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that we get the expected results for the `pref_align_of` intrinsic
5+
// with common data types
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::pref_align_of;
8+
9+
struct MyStruct {}
10+
11+
enum MyEnum {}
12+
13+
#[kani::proof]
14+
fn main() {
15+
// Scalar types
16+
assert!(unsafe { pref_align_of::<i8>() } == 1);
17+
assert!(unsafe { pref_align_of::<i16>() } == 2);
18+
assert!(unsafe { pref_align_of::<i32>() } == 4);
19+
assert!(unsafe { pref_align_of::<i64>() } == 8);
20+
assert!(unsafe { pref_align_of::<i128>() } == 8);
21+
assert!(unsafe { pref_align_of::<isize>() } == 8);
22+
assert!(unsafe { pref_align_of::<u8>() } == 1);
23+
assert!(unsafe { pref_align_of::<u16>() } == 2);
24+
assert!(unsafe { pref_align_of::<u32>() } == 4);
25+
assert!(unsafe { pref_align_of::<u64>() } == 8);
26+
assert!(unsafe { pref_align_of::<u128>() } == 8);
27+
assert!(unsafe { pref_align_of::<usize>() } == 8);
28+
assert!(unsafe { pref_align_of::<f32>() } == 4);
29+
assert!(unsafe { pref_align_of::<f64>() } == 8);
30+
assert!(unsafe { pref_align_of::<bool>() } == 1);
31+
assert!(unsafe { pref_align_of::<char>() } == 4);
32+
// Compound types (tuple and array)
33+
assert!(unsafe { pref_align_of::<(i32, i32)>() } == 8);
34+
assert!(unsafe { pref_align_of::<[i32; 5]>() } == 4);
35+
// Custom data types (struct and enum)
36+
assert!(unsafe { pref_align_of::<MyStruct>() } == 8);
37+
assert!(unsafe { pref_align_of::<MyEnum>() } == 1);
38+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that we get the expected results for the `size_of` intrinsic
5+
// with common data types
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::size_of;
8+
9+
struct MyStruct {}
10+
11+
enum MyEnum {}
12+
13+
#[kani::proof]
14+
fn main() {
15+
// Scalar types
16+
assert!(size_of::<i8>() == 1);
17+
assert!(size_of::<i16>() == 2);
18+
assert!(size_of::<i32>() == 4);
19+
assert!(size_of::<i64>() == 8);
20+
assert!(size_of::<i128>() == 16);
21+
assert!(size_of::<isize>() == 8);
22+
assert!(size_of::<u8>() == 1);
23+
assert!(size_of::<u16>() == 2);
24+
assert!(size_of::<u32>() == 4);
25+
assert!(size_of::<u64>() == 8);
26+
assert!(size_of::<u128>() == 16);
27+
assert!(size_of::<usize>() == 8);
28+
assert!(size_of::<f32>() == 4);
29+
assert!(size_of::<f64>() == 8);
30+
assert!(size_of::<bool>() == 1);
31+
assert!(size_of::<char>() == 4);
32+
// Compound types (tuple and array)
33+
assert!(size_of::<(i32, i32)>() == 8);
34+
assert!(size_of::<[i32; 5]>() == 20);
35+
// Custom data types (struct and enum)
36+
assert!(size_of::<MyStruct>() == 0);
37+
assert!(size_of::<MyEnum>() == 0);
38+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that the `type_id` intrinsic is supported with common data types
5+
// and that there are no duplicate type IDs
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::type_id;
8+
9+
struct MyStruct {}
10+
11+
enum MyEnum {}
12+
13+
#[kani::proof]
14+
fn main() {
15+
let type_ids = [
16+
// Scalar types
17+
type_id::<i8>(),
18+
type_id::<i16>(),
19+
type_id::<i32>(),
20+
type_id::<i64>(),
21+
type_id::<i128>(),
22+
type_id::<isize>(),
23+
type_id::<u8>(),
24+
type_id::<u16>(),
25+
type_id::<u32>(),
26+
type_id::<u64>(),
27+
type_id::<u128>(),
28+
type_id::<usize>(),
29+
type_id::<f32>(),
30+
type_id::<f64>(),
31+
type_id::<bool>(),
32+
type_id::<char>(),
33+
// Compound types (tuple and array)
34+
type_id::<(i32, i32)>(),
35+
type_id::<[i32; 5]>(),
36+
// Custom data types (struct and enum)
37+
type_id::<MyStruct>(),
38+
type_id::<MyEnum>(),
39+
];
40+
41+
// Check that there are no duplicate type IDs
42+
let i: usize = kani::any();
43+
let j: usize = kani::any();
44+
kani::assume(i < type_ids.len());
45+
kani::assume(j < type_ids.len());
46+
if i != j {
47+
assert_ne!(type_ids[i], type_ids[j]);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that we get the expected results for the `type_name` intrinsic
5+
// with common data types
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::type_name;
8+
9+
struct MyStruct {}
10+
11+
enum MyEnum {}
12+
13+
#[kani::proof]
14+
fn main() {
15+
// Scalar types
16+
assert!(type_name::<i8>() == "i8");
17+
assert!(type_name::<i16>() == "i16");
18+
assert!(type_name::<i32>() == "i32");
19+
assert!(type_name::<i64>() == "i64");
20+
assert!(type_name::<i128>() == "i128");
21+
assert!(type_name::<isize>() == "isize");
22+
assert!(type_name::<u8>() == "u8");
23+
assert!(type_name::<u16>() == "u16");
24+
assert!(type_name::<u32>() == "u32");
25+
assert!(type_name::<u64>() == "u64");
26+
assert!(type_name::<u128>() == "u128");
27+
assert!(type_name::<usize>() == "usize");
28+
assert!(type_name::<f32>() == "f32");
29+
assert!(type_name::<f64>() == "f64");
30+
assert!(type_name::<bool>() == "bool");
31+
assert!(type_name::<char>() == "char");
32+
// Compound types (tuple and array)
33+
assert!(type_name::<(i32, i32)>() == "(i32, i32)");
34+
assert!(type_name::<[i32; 5]>() == "[i32; 5]");
35+
// Custom data types (struct and enum)
36+
assert!(type_name::<MyStruct>() == "type_name::MyStruct");
37+
assert!(type_name::<MyEnum>() == "type_name::MyEnum");
38+
}

0 commit comments

Comments
 (0)