forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`
- Loading branch information
1 parent
1d9485e
commit b45f72e
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-check-fail | ||
|
||
// Checks that `forget` produces a compilation error if the value is referenced | ||
// after "forgetting" it | ||
|
||
// This test is a modified version of the code found in | ||
// https://doc.rust-lang.org/std/mem/fn.forget.html#relationship-with-manuallydrop | ||
#![feature(core_intrinsics)] | ||
|
||
#[kani::proof] | ||
fn main() { | ||
let mut v = vec![65, 122]; | ||
// Build a `String` using the contents of `v` | ||
let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()) }; | ||
// leak `v` because its memory is now managed by `s` | ||
std::intrinsics::forget(v); // v is now invalid and must not be passed to a function | ||
assert!(v[0] == 65); // Error: v is referenced after `forget` | ||
assert_eq!(s, "Az"); | ||
// `s` is implicitly dropped and its memory deallocated. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
// Checks that `forget` does not cause a compilation error if the value is not | ||
// referenced after "forgetting" it | ||
|
||
// This test is a modified version of the code found in | ||
// https://doc.rust-lang.org/std/mem/fn.forget.html#relationship-with-manuallydrop | ||
#![feature(core_intrinsics)] | ||
|
||
#[kani::proof] | ||
fn main() { | ||
let mut v = vec![65, 122]; | ||
// Build a `String` using the contents of `v` | ||
let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()) }; | ||
// leak `v` because its memory is now managed by `s` | ||
std::intrinsics::forget(v); // v is now invalid and must not be passed to a function | ||
assert_eq!(s, "Az"); | ||
// `s` is implicitly dropped and its memory deallocated. | ||
} |