|
| 1 | +use proc_macro2::Span; |
| 2 | +use syn::fold::Fold; |
| 3 | +use syn::punctuated::Punctuated; |
| 4 | +use syn::token::Comma; |
| 5 | +use syn::{ArgSelfRef, FnArg, GenericParam, Lifetime, LifetimeDef, TypeReference}; |
| 6 | + |
| 7 | +pub(super) fn unelide_lifetimes( |
| 8 | + generics: &mut Punctuated<GenericParam, Comma>, |
| 9 | + args: Vec<FnArg>, |
| 10 | +) -> Vec<FnArg> { |
| 11 | + let mut folder = UnelideLifetimes::new(generics); |
| 12 | + args.into_iter().map(|arg| folder.fold_fn_arg(arg)).collect() |
| 13 | +} |
| 14 | + |
| 15 | +struct UnelideLifetimes<'a> { |
| 16 | + generics: &'a mut Punctuated<GenericParam, Comma>, |
| 17 | + lifetime_index: usize, |
| 18 | + lifetime_name: String, |
| 19 | + count: u32, |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a> UnelideLifetimes<'a> { |
| 23 | + fn new(generics: &'a mut Punctuated<GenericParam, Comma>) -> UnelideLifetimes<'a> { |
| 24 | + let lifetime_index = lifetime_index(generics); |
| 25 | + let lifetime_name = lifetime_name(generics); |
| 26 | + UnelideLifetimes { generics, lifetime_index, lifetime_name, count: 0 } |
| 27 | + } |
| 28 | + |
| 29 | + // Constitute a new lifetime |
| 30 | + fn new_lifetime(&mut self) -> Lifetime { |
| 31 | + let lifetime_name = format!("{}{}", self.lifetime_name, self.count); |
| 32 | + let lifetime = Lifetime::new(&lifetime_name, Span::call_site()); |
| 33 | + |
| 34 | + let idx = self.lifetime_index + self.count as usize; |
| 35 | + self.generics.insert(idx, GenericParam::Lifetime(LifetimeDef::new(lifetime.clone()))); |
| 36 | + self.count += 1; |
| 37 | + |
| 38 | + lifetime |
| 39 | + } |
| 40 | + |
| 41 | + // Take an Option<Lifetime> and guarantee its an unelided lifetime |
| 42 | + fn expand_lifetime(&mut self, lifetime: Option<Lifetime>) -> Lifetime { |
| 43 | + match lifetime { |
| 44 | + Some(l) => self.fold_lifetime(l), |
| 45 | + None => self.new_lifetime(), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl Fold for UnelideLifetimes<'_> { |
| 51 | + // Handling self arguments |
| 52 | + fn fold_arg_self_ref(&mut self, arg: ArgSelfRef) -> ArgSelfRef { |
| 53 | + let ArgSelfRef { and_token, lifetime, mutability, self_token } = arg; |
| 54 | + let lifetime = Some(self.expand_lifetime(lifetime)); |
| 55 | + ArgSelfRef { and_token, lifetime, mutability, self_token } |
| 56 | + } |
| 57 | + |
| 58 | + // If the lifetime is `'_`, replace it with a new unelided lifetime |
| 59 | + fn fold_lifetime(&mut self, lifetime: Lifetime) -> Lifetime { |
| 60 | + if lifetime.ident == "_" { |
| 61 | + self.new_lifetime() |
| 62 | + } else { |
| 63 | + lifetime |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // If the reference's lifetime is elided, replace it with a new unelided lifetime |
| 68 | + fn fold_type_reference(&mut self, ty_ref: TypeReference) -> TypeReference { |
| 69 | + let TypeReference { and_token, lifetime, mutability, elem } = ty_ref; |
| 70 | + let lifetime = Some(self.expand_lifetime(lifetime)); |
| 71 | + let elem = Box::new(self.fold_type(*elem)); |
| 72 | + TypeReference { and_token, lifetime, mutability, elem } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn lifetime_index(generics: &Punctuated<GenericParam, Comma>) -> usize { |
| 77 | + generics |
| 78 | + .iter() |
| 79 | + .take_while(|param| if let GenericParam::Lifetime(_) = param { true } else { false }) |
| 80 | + .count() |
| 81 | +} |
| 82 | + |
| 83 | +// Determine the prefix for all lifetime names. Ensure it doesn't |
| 84 | +// overlap with any existing lifetime names. |
| 85 | +fn lifetime_name(generics: &Punctuated<GenericParam, Comma>) -> String { |
| 86 | + let mut lifetime_name = String::from("'_async"); |
| 87 | + let existing_lifetimes: Vec<String> = generics |
| 88 | + .iter() |
| 89 | + .filter_map(|param| { |
| 90 | + if let GenericParam::Lifetime(LifetimeDef { lifetime, .. }) = param { |
| 91 | + Some(lifetime.to_string()) |
| 92 | + } else { |
| 93 | + None |
| 94 | + } |
| 95 | + }) |
| 96 | + .collect(); |
| 97 | + while existing_lifetimes.iter().any(|name| name.starts_with(&lifetime_name)) { |
| 98 | + lifetime_name.push('_'); |
| 99 | + } |
| 100 | + lifetime_name |
| 101 | +} |
0 commit comments