-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compilation failure tests for Handle (#220)
The FFI `Handle` class relies on several invariants that should cause compilation failures when somebody attempts to misuse a handle. This PR adds such negative test coverage with help from the `trybuild` crate. To run those tests, do: ``` $ cargo test --features developer-visibility --package delta_kernel_ffi -- invalid_handle_code ``` The newly added `developer-visibility` feature is similar to the one already present in the kernel crate, and makes additional classes and modules public. This is important for testing, because doc tests and compilation failure tests are not "inside" the crate and thus cannot otherwise access `pub(crate) mod handle`. The same mechanism also allows generating internal docs that include classes relevant to those building or extending the kernel FFI.
- Loading branch information
Showing
22 changed files
with
427 additions
and
46 deletions.
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
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
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,16 @@ | ||
use delta_kernel_ffi_macros::handle_descriptor; | ||
use delta_kernel_ffi::handle::Handle; | ||
|
||
pub struct Foo(u32); | ||
|
||
#[handle_descriptor(target=Foo, mutable=true, sized=true)] | ||
pub struct MutFoo; | ||
|
||
fn main() { | ||
let s = Foo(0); | ||
let mut h: Handle<MutFoo> = Box::new(s).into(); | ||
let r = unsafe { h.as_mut() }; | ||
let _ = unsafe { h.as_mut() }; | ||
let _ = unsafe { h.as_ref() }; | ||
r.0 = 1; | ||
} |
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,21 @@ | ||
error[E0499]: cannot borrow `h` as mutable more than once at a time | ||
--> tests/invalid-handle-code/double-mut-reference.rs:13:22 | ||
| | ||
12 | let r = unsafe { h.as_mut() }; | ||
| - first mutable borrow occurs here | ||
13 | let _ = unsafe { h.as_mut() }; | ||
| ^ second mutable borrow occurs here | ||
14 | let _ = unsafe { h.as_ref() }; | ||
15 | r.0 = 1; | ||
| ------- first borrow later used here | ||
|
||
error[E0502]: cannot borrow `h` as immutable because it is also borrowed as mutable | ||
--> tests/invalid-handle-code/double-mut-reference.rs:14:22 | ||
| | ||
12 | let r = unsafe { h.as_mut() }; | ||
| - mutable borrow occurs here | ||
13 | let _ = unsafe { h.as_mut() }; | ||
14 | let _ = unsafe { h.as_ref() }; | ||
| ^ immutable borrow occurs here | ||
15 | r.0 = 1; | ||
| ------- mutable borrow later used here |
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,15 @@ | ||
use delta_kernel_ffi_macros::handle_descriptor; | ||
use delta_kernel_ffi::handle::Handle; | ||
|
||
pub struct Foo(u32); | ||
|
||
#[handle_descriptor(target=Foo, mutable=true, sized=true)] | ||
pub struct MutFoo; | ||
|
||
fn main() { | ||
let s = Foo(0); | ||
let mut h: Handle<MutFoo> = Box::new(s).into(); | ||
unsafe { h.drop_handle() }; | ||
let _ = unsafe { h.into_inner() }; | ||
let _ = unsafe { h.as_mut() }; | ||
} |
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,32 @@ | ||
error[E0382]: use of moved value: `h` | ||
--> tests/invalid-handle-code/moved-from-handle.rs:13:22 | ||
| | ||
11 | let mut h: Handle<MutFoo> = Box::new(s).into(); | ||
| ----- move occurs because `h` has type `Handle<MutFoo>`, which does not implement the `Copy` trait | ||
12 | unsafe { h.drop_handle() }; | ||
| ------------- `h` moved due to this method call | ||
13 | let _ = unsafe { h.into_inner() }; | ||
| ^ value used here after move | ||
| | ||
note: `Handle::<H>::drop_handle` takes ownership of the receiver `self`, which moves `h` | ||
--> src/handle.rs | ||
| | ||
| pub unsafe fn drop_handle(self) { | ||
| ^^^^ | ||
|
||
error[E0382]: borrow of moved value: `h` | ||
--> tests/invalid-handle-code/moved-from-handle.rs:14:22 | ||
| | ||
11 | let mut h: Handle<MutFoo> = Box::new(s).into(); | ||
| ----- move occurs because `h` has type `Handle<MutFoo>`, which does not implement the `Copy` trait | ||
12 | unsafe { h.drop_handle() }; | ||
13 | let _ = unsafe { h.into_inner() }; | ||
| ------------ `h` moved due to this method call | ||
14 | let _ = unsafe { h.as_mut() }; | ||
| ^ value borrowed here after move | ||
| | ||
note: `Handle::<H>::into_inner` takes ownership of the receiver `self`, which moves `h` | ||
--> src/handle.rs | ||
| | ||
| pub unsafe fn into_inner(self) -> H::From { | ||
| ^^^^ |
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,15 @@ | ||
use delta_kernel_ffi_macros::handle_descriptor; | ||
use delta_kernel_ffi::handle::Handle; | ||
use std::sync::Arc; | ||
|
||
pub struct Foo(u32); | ||
|
||
#[handle_descriptor(target=Foo, mutable=true, sized=true)] | ||
pub struct MutFoo; | ||
|
||
fn main() { | ||
let s = Foo(0); | ||
let h: Handle<MutFoo> = Arc::new(s).into(); | ||
let r = h.clone_as_arc(); | ||
let h = h.clone_handle(); | ||
} |
Oops, something went wrong.