diff options
Diffstat (limited to 'embassy-executor-macros')
| -rw-r--r-- | embassy-executor-macros/src/macros/task.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/embassy-executor-macros/src/macros/task.rs b/embassy-executor-macros/src/macros/task.rs index 0404dba64..2f2aeda76 100644 --- a/embassy-executor-macros/src/macros/task.rs +++ b/embassy-executor-macros/src/macros/task.rs | |||
| @@ -2,7 +2,7 @@ use darling::export::NestedMeta; | |||
| 2 | use darling::FromMeta; | 2 | use darling::FromMeta; |
| 3 | use proc_macro2::{Span, TokenStream}; | 3 | use proc_macro2::{Span, TokenStream}; |
| 4 | use quote::{format_ident, quote}; | 4 | use quote::{format_ident, quote}; |
| 5 | use syn::visit::Visit; | 5 | use syn::visit::{self, Visit}; |
| 6 | use syn::{Expr, ExprLit, Lit, LitInt, ReturnType, Type}; | 6 | use syn::{Expr, ExprLit, Lit, LitInt, ReturnType, Type}; |
| 7 | 7 | ||
| 8 | use crate::util::*; | 8 | use crate::util::*; |
| @@ -76,7 +76,7 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 76 | for arg in fargs.iter_mut() { | 76 | for arg in fargs.iter_mut() { |
| 77 | match arg { | 77 | match arg { |
| 78 | syn::FnArg::Receiver(_) => { | 78 | syn::FnArg::Receiver(_) => { |
| 79 | error(&mut errors, arg, "task functions must not have receiver arguments"); | 79 | error(&mut errors, arg, "task functions must not have `self` arguments"); |
| 80 | } | 80 | } |
| 81 | syn::FnArg::Typed(t) => { | 81 | syn::FnArg::Typed(t) => { |
| 82 | check_arg_ty(&mut errors, &t.ty); | 82 | check_arg_ty(&mut errors, &t.ty); |
| @@ -180,6 +180,28 @@ fn check_arg_ty(errors: &mut TokenStream, ty: &Type) { | |||
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | impl<'a, 'ast> Visit<'ast> for Visitor<'a> { | 182 | impl<'a, 'ast> Visit<'ast> for Visitor<'a> { |
| 183 | fn visit_type_reference(&mut self, i: &'ast syn::TypeReference) { | ||
| 184 | // only check for elided lifetime here. If not elided, it's checked by `visit_lifetime`. | ||
| 185 | if i.lifetime.is_none() { | ||
| 186 | error( | ||
| 187 | self.errors, | ||
| 188 | i.and_token, | ||
| 189 | "Arguments for tasks must live forever. Try using the `'static` lifetime.", | ||
| 190 | ) | ||
| 191 | } | ||
| 192 | visit::visit_type_reference(self, i); | ||
| 193 | } | ||
| 194 | |||
| 195 | fn visit_lifetime(&mut self, i: &'ast syn::Lifetime) { | ||
| 196 | if i.ident.to_string() != "static" { | ||
| 197 | error( | ||
| 198 | self.errors, | ||
| 199 | i, | ||
| 200 | "Arguments for tasks must live forever. Try using the `'static` lifetime.", | ||
| 201 | ) | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 183 | fn visit_type_impl_trait(&mut self, i: &'ast syn::TypeImplTrait) { | 205 | fn visit_type_impl_trait(&mut self, i: &'ast syn::TypeImplTrait) { |
| 184 | error(self.errors, i, "`impl Trait` is not allowed in task arguments. It is syntax sugar for generics, and tasks can't be generic."); | 206 | error(self.errors, i, "`impl Trait` is not allowed in task arguments. It is syntax sugar for generics, and tasks can't be generic."); |
| 185 | } | 207 | } |
