From aace4a90d36f21aee2c757f240572d6c6da842f9 Mon Sep 17 00:00:00 2001 From: 823984418 <823984418@qq.com> Date: Sun, 30 Jul 2023 18:09:11 +0800 Subject: [PATCH] Consider lifetime when adding trait bounds. --- src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5c5cd0c..069319b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,8 +169,8 @@ use syn::parse::{ParseStream, Parser}; use syn::visit::{self, Visit}; use syn::{ braced, punctuated, token, Attribute, Data, DeriveInput, Error, Expr, Field, Fields, - FieldsNamed, FieldsUnnamed, GenericParam, Generics, Ident, PredicateType, Result, Token, - TraitBound, Type, TypeMacro, TypeParamBound, TypePath, WhereClause, WherePredicate, + FieldsNamed, FieldsUnnamed, GenericParam, Generics, Ident, Lifetime, PredicateType, Result, + Token, TraitBound, Type, TypeMacro, TypeParamBound, TypePath, WhereClause, WherePredicate, }; use quote::{format_ident, quote_spanned, ToTokens}; @@ -476,6 +476,16 @@ fn get_ty_params(field: &Field, generics: &Generics) -> Vec { } } + fn visit_lifetime(&mut self, id: &'a Lifetime) { + for (idx, i) in self.generics.params.iter().enumerate() { + if let GenericParam::Lifetime(lparam) = i { + if lparam.lifetime == *id { + self.result[idx] = true; + } + } + } + } + fn visit_type_macro(&mut self, x: &'a TypeMacro) { // If we see a type_mac declaration, then we can't know what type parameters // it might be binding, so we presume it binds all of them.