Skip to content

Commit

Permalink
feat(macros): improve spawner macro errors (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
ROMemories authored Jul 1, 2024
2 parents e1cc771 + 0529790 commit 22bcdc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
22 changes: 16 additions & 6 deletions src/riot-rs-macros/src/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#[proc_macro_attribute]
pub fn spawner(args: TokenStream, item: TokenStream) -> TokenStream {
use quote::{format_ident, quote};
use syn::{spanned::Spanned, Error};

#[allow(clippy::wildcard_imports)]
use spawner::*;
Expand All @@ -55,12 +56,21 @@ pub fn spawner(args: TokenStream, item: TokenStream) -> TokenStream {
"spawner functions cannot be async, consider using `task` instead",
);

if !attrs.peripherals {
let param_count = spawner_function.sig.inputs.len();
assert!(
param_count == 1,
"to provide this function with peripherals, use the `{PERIPHERALS_PARAM}` macro parameter",
);
let param_count = spawner_function.sig.inputs.len();
if param_count == 0 {
let span = spawner_function.sig.span();
let error = Error::new(span, "`spawner: Spawner` function argument missing");
return error.to_compile_error().into();
} else if param_count == 2 && !attrs.peripherals {
let span = proc_macro2::Span::call_site();
let mut error = Error::new(span, "`peripherals` macro parameter missing here ...");

error.combine(Error::new(
spawner_function.sig.inputs.span(),
"... because this function has a second parameter",
));

return error.to_compile_error().into();
}

let riot_rs_crate = utils::riot_rs_crate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
error: custom attribute panicked
error: `peripherals` macro parameter missing here ...
--> tests/ui/spawner/missing_peripherals_param.rs:10:1
|
10 | #[riot_rs::spawner(autostart)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: to provide this function with peripherals, use the `peripherals` macro parameter
= note: this error originates in the attribute macro `riot_rs::spawner` (in Nightly builds, run with -Z macro-backtrace for more info)

error: ... because this function has a second parameter
--> tests/ui/spawner/missing_peripherals_param.rs:11:9
|
11 | fn main(_spawner: Spawner, _peripherals: Peripherals) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 comments on commit 22bcdc8

Please sign in to comment.