Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generates invalid code after rust-lang/rust#89970 #4

Closed
i64 opened this issue Nov 17, 2021 · 1 comment
Closed

Generates invalid code after rust-lang/rust#89970 #4

i64 opened this issue Nov 17, 2021 · 1 comment

Comments

@i64
Copy link

i64 commented Nov 17, 2021

generates invalid code after rust-lang/rust#89970

the generated code:

  fn open< 'a>(& 'a mut self,path: & 'a[u8],flags:usize) -> Self::__real_async_trait_impl_TypeFor_open< 'a> ;
  type __real_async_trait_impl_TypeFor_open< 'a> : ::core::future::Future<Output = Result<usize,Errno> > + 'a;
18 | #[real_async_trait]
   | ^^^^^^^^^^^^^^^^^^^
...
21 |     async fn open<'a>(&'a mut self, path: &'a [u8], flags: usize) -> Result<usize, Errno>;
   |                     - help: add the required where clauses: `where Self: 'a`

the expected code:

    fn open<'a>(&'a mut self, path: &'a [u8], flags: usize, ) -> Self::__real_async_trait_impl_TypeFor_opsen<'a>;
    
    type __real_async_trait_impl_TypeFor_opsen<'a>: ::core::future::Future<Output = Result<usize, Errno>> + 'a where Self: 'a;

i'm not sure if this is solving the issue completely but it was ok in my case.

diff --git a/src/lib.rs b/src/lib.rs
index 1366b81..cc2ba22 100644
--- a/src/lib.rs
+++ b/src/lib.rs
 use syn::{
     AngleBracketedGenericArguments, AttrStyle, Attribute, Binding, Block, Expr, ExprAsync, FnArg,
     GenericArgument, GenericParam, Generics, Ident, ImplItem, ImplItemType, ItemImpl, ItemTrait,
@@ -311,7 +315,30 @@ fn handle_item_impl(mut item: ItemImpl) -> TokenStream {
             generics: Generics {
                 lt_token: Some(Token!(<)(Span::call_site())),
                 gt_token: Some(Token!(>)(Span::call_site())),
-                where_clause: None,
+                where_clause: Some(WhereClause {
+                    where_token: Where::default(),
+                    predicates: function_lifetimes
+                        .iter()
+                        .cloned()
+                        .map(|lifetimedef| {
+                            WherePredicate::Type(PredicateType {
+                                colon_token: Token!(:)(Span::call_site()),
+                                lifetimes: None,
+                                bounded_ty: Type::Path(TypePath {
+                                    qself: None,
+                                    path: Path {
+                                        leading_colon: None,
+                                        segments: Punctuated::from_iter([PathSegment {
+                                            ident: Ident::new("Self", Span::call_site()),
+                                            arguments: PathArguments::None,
+                                        }]),
+                                    },
+                                }),
+                                bounds: Punctuated::from_iter([TypeParamBound::Lifetime(lifetimedef.lifetime)]),
+                            })
+                        })
+                        .collect(),
+                }),
                 params: function_lifetimes
                     .iter()
                     .cloned()
@@ -590,7 +617,30 @@ fn handle_item_trait(mut item: ItemTrait) -> TokenStream {
             generics: Generics {
                 lt_token: Some(Token!(<)(Span::call_site())),
                 gt_token: Some(Token!(>)(Span::call_site())),
-                where_clause: None,
+                where_clause: Some(WhereClause {
+                    where_token: Where::default(),
+                    predicates: function_lifetimes
+                        .iter()
+                        .cloned()
+                        .map(|lifetimedef| {
+                            WherePredicate::Type(PredicateType {
+                                colon_token: Token!(:)(Span::call_site()),
+                                lifetimes: None,
+                                bounded_ty: Type::Path(TypePath {
+                                    qself: None,
+                                    path: Path {
+                                        leading_colon: None,
+                                        segments: Punctuated::from_iter([PathSegment {
+                                            ident: Ident::new("Self", Span::call_site()),
+                                            arguments: PathArguments::None,
+                                        }]),
+                                    },
+                                }),
+                                bounds: Punctuated::from_iter([TypeParamBound::Lifetime(lifetimedef.lifetime)]),
+                            })
+                        })
+                        .collect(),
+                }),
                 params: function_lifetimes
                     .iter()
                     .cloned()
@4lDO2
Copy link
Owner

4lDO2 commented Jun 11, 2022

I think requiring Self: 'a should be fine. While it to me seems redundant to both include Def: 'a and the following clause where Self: 'a, we are AFAIK not imposing any further limits on which functions can be used when implementing the trait.

@4lDO2 4lDO2 closed this as completed Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants