From 905aed45f9c15eef9bf9afa5a6b81ae345694581 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Wed, 18 Jun 2025 15:37:05 -0700 Subject: add tests illustrating the problem --- embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs (limited to 'embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs') diff --git a/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs new file mode 100644 index 000000000..c6e6f7e64 --- /dev/null +++ b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs @@ -0,0 +1,15 @@ +use embassy_sync::lazy_lock::LazyLock; + +fn main() { + let x = 128u8; + let x_ptr: *const u8 = core::ptr::addr_of!(x); + + let closure_capturing_non_sync_variable = || { + unsafe { + core::ptr::read(x_ptr) + } + }; + + // This should fail to compile because the closure captures a non-Sync variable: x_ptr. + let _lazy_u8: LazyLock = LazyLock::new(closure_capturing_non_sync_variable); +} -- cgit From 3a432920978dc48f0b4c0a8da7c118415a0708fd Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Wed, 18 Jun 2025 16:16:06 -0700 Subject: commit expected errors --- embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs') diff --git a/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs index c6e6f7e64..35f5587c0 100644 --- a/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs +++ b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs @@ -3,13 +3,9 @@ use embassy_sync::lazy_lock::LazyLock; fn main() { let x = 128u8; let x_ptr: *const u8 = core::ptr::addr_of!(x); + let closure_capturing_non_sync_variable = || unsafe { core::ptr::read(x_ptr) }; - let closure_capturing_non_sync_variable = || { - unsafe { - core::ptr::read(x_ptr) - } - }; - - // This should fail to compile because the closure captures a non-Sync variable: x_ptr. - let _lazy_u8: LazyLock = LazyLock::new(closure_capturing_non_sync_variable); + check_sync(LazyLock::new(closure_capturing_non_sync_variable)); } + +fn check_sync(_lazy_lock: T) {} -- cgit