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/util | |
| 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/util')
| -rw-r--r-- | embassy-executor-macros/src/util/ctxt.rs | 72 | ||||
| -rw-r--r-- | embassy-executor-macros/src/util/mod.rs | 1 |
2 files changed, 0 insertions, 73 deletions
diff --git a/embassy-executor-macros/src/util/ctxt.rs b/embassy-executor-macros/src/util/ctxt.rs deleted file mode 100644 index 9c78cda01..000000000 --- a/embassy-executor-macros/src/util/ctxt.rs +++ /dev/null | |||
| @@ -1,72 +0,0 @@ | |||
| 1 | // nifty utility borrowed from serde :) | ||
| 2 | // https://github.com/serde-rs/serde/blob/master/serde_derive/src/internals/ctxt.rs | ||
| 3 | |||
| 4 | use std::cell::RefCell; | ||
| 5 | use std::fmt::Display; | ||
| 6 | use std::thread; | ||
| 7 | |||
| 8 | use proc_macro2::TokenStream; | ||
| 9 | use quote::{quote, ToTokens}; | ||
| 10 | |||
| 11 | /// A type to collect errors together and format them. | ||
| 12 | /// | ||
| 13 | /// Dropping this object will cause a panic. It must be consumed using `check`. | ||
| 14 | /// | ||
| 15 | /// References can be shared since this type uses run-time exclusive mut checking. | ||
| 16 | #[derive(Default)] | ||
| 17 | pub struct Ctxt { | ||
| 18 | // The contents will be set to `None` during checking. This is so that checking can be | ||
| 19 | // enforced. | ||
| 20 | errors: RefCell<Option<Vec<syn::Error>>>, | ||
| 21 | } | ||
| 22 | |||
| 23 | impl Ctxt { | ||
| 24 | /// Create a new context object. | ||
| 25 | /// | ||
| 26 | /// This object contains no errors, but will still trigger a panic if it is not `check`ed. | ||
| 27 | pub fn new() -> Self { | ||
| 28 | Ctxt { | ||
| 29 | errors: RefCell::new(Some(Vec::new())), | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | /// Add an error to the context object with a tokenenizable object. | ||
| 34 | /// | ||
| 35 | /// The object is used for spanning in error messages. | ||
| 36 | pub fn error_spanned_by<A: ToTokens, T: Display>(&self, obj: A, msg: T) { | ||
| 37 | self.errors | ||
| 38 | .borrow_mut() | ||
| 39 | .as_mut() | ||
| 40 | .unwrap() | ||
| 41 | // Curb monomorphization from generating too many identical methods. | ||
| 42 | .push(syn::Error::new_spanned(obj.into_token_stream(), msg)); | ||
| 43 | } | ||
| 44 | |||
| 45 | /// Add one of Syn's parse errors. | ||
| 46 | #[allow(unused)] | ||
| 47 | pub fn syn_error(&self, err: syn::Error) { | ||
| 48 | self.errors.borrow_mut().as_mut().unwrap().push(err); | ||
| 49 | } | ||
| 50 | |||
| 51 | /// Consume this object, producing a formatted error string if there are errors. | ||
| 52 | pub fn check(self) -> Result<(), TokenStream> { | ||
| 53 | let errors = self.errors.borrow_mut().take().unwrap(); | ||
| 54 | match errors.len() { | ||
| 55 | 0 => Ok(()), | ||
| 56 | _ => Err(to_compile_errors(errors)), | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream { | ||
| 62 | let compile_errors = errors.iter().map(syn::Error::to_compile_error); | ||
| 63 | quote!(#(#compile_errors)*) | ||
| 64 | } | ||
| 65 | |||
| 66 | impl Drop for Ctxt { | ||
| 67 | fn drop(&mut self) { | ||
| 68 | if !thread::panicking() && self.errors.borrow().is_some() { | ||
| 69 | panic!("forgot to check for errors"); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
diff --git a/embassy-executor-macros/src/util/mod.rs b/embassy-executor-macros/src/util/mod.rs deleted file mode 100644 index 28702809e..000000000 --- a/embassy-executor-macros/src/util/mod.rs +++ /dev/null | |||
| @@ -1 +0,0 @@ | |||
| 1 | pub mod ctxt; | ||
