diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-10-18 03:18:59 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-10-18 03:18:59 +0200 |
| commit | 1f58e0efd05e5c96409db301e4d481098038aedf (patch) | |
| tree | 853857285b5ff14eff9a0486dc3eae645de3ee03 /embassy-executor-macros/src/lib.rs | |
| parent | 3d0c557138e87ce94686e5820337c7495206ae56 (diff) | |
executor: fix unsoundness due to `impl Trait`, improve macro error handling. (#3425)
* executor-macros: don't parse function bodies.
* executor-macros: refactor for better recovery and ide-friendliness on errors.
* executor-macros: disallow `impl Trait` in task arguments.
Fixes #3420
* Fix example using `impl Trait` in tasks.
Diffstat (limited to 'embassy-executor-macros/src/lib.rs')
| -rw-r--r-- | embassy-executor-macros/src/lib.rs | 50 |
1 files changed, 7 insertions, 43 deletions
diff --git a/embassy-executor-macros/src/lib.rs b/embassy-executor-macros/src/lib.rs index 61d388b9e..5f2182f10 100644 --- a/embassy-executor-macros/src/lib.rs +++ b/embassy-executor-macros/src/lib.rs | |||
| @@ -1,28 +1,11 @@ | |||
| 1 | #![doc = include_str!("../README.md")] | 1 | #![doc = include_str!("../README.md")] |
| 2 | extern crate proc_macro; | 2 | extern crate proc_macro; |
| 3 | 3 | ||
| 4 | use darling::ast::NestedMeta; | ||
| 5 | use proc_macro::TokenStream; | 4 | use proc_macro::TokenStream; |
| 6 | 5 | ||
| 7 | mod macros; | 6 | mod macros; |
| 8 | mod util; | 7 | mod util; |
| 9 | use macros::*; | 8 | use macros::*; |
| 10 | use syn::parse::{Parse, ParseBuffer}; | ||
| 11 | use syn::punctuated::Punctuated; | ||
| 12 | use syn::Token; | ||
| 13 | |||
| 14 | struct Args { | ||
| 15 | meta: Vec<NestedMeta>, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl Parse for Args { | ||
| 19 | fn parse(input: &ParseBuffer) -> syn::Result<Self> { | ||
| 20 | let meta = Punctuated::<NestedMeta, Token![,]>::parse_terminated(input)?; | ||
| 21 | Ok(Args { | ||
| 22 | meta: meta.into_iter().collect(), | ||
| 23 | }) | ||
| 24 | } | ||
| 25 | } | ||
| 26 | 9 | ||
| 27 | /// Declares an async task that can be run by `embassy-executor`. The optional `pool_size` parameter can be used to specify how | 10 | /// Declares an async task that can be run by `embassy-executor`. The optional `pool_size` parameter can be used to specify how |
| 28 | /// many concurrent tasks can be spawned (default is 1) for the function. | 11 | /// many concurrent tasks can be spawned (default is 1) for the function. |
| @@ -56,17 +39,12 @@ impl Parse for Args { | |||
| 56 | /// ``` | 39 | /// ``` |
| 57 | #[proc_macro_attribute] | 40 | #[proc_macro_attribute] |
| 58 | pub fn task(args: TokenStream, item: TokenStream) -> TokenStream { | 41 | pub fn task(args: TokenStream, item: TokenStream) -> TokenStream { |
| 59 | let args = syn::parse_macro_input!(args as Args); | 42 | task::run(args.into(), item.into()).into() |
| 60 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 61 | |||
| 62 | task::run(&args.meta, f).unwrap_or_else(|x| x).into() | ||
| 63 | } | 43 | } |
| 64 | 44 | ||
| 65 | #[proc_macro_attribute] | 45 | #[proc_macro_attribute] |
| 66 | pub fn main_avr(args: TokenStream, item: TokenStream) -> TokenStream { | 46 | pub fn main_avr(args: TokenStream, item: TokenStream) -> TokenStream { |
| 67 | let args = syn::parse_macro_input!(args as Args); | 47 | main::run(args.into(), item.into(), &main::ARCH_AVR).into() |
| 68 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 69 | main::run(&args.meta, f, main::avr()).unwrap_or_else(|x| x).into() | ||
| 70 | } | 48 | } |
| 71 | 49 | ||
| 72 | /// Creates a new `executor` instance and declares an application entry point for Cortex-M spawning the corresponding function body as an async task. | 50 | /// Creates a new `executor` instance and declares an application entry point for Cortex-M spawning the corresponding function body as an async task. |
| @@ -89,9 +67,7 @@ pub fn main_avr(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 89 | /// ``` | 67 | /// ``` |
| 90 | #[proc_macro_attribute] | 68 | #[proc_macro_attribute] |
| 91 | pub fn main_cortex_m(args: TokenStream, item: TokenStream) -> TokenStream { | 69 | pub fn main_cortex_m(args: TokenStream, item: TokenStream) -> TokenStream { |
| 92 | let args = syn::parse_macro_input!(args as Args); | 70 | main::run(args.into(), item.into(), &main::ARCH_CORTEX_M).into() |
| 93 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 94 | main::run(&args.meta, f, main::cortex_m()).unwrap_or_else(|x| x).into() | ||
| 95 | } | 71 | } |
| 96 | 72 | ||
| 97 | /// Creates a new `executor` instance and declares an architecture agnostic application entry point spawning | 73 | /// Creates a new `executor` instance and declares an architecture agnostic application entry point spawning |
| @@ -116,11 +92,7 @@ pub fn main_cortex_m(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 116 | /// ``` | 92 | /// ``` |
| 117 | #[proc_macro_attribute] | 93 | #[proc_macro_attribute] |
| 118 | pub fn main_spin(args: TokenStream, item: TokenStream) -> TokenStream { | 94 | pub fn main_spin(args: TokenStream, item: TokenStream) -> TokenStream { |
| 119 | let args = syn::parse_macro_input!(args as Args); | 95 | main::run(args.into(), item.into(), &main::ARCH_SPIN).into() |
| 120 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 121 | main::run(&args.meta, f, main::spin(&args.meta)) | ||
| 122 | .unwrap_or_else(|x| x) | ||
| 123 | .into() | ||
| 124 | } | 96 | } |
| 125 | 97 | ||
| 126 | /// Creates a new `executor` instance and declares an application entry point for RISC-V spawning the corresponding function body as an async task. | 98 | /// Creates a new `executor` instance and declares an application entry point for RISC-V spawning the corresponding function body as an async task. |
| @@ -153,11 +125,7 @@ pub fn main_spin(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 153 | /// ``` | 125 | /// ``` |
| 154 | #[proc_macro_attribute] | 126 | #[proc_macro_attribute] |
| 155 | pub fn main_riscv(args: TokenStream, item: TokenStream) -> TokenStream { | 127 | pub fn main_riscv(args: TokenStream, item: TokenStream) -> TokenStream { |
| 156 | let args = syn::parse_macro_input!(args as Args); | 128 | main::run(args.into(), item.into(), &main::ARCH_RISCV).into() |
| 157 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 158 | main::run(&args.meta, f, main::riscv(&args.meta)) | ||
| 159 | .unwrap_or_else(|x| x) | ||
| 160 | .into() | ||
| 161 | } | 129 | } |
| 162 | 130 | ||
| 163 | /// Creates a new `executor` instance and declares an application entry point for STD spawning the corresponding function body as an async task. | 131 | /// Creates a new `executor` instance and declares an application entry point for STD spawning the corresponding function body as an async task. |
| @@ -180,9 +148,7 @@ pub fn main_riscv(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 180 | /// ``` | 148 | /// ``` |
| 181 | #[proc_macro_attribute] | 149 | #[proc_macro_attribute] |
| 182 | pub fn main_std(args: TokenStream, item: TokenStream) -> TokenStream { | 150 | pub fn main_std(args: TokenStream, item: TokenStream) -> TokenStream { |
| 183 | let args = syn::parse_macro_input!(args as Args); | 151 | main::run(args.into(), item.into(), &main::ARCH_STD).into() |
| 184 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 185 | main::run(&args.meta, f, main::std()).unwrap_or_else(|x| x).into() | ||
| 186 | } | 152 | } |
| 187 | 153 | ||
| 188 | /// Creates a new `executor` instance and declares an application entry point for WASM spawning the corresponding function body as an async task. | 154 | /// Creates a new `executor` instance and declares an application entry point for WASM spawning the corresponding function body as an async task. |
| @@ -205,7 +171,5 @@ pub fn main_std(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 205 | /// ``` | 171 | /// ``` |
| 206 | #[proc_macro_attribute] | 172 | #[proc_macro_attribute] |
| 207 | pub fn main_wasm(args: TokenStream, item: TokenStream) -> TokenStream { | 173 | pub fn main_wasm(args: TokenStream, item: TokenStream) -> TokenStream { |
| 208 | let args = syn::parse_macro_input!(args as Args); | 174 | main::run(args.into(), item.into(), &main::ARCH_WASM).into() |
| 209 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 210 | main::run(&args.meta, f, main::wasm()).unwrap_or_else(|x| x).into() | ||
| 211 | } | 175 | } |
