From 54d9a7fed3ab211b1049aae0af0bc49f912c9df4 Mon Sep 17 00:00:00 2001 From: Brezak Date: Wed, 23 Jul 2025 21:17:12 +0200 Subject: embassy-executor: add macro ui test for unsafe ops in unsafe tasks Check if the #[task] macro properly handles unsafe functions so the `unsafe_op_in_unsafe_fn` lint still works --- embassy-executor/tests/ui.rs | 2 ++ embassy-executor/tests/ui/unsafe_op_in_unsafe_task.rs | 10 ++++++++++ .../tests/ui/unsafe_op_in_unsafe_task.stderr | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 embassy-executor/tests/ui/unsafe_op_in_unsafe_task.rs create mode 100644 embassy-executor/tests/ui/unsafe_op_in_unsafe_task.stderr diff --git a/embassy-executor/tests/ui.rs b/embassy-executor/tests/ui.rs index 8b83cd368..5486a0624 100644 --- a/embassy-executor/tests/ui.rs +++ b/embassy-executor/tests/ui.rs @@ -32,5 +32,7 @@ fn ui() { t.compile_fail("tests/ui/self.rs"); t.compile_fail("tests/ui/type_error.rs"); t.compile_fail("tests/ui/where_clause.rs"); + t.compile_fail("tests/ui/unsafe_op_in_unsafe_task.rs"); + t.pass("tests/ui/task_safety_attribute.rs"); } diff --git a/embassy-executor/tests/ui/unsafe_op_in_unsafe_task.rs b/embassy-executor/tests/ui/unsafe_op_in_unsafe_task.rs new file mode 100644 index 000000000..ee7924838 --- /dev/null +++ b/embassy-executor/tests/ui/unsafe_op_in_unsafe_task.rs @@ -0,0 +1,10 @@ +#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))] +#![deny(unsafe_op_in_unsafe_fn)] + +#[embassy_executor::task] +async unsafe fn task() { + let x = 5; + (&x as *const i32).read(); +} + +fn main() {} diff --git a/embassy-executor/tests/ui/unsafe_op_in_unsafe_task.stderr b/embassy-executor/tests/ui/unsafe_op_in_unsafe_task.stderr new file mode 100644 index 000000000..d987a4b95 --- /dev/null +++ b/embassy-executor/tests/ui/unsafe_op_in_unsafe_task.stderr @@ -0,0 +1,18 @@ +error[E0133]: call to unsafe function `std::ptr::const_ptr::::read` is unsafe and requires unsafe block + --> tests/ui/unsafe_op_in_unsafe_task.rs:7:5 + | +7 | (&x as *const i32).read(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function + | + = note: for more information, see + = note: consult the function's documentation for information on how to avoid undefined behavior +note: an unsafe function restricts its caller, but its body is safe by default + --> tests/ui/unsafe_op_in_unsafe_task.rs:5:1 + | +5 | async unsafe fn task() { + | ^^^^^^^^^^^^^^^^^^^^^^ +note: the lint level is defined here + --> tests/ui/unsafe_op_in_unsafe_task.rs:2:9 + | +2 | #![deny(unsafe_op_in_unsafe_fn)] + | ^^^^^^^^^^^^^^^^^^^^^^ -- cgit