Skip to content

Commit

Permalink
Merge pull request #3434 from embassy-rs/macro-fixes
Browse files Browse the repository at this point in the history
executor: improve macro error messages, add compile-fail / ui tests.
  • Loading branch information
Dirbaio authored Oct 20, 2024
2 parents c9358e1 + 8f98268 commit f92b27d
Show file tree
Hide file tree
Showing 39 changed files with 302 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/ci/test-nightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export CARGO_HOME=/ci/cache/cargo
export CARGO_TARGET_DIR=/ci/cache/target
mv rust-toolchain-nightly.toml rust-toolchain.toml

cargo test --manifest-path ./embassy-executor/Cargo.toml
cargo test --manifest-path ./embassy-executor/Cargo.toml --features nightly

MIRIFLAGS=-Zmiri-ignore-leaks cargo miri test --manifest-path ./embassy-executor/Cargo.toml
MIRIFLAGS=-Zmiri-ignore-leaks cargo miri test --manifest-path ./embassy-executor/Cargo.toml --features nightly
MIRIFLAGS=-Zmiri-ignore-leaks cargo miri test --manifest-path ./embassy-sync/Cargo.toml
1 change: 1 addition & 0 deletions .github/ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export CARGO_TARGET_DIR=/ci/cache/target
# used when pointing stm32-metapac to a CI-built one.
export CARGO_NET_GIT_FETCH_WITH_CLI=true

cargo test --manifest-path ./embassy-executor/Cargo.toml
cargo test --manifest-path ./embassy-futures/Cargo.toml
cargo test --manifest-path ./embassy-sync/Cargo.toml
cargo test --manifest-path ./embassy-embedded-hal/Cargo.toml
Expand Down
26 changes: 24 additions & 2 deletions embassy-executor-macros/src/macros/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use darling::export::NestedMeta;
use darling::FromMeta;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::visit::Visit;
use syn::visit::{self, Visit};
use syn::{Expr, ExprLit, Lit, LitInt, ReturnType, Type};

use crate::util::*;
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
for arg in fargs.iter_mut() {
match arg {
syn::FnArg::Receiver(_) => {
error(&mut errors, arg, "task functions must not have receiver arguments");
error(&mut errors, arg, "task functions must not have `self` arguments");
}
syn::FnArg::Typed(t) => {
check_arg_ty(&mut errors, &t.ty);
Expand Down Expand Up @@ -180,6 +180,28 @@ fn check_arg_ty(errors: &mut TokenStream, ty: &Type) {
}

impl<'a, 'ast> Visit<'ast> for Visitor<'a> {
fn visit_type_reference(&mut self, i: &'ast syn::TypeReference) {
// only check for elided lifetime here. If not elided, it's checked by `visit_lifetime`.
if i.lifetime.is_none() {
error(
self.errors,
i.and_token,
"Arguments for tasks must live forever. Try using the `'static` lifetime.",
)
}
visit::visit_type_reference(self, i);
}

fn visit_lifetime(&mut self, i: &'ast syn::Lifetime) {
if i.ident.to_string() != "static" {
error(
self.errors,
i,
"Arguments for tasks must live forever. Try using the `'static` lifetime.",
)
}
}

fn visit_type_impl_trait(&mut self, i: &'ast syn::TypeImplTrait) {
error(self.errors, i, "`impl Trait` is not allowed in task arguments. It is syntax sugar for generics, and tasks can't be generic.");
}
Expand Down
2 changes: 1 addition & 1 deletion embassy-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ avr-device = { version = "0.5.3", optional = true }

[dev-dependencies]
critical-section = { version = "1.1", features = ["std"] }

trybuild = "1.0"

[features]

Expand Down
23 changes: 23 additions & 0 deletions embassy-executor/tests/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[cfg(not(miri))]
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/abi.rs");
t.compile_fail("tests/ui/bad_return.rs");
t.compile_fail("tests/ui/generics.rs");
t.compile_fail("tests/ui/impl_trait_nested.rs");
t.compile_fail("tests/ui/impl_trait.rs");
t.compile_fail("tests/ui/impl_trait_static.rs");
t.compile_fail("tests/ui/nonstatic_ref_anon_nested.rs");
t.compile_fail("tests/ui/nonstatic_ref_anon.rs");
t.compile_fail("tests/ui/nonstatic_ref_elided.rs");
t.compile_fail("tests/ui/nonstatic_ref_generic.rs");
t.compile_fail("tests/ui/nonstatic_struct_anon.rs");
#[cfg(not(feature = "nightly"))] // we can't catch this case with the macro, so the output changes on nightly.
t.compile_fail("tests/ui/nonstatic_struct_elided.rs");
t.compile_fail("tests/ui/nonstatic_struct_generic.rs");
t.compile_fail("tests/ui/not_async.rs");
t.compile_fail("tests/ui/self_ref.rs");
t.compile_fail("tests/ui/self.rs");
t.compile_fail("tests/ui/where_clause.rs");
}
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async extern "C" fn task() {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/abi.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: task functions must not have an ABI qualifier
--> tests/ui/abi.rs:6:1
|
6 | async extern "C" fn task() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
10 changes: 10 additions & 0 deletions embassy-executor/tests/ui/bad_return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task() -> u32 {
5
}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/bad_return.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: task functions must either not return a value, return `()` or return `!`
--> tests/ui/bad_return.rs:6:1
|
6 | async fn task() -> u32 {
| ^^^^^^^^^^^^^^^^^^^^^^
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task<T: Sized>(_t: T) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/generics.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: task functions must not be generic
--> tests/ui/generics.rs:6:1
|
6 | async fn task<T: Sized>(_t: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 changes: 6 additions & 0 deletions embassy-executor/tests/ui/impl_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

#[embassy_executor::task]
async fn foo(_x: impl Sized) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/impl_trait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `impl Trait` is not allowed in task arguments. It is syntax sugar for generics, and tasks can't be generic.
--> tests/ui/impl_trait.rs:4:18
|
4 | async fn foo(_x: impl Sized) {}
| ^^^^^^^^^^
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/impl_trait_nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<T>(T);

#[embassy_executor::task]
async fn foo(_x: Foo<impl Sized + 'static>) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/impl_trait_nested.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `impl Trait` is not allowed in task arguments. It is syntax sugar for generics, and tasks can't be generic.
--> tests/ui/impl_trait_nested.rs:6:22
|
6 | async fn foo(_x: Foo<impl Sized + 'static>) {}
| ^^^^^^^^^^^^^^^^^^^^
6 changes: 6 additions & 0 deletions embassy-executor/tests/ui/impl_trait_static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

#[embassy_executor::task]
async fn foo(_x: impl Sized + 'static) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/impl_trait_static.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `impl Trait` is not allowed in task arguments. It is syntax sugar for generics, and tasks can't be generic.
--> tests/ui/impl_trait_static.rs:4:18
|
4 | async fn foo(_x: impl Sized + 'static) {}
| ^^^^^^^^^^^^^^^^^^^^
6 changes: 6 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_anon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

#[embassy_executor::task]
async fn foo(_x: &'_ u32) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_anon.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Arguments for tasks must live forever. Try using the `'static` lifetime.
--> tests/ui/nonstatic_ref_anon.rs:4:19
|
4 | async fn foo(_x: &'_ u32) {}
| ^^
6 changes: 6 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_anon_nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

#[embassy_executor::task]
async fn foo(_x: &'static &'_ u32) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_anon_nested.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Arguments for tasks must live forever. Try using the `'static` lifetime.
--> tests/ui/nonstatic_ref_anon_nested.rs:4:28
|
4 | async fn foo(_x: &'static &'_ u32) {}
| ^^
6 changes: 6 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_elided.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

#[embassy_executor::task]
async fn foo(_x: &u32) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_elided.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Arguments for tasks must live forever. Try using the `'static` lifetime.
--> tests/ui/nonstatic_ref_elided.rs:4:18
|
4 | async fn foo(_x: &u32) {}
| ^
6 changes: 6 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

#[embassy_executor::task]
async fn foo<'a>(_x: &'a u32) {}

fn main() {}
11 changes: 11 additions & 0 deletions embassy-executor/tests/ui/nonstatic_ref_generic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: task functions must not be generic
--> tests/ui/nonstatic_ref_generic.rs:4:1
|
4 | async fn foo<'a>(_x: &'a u32) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Arguments for tasks must live forever. Try using the `'static` lifetime.
--> tests/ui/nonstatic_ref_generic.rs:4:23
|
4 | async fn foo<'a>(_x: &'a u32) {}
| ^^
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/nonstatic_struct_anon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task(_x: Foo<'_>) {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/nonstatic_struct_anon.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Arguments for tasks must live forever. Try using the `'static` lifetime.
--> tests/ui/nonstatic_struct_anon.rs:6:23
|
6 | async fn task(_x: Foo<'_>) {}
| ^^
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/nonstatic_struct_elided.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task(_x: Foo) {}

fn main() {}
10 changes: 10 additions & 0 deletions embassy-executor/tests/ui/nonstatic_struct_elided.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error[E0726]: implicit elided lifetime not allowed here
--> tests/ui/nonstatic_struct_elided.rs:6:19
|
6 | async fn task(_x: Foo) {}
| ^^^ expected lifetime parameter
|
help: indicate the anonymous lifetime
|
6 | async fn task(_x: Foo<'_>) {}
| ++++
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/nonstatic_struct_generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task<'a>(_x: Foo<'a>) {}

fn main() {}
11 changes: 11 additions & 0 deletions embassy-executor/tests/ui/nonstatic_struct_generic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: task functions must not be generic
--> tests/ui/nonstatic_struct_generic.rs:6:1
|
6 | async fn task<'a>(_x: Foo<'a>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Arguments for tasks must live forever. Try using the `'static` lifetime.
--> tests/ui/nonstatic_struct_generic.rs:6:27
|
6 | async fn task<'a>(_x: Foo<'a>) {}
| ^^
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/not_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
fn task() {}

fn main() {}
5 changes: 5 additions & 0 deletions embassy-executor/tests/ui/not_async.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: task functions must be async
--> tests/ui/not_async.rs:6:1
|
6 | fn task() {}
| ^^^^^^^^^
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task(self) {}

fn main() {}
13 changes: 13 additions & 0 deletions embassy-executor/tests/ui/self.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: task functions must not have `self` arguments
--> tests/ui/self.rs:6:15
|
6 | async fn task(self) {}
| ^^^^

error: `self` parameter is only allowed in associated functions
--> tests/ui/self.rs:6:15
|
6 | async fn task(self) {}
| ^^^^ not semantically valid as function parameter
|
= note: associated functions are those in `impl` or `trait` definitions
8 changes: 8 additions & 0 deletions embassy-executor/tests/ui/self_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task(&mut self) {}

fn main() {}
13 changes: 13 additions & 0 deletions embassy-executor/tests/ui/self_ref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: task functions must not have `self` arguments
--> tests/ui/self_ref.rs:6:15
|
6 | async fn task(&mut self) {}
| ^^^^^^^^^

error: `self` parameter is only allowed in associated functions
--> tests/ui/self_ref.rs:6:15
|
6 | async fn task(&mut self) {}
| ^^^^^^^^^ not semantically valid as function parameter
|
= note: associated functions are those in `impl` or `trait` definitions
12 changes: 12 additions & 0 deletions embassy-executor/tests/ui/where_clause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

struct Foo<'a>(&'a ());

#[embassy_executor::task]
async fn task()
where
(): Sized,
{
}

fn main() {}
7 changes: 7 additions & 0 deletions embassy-executor/tests/ui/where_clause.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: task functions must not have `where` clauses
--> tests/ui/where_clause.rs:6:1
|
6 | / async fn task()
7 | | where
8 | | (): Sized,
| |______________^

0 comments on commit f92b27d

Please sign in to comment.