Skip to content

Commit dc0131a

Browse files
committed
tests: ensure the guards cannot be accessed
The guards created in order to drop already initialized fields correctly when the initializer errors or panics must not be accessible by user controlled code. (They could for example `mem::forget` the guard and thus prevent its normal function, making the initializer macros unsound.) Thus add a test to ensure that this is true. Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent a693e45 commit dc0131a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use pin_init::*;
2+
3+
struct Foo {
4+
x: usize,
5+
}
6+
7+
fn main() {
8+
let _ = init!(Foo {
9+
x: 0,
10+
_: {
11+
let _ = __x_guard;
12+
},
13+
});
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error[E0425]: cannot find value `__x_guard` in this scope
2+
--> tests/ui/compile-fail/init/guard_access.rs:11:21
3+
|
4+
11 | let _ = __x_guard;
5+
| ^^^^^^^^^ not found in this scope

0 commit comments

Comments
 (0)