Skip to content

Commit d3940ec

Browse files
committed
singleton!: check that calls to unsafe functions require an unsafe block
1 parent d30209f commit d3940ec

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/macros.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ macro_rules! iprintln {
4343
/// let y = alias();
4444
/// // BAD this second call to `alias` will definitively `panic!`
4545
/// let y_alias = alias();
46+
///
47+
/// # // check that the call to `uninitialized` requires unsafe
48+
/// # singleton!(: u8 = unsafe { std::mem::uninitialized() });
4649
/// }
4750
///
4851
/// fn alias() -> &'static mut bool {
@@ -52,16 +55,17 @@ macro_rules! iprintln {
5255
#[macro_export]
5356
macro_rules! singleton {
5457
(: $ty:ty = $expr:expr) => {
55-
$crate::interrupt::free(|_| unsafe {
58+
$crate::interrupt::free(|_| {
5659
static mut USED: bool = false;
5760
static mut VAR: $crate::UntaggedOption<$ty> = $crate::UntaggedOption { none: () };
5861

59-
if USED {
62+
if unsafe { USED } {
6063
None
6164
} else {
62-
USED = true;
63-
VAR.some = $expr;
64-
let var: &'static mut _ = &mut VAR.some;
65+
unsafe { USED = true }
66+
let expr = $expr;
67+
unsafe { VAR.some = expr }
68+
let var: &'static mut _ = unsafe { &mut VAR.some };
6569
Some(var)
6670
}
6771
})

0 commit comments

Comments
 (0)