Skip to content

Commit b45f72e

Browse files
adpaco-awstedinski
authored andcommitted
Add tests for forget (rust-lang#1041)
* Disable `forget` intrinsic * Restore `forget` * Add two tests for `forget` * Update `forget` status in support table * Use `check-fail` instead of `codegen-fail`
1 parent 1d9485e commit b45f72e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

docs/src/rust-feature-support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ floorf64 | No | |
338338
fmaf32 | Yes | |
339339
fmaf64 | Yes | |
340340
fmul_fast | Partial | [#809](https://github.com/model-checking/kani/issues/809) |
341-
forget | Partial | Generates `SKIP` statement |
341+
forget | Yes | |
342342
frem_fast | No | |
343343
fsub_fast | Yes | |
344344
likely | Yes | |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// kani-check-fail
4+
5+
// Checks that `forget` produces a compilation error if the value is referenced
6+
// after "forgetting" it
7+
8+
// This test is a modified version of the code found in
9+
// https://doc.rust-lang.org/std/mem/fn.forget.html#relationship-with-manuallydrop
10+
#![feature(core_intrinsics)]
11+
12+
#[kani::proof]
13+
fn main() {
14+
let mut v = vec![65, 122];
15+
// Build a `String` using the contents of `v`
16+
let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()) };
17+
// leak `v` because its memory is now managed by `s`
18+
std::intrinsics::forget(v); // v is now invalid and must not be passed to a function
19+
assert!(v[0] == 65); // Error: v is referenced after `forget`
20+
assert_eq!(s, "Az");
21+
// `s` is implicitly dropped and its memory deallocated.
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Checks that `forget` does not cause a compilation error if the value is not
5+
// referenced after "forgetting" it
6+
7+
// This test is a modified version of the code found in
8+
// https://doc.rust-lang.org/std/mem/fn.forget.html#relationship-with-manuallydrop
9+
#![feature(core_intrinsics)]
10+
11+
#[kani::proof]
12+
fn main() {
13+
let mut v = vec![65, 122];
14+
// Build a `String` using the contents of `v`
15+
let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()) };
16+
// leak `v` because its memory is now managed by `s`
17+
std::intrinsics::forget(v); // v is now invalid and must not be passed to a function
18+
assert_eq!(s, "Az");
19+
// `s` is implicitly dropped and its memory deallocated.
20+
}

0 commit comments

Comments
 (0)