aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/tests
diff options
context:
space:
mode:
authorbenjaminschlegel87 <[email protected]>2025-07-25 20:39:40 +0200
committerGitHub <[email protected]>2025-07-25 20:39:40 +0200
commitdbc1818acd69e2e15ac574356c9b07cb717df441 (patch)
tree05e6360c1946183b524a1ce82268547fe4bbcfd0 /embassy-sync/tests
parentadb728009ceba095d2190038ff698aaee08907a9 (diff)
parent996974e313fa5ec2c7c2d9dd0998fab244c0a180 (diff)
Merge branch 'embassy-rs:main' into stm32_adc_v3_hw_oversampling_support
Diffstat (limited to 'embassy-sync/tests')
-rw-r--r--embassy-sync/tests/ui.rs13
-rw-r--r--embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs11
-rw-r--r--embassy-sync/tests/ui/sync_impl/lazy_lock_function.stderr24
-rw-r--r--embassy-sync/tests/ui/sync_impl/lazy_lock_type.rs6
-rw-r--r--embassy-sync/tests/ui/sync_impl/lazy_lock_type.stderr9
-rw-r--r--embassy-sync/tests/ui/sync_impl/once_lock.rs6
-rw-r--r--embassy-sync/tests/ui/sync_impl/once_lock.stderr9
7 files changed, 78 insertions, 0 deletions
diff --git a/embassy-sync/tests/ui.rs b/embassy-sync/tests/ui.rs
new file mode 100644
index 000000000..e8b1080d8
--- /dev/null
+++ b/embassy-sync/tests/ui.rs
@@ -0,0 +1,13 @@
1#[cfg(not(miri))]
2#[test]
3fn ui() {
4 let t = trybuild::TestCases::new();
5
6 // These test cases should fail to compile since OnceLock and LazyLock should not unconditionally implement sync
7 // for all types. These tests are regression tests against the following issues:
8 // * https://github.com/embassy-rs/embassy/issues/4307
9 // * https://github.com/embassy-rs/embassy/issues/3904
10 t.compile_fail("tests/ui/sync_impl/lazy_lock_function.rs");
11 t.compile_fail("tests/ui/sync_impl/lazy_lock_type.rs");
12 t.compile_fail("tests/ui/sync_impl/once_lock.rs");
13}
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..35f5587c0
--- /dev/null
+++ b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.rs
@@ -0,0 +1,11 @@
1use embassy_sync::lazy_lock::LazyLock;
2
3fn main() {
4 let x = 128u8;
5 let x_ptr: *const u8 = core::ptr::addr_of!(x);
6 let closure_capturing_non_sync_variable = || unsafe { core::ptr::read(x_ptr) };
7
8 check_sync(LazyLock::new(closure_capturing_non_sync_variable));
9}
10
11fn check_sync<T: Sync>(_lazy_lock: T) {}
diff --git a/embassy-sync/tests/ui/sync_impl/lazy_lock_function.stderr b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.stderr
new file mode 100644
index 000000000..daf79ad28
--- /dev/null
+++ b/embassy-sync/tests/ui/sync_impl/lazy_lock_function.stderr
@@ -0,0 +1,24 @@
1error[E0277]: `*const u8` cannot be shared between threads safely
2 --> tests/ui/sync_impl/lazy_lock_function.rs:8:16
3 |
46 | let closure_capturing_non_sync_variable = || unsafe { core::ptr::read(x_ptr) };
5 | -- within this `{closure@$DIR/tests/ui/sync_impl/lazy_lock_function.rs:6:47: 6:49}`
67 |
78 | check_sync(LazyLock::new(closure_capturing_non_sync_variable));
8 | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const u8` cannot be shared between threads safely
9 | |
10 | required by a bound introduced by this call
11 |
12 = help: within `{closure@$DIR/tests/ui/sync_impl/lazy_lock_function.rs:6:47: 6:49}`, the trait `Sync` is not implemented for `*const u8`
13 = note: required because it appears within the type `&*const u8`
14note: required because it's used within this closure
15 --> tests/ui/sync_impl/lazy_lock_function.rs:6:47
16 |
176 | let closure_capturing_non_sync_variable = || unsafe { core::ptr::read(x_ptr) };
18 | ^^
19 = note: required for `embassy_sync::lazy_lock::LazyLock<u8, {closure@$DIR/tests/ui/sync_impl/lazy_lock_function.rs:6:47: 6:49}>` to implement `Sync`
20note: required by a bound in `check_sync`
21 --> tests/ui/sync_impl/lazy_lock_function.rs:11:18
22 |
2311 | fn check_sync<T: Sync>(_lazy_lock: T) {}
24 | ^^^^ required by this bound in `check_sync`
diff --git a/embassy-sync/tests/ui/sync_impl/lazy_lock_type.rs b/embassy-sync/tests/ui/sync_impl/lazy_lock_type.rs
new file mode 100644
index 000000000..4e1383143
--- /dev/null
+++ b/embassy-sync/tests/ui/sync_impl/lazy_lock_type.rs
@@ -0,0 +1,6 @@
1use embassy_sync::lazy_lock::LazyLock;
2
3// *mut u8 is not Sync, so LazyLock should not implement Sync for this type. This should fail to compile.
4static GLOBAL: LazyLock<*mut u8> = LazyLock::new(|| core::ptr::null_mut());
5
6fn main() {}
diff --git a/embassy-sync/tests/ui/sync_impl/lazy_lock_type.stderr b/embassy-sync/tests/ui/sync_impl/lazy_lock_type.stderr
new file mode 100644
index 000000000..1ccc54c7a
--- /dev/null
+++ b/embassy-sync/tests/ui/sync_impl/lazy_lock_type.stderr
@@ -0,0 +1,9 @@
1error[E0277]: `*mut u8` cannot be shared between threads safely
2 --> tests/ui/sync_impl/lazy_lock_type.rs:4:16
3 |
44 | static GLOBAL: LazyLock<*mut u8> = LazyLock::new(|| core::ptr::null_mut());
5 | ^^^^^^^^^^^^^^^^^ `*mut u8` cannot be shared between threads safely
6 |
7 = help: the trait `Sync` is not implemented for `*mut u8`
8 = note: required for `embassy_sync::lazy_lock::LazyLock<*mut u8>` to implement `Sync`
9 = note: shared static variables must have a type that implements `Sync`
diff --git a/embassy-sync/tests/ui/sync_impl/once_lock.rs b/embassy-sync/tests/ui/sync_impl/once_lock.rs
new file mode 100644
index 000000000..8f50d583b
--- /dev/null
+++ b/embassy-sync/tests/ui/sync_impl/once_lock.rs
@@ -0,0 +1,6 @@
1use embassy_sync::once_lock::OnceLock;
2
3// *mut u8 is not Sync, so OnceLock should not implement Sync for this type. This should fail to compile.
4static GLOBAL: OnceLock<*mut u8> = OnceLock::new();
5
6fn main() {}
diff --git a/embassy-sync/tests/ui/sync_impl/once_lock.stderr b/embassy-sync/tests/ui/sync_impl/once_lock.stderr
new file mode 100644
index 000000000..e2419f844
--- /dev/null
+++ b/embassy-sync/tests/ui/sync_impl/once_lock.stderr
@@ -0,0 +1,9 @@
1error[E0277]: `*mut u8` cannot be shared between threads safely
2 --> tests/ui/sync_impl/once_lock.rs:4:16
3 |
44 | static GLOBAL: OnceLock<*mut u8> = OnceLock::new();
5 | ^^^^^^^^^^^^^^^^^ `*mut u8` cannot be shared between threads safely
6 |
7 = help: the trait `Sync` is not implemented for `*mut u8`
8 = note: required for `embassy_sync::once_lock::OnceLock<*mut u8>` to implement `Sync`
9 = note: shared static variables must have a type that implements `Sync`