• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use proc_macro2::TokenStream;
2 use quote::{format_ident, quote, ToTokens};
3 use syn::{
4     parse_quote, spanned::Spanned, visit_mut::VisitMut, Error, FnArg, GenericArgument, ImplItem,
5     ItemImpl, Pat, PatIdent, Path, PathArguments, Result, ReturnType, Signature, Token, Type,
6     TypePath, TypeReference,
7 };
8 
9 use crate::utils::{parse_as_empty, prepend_underscore_to_self, ReplaceReceiver, SliceExt};
10 
attribute(args: &TokenStream, mut input: ItemImpl) -> TokenStream11 pub(crate) fn attribute(args: &TokenStream, mut input: ItemImpl) -> TokenStream {
12     let res = (|| -> Result<()> {
13         parse_as_empty(args)?;
14         validate_impl(&input)?;
15         expand_impl(&mut input);
16         Ok(())
17     })();
18 
19     if let Err(e) = res {
20         let mut tokens = e.to_compile_error();
21         if let Type::Path(self_ty) = &*input.self_ty {
22             let (impl_generics, _, where_clause) = input.generics.split_for_impl();
23 
24             // Generate a dummy impl of `PinnedDrop`.
25             // In many cases, `#[pinned_drop] impl` is declared after `#[pin_project]`.
26             // Therefore, if `pinned_drop` compile fails, you will also get an error
27             // about `PinnedDrop` not being implemented.
28             // This can be prevented to some extent by generating a dummy
29             // `PinnedDrop` implementation.
30             // We already know that we will get a compile error, so this won't
31             // accidentally compile successfully.
32             //
33             // However, if `input.self_ty` is not Type::Path, there is a high possibility that
34             // the type does not exist (since #[pin_project] can only be used on struct/enum
35             // definitions), so do not generate a dummy impl.
36             tokens.extend(quote! {
37                 impl #impl_generics ::pin_project::__private::PinnedDrop for #self_ty
38                 #where_clause
39                 {
40                     unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {}
41                 }
42             });
43         }
44         tokens
45     } else {
46         input.into_token_stream()
47     }
48 }
49 
50 /// Validates the signature of given `PinnedDrop` impl.
validate_impl(item: &ItemImpl) -> Result<()>51 fn validate_impl(item: &ItemImpl) -> Result<()> {
52     const INVALID_ITEM: &str =
53         "#[pinned_drop] may only be used on implementation for the `PinnedDrop` trait";
54 
55     if let Some(attr) = item.attrs.find("pinned_drop") {
56         return Err(error!(attr, "duplicate #[pinned_drop] attribute"));
57     }
58 
59     if let Some((_, path, _)) = &item.trait_ {
60         if !path.is_ident("PinnedDrop") {
61             return Err(error!(path, INVALID_ITEM));
62         }
63     } else {
64         return Err(error!(item.self_ty, INVALID_ITEM));
65     }
66 
67     if item.unsafety.is_some() {
68         return Err(error!(item.unsafety, "implementing the trait `PinnedDrop` is not unsafe"));
69     }
70     if item.items.is_empty() {
71         return Err(error!(item, "not all trait items implemented, missing: `drop`"));
72     }
73 
74     match &*item.self_ty {
75         Type::Path(_) => {}
76         ty => {
77             return Err(error!(
78                 ty,
79                 "implementing the trait `PinnedDrop` on this type is unsupported"
80             ));
81         }
82     }
83 
84     item.items.iter().enumerate().try_for_each(|(i, item)| match item {
85         ImplItem::Const(item) => {
86             Err(error!(item, "const `{}` is not a member of trait `PinnedDrop`", item.ident))
87         }
88         ImplItem::Type(item) => {
89             Err(error!(item, "type `{}` is not a member of trait `PinnedDrop`", item.ident))
90         }
91         ImplItem::Method(method) => {
92             validate_sig(&method.sig)?;
93             if i == 0 {
94                 Ok(())
95             } else {
96                 Err(error!(method, "duplicate definitions with name `drop`"))
97             }
98         }
99         _ => unreachable!("unexpected ImplItem"),
100     })
101 }
102 
103 /// Validates the signature of given `PinnedDrop::drop` method.
104 ///
105 /// The correct signature is: `(mut) self: (<path>::)Pin<&mut Self>`
validate_sig(sig: &Signature) -> Result<()>106 fn validate_sig(sig: &Signature) -> Result<()> {
107     fn get_ty_path(ty: &Type) -> Option<&Path> {
108         if let Type::Path(TypePath { qself: None, path }) = ty { Some(path) } else { None }
109     }
110 
111     const INVALID_ARGUMENT: &str = "method `drop` must take an argument `self: Pin<&mut Self>`";
112 
113     if sig.ident != "drop" {
114         return Err(error!(
115             sig.ident,
116             "method `{}` is not a member of trait `PinnedDrop", sig.ident,
117         ));
118     }
119 
120     if let ReturnType::Type(_, ty) = &sig.output {
121         match &**ty {
122             Type::Tuple(ty) if ty.elems.is_empty() => {}
123             _ => return Err(error!(ty, "method `drop` must return the unit type")),
124         }
125     }
126 
127     match sig.inputs.len() {
128         1 => {}
129         0 => return Err(Error::new(sig.paren_token.span, INVALID_ARGUMENT)),
130         _ => return Err(error!(sig.inputs, INVALID_ARGUMENT)),
131     }
132 
133     if let Some(FnArg::Typed(arg)) = sig.receiver() {
134         // (mut) self: <path>
135         if let Some(path) = get_ty_path(&arg.ty) {
136             let ty = path.segments.last().unwrap();
137             if let PathArguments::AngleBracketed(args) = &ty.arguments {
138                 // (mut) self: (<path>::)<ty><&mut <elem>..>
139                 if let Some(GenericArgument::Type(Type::Reference(TypeReference {
140                     mutability: Some(_),
141                     elem,
142                     ..
143                 }))) = args.args.first()
144                 {
145                     // (mut) self: (<path>::)Pin<&mut Self>
146                     if args.args.len() == 1
147                         && ty.ident == "Pin"
148                         && get_ty_path(elem).map_or(false, |path| path.is_ident("Self"))
149                     {
150                         if sig.unsafety.is_some() {
151                             return Err(error!(
152                                 sig.unsafety,
153                                 "implementing the method `drop` is not unsafe"
154                             ));
155                         }
156                         return Ok(());
157                     }
158                 }
159             }
160         }
161     }
162 
163     Err(error!(sig.inputs[0], INVALID_ARGUMENT))
164 }
165 
166 // from:
167 //
168 // fn drop(self: Pin<&mut Self>) {
169 //     // ...
170 // }
171 //
172 // into:
173 //
174 // unsafe fn drop(self: Pin<&mut Self>) {
175 //     fn __drop_inner<T>(__self: Pin<&mut Foo<'_, T>>) {
176 //         fn __drop_inner() {}
177 //         // ...
178 //     }
179 //     __drop_inner(self);
180 // }
181 //
expand_impl(item: &mut ItemImpl)182 fn expand_impl(item: &mut ItemImpl) {
183     fn get_arg_pat(arg: &mut FnArg) -> Option<&mut PatIdent> {
184         if let FnArg::Typed(arg) = arg {
185             if let Pat::Ident(ident) = &mut *arg.pat {
186                 return Some(ident);
187             }
188         }
189         None
190     }
191 
192     let path = &mut item.trait_.as_mut().unwrap().1;
193     *path = parse_quote_spanned! { path.span() =>
194         ::pin_project::__private::PinnedDrop
195     };
196 
197     let method =
198         if let ImplItem::Method(method) = &mut item.items[0] { method } else { unreachable!() };
199 
200     // `fn drop(mut self: Pin<&mut Self>)` -> `fn __drop_inner<T>(mut __self: Pin<&mut Receiver>)`
201     let drop_inner = {
202         let mut drop_inner = method.clone();
203         let ident = format_ident!("__drop_inner");
204         // Add a dummy `__drop_inner` function to prevent users call outer `__drop_inner`.
205         drop_inner.block.stmts.insert(0, parse_quote!(fn #ident() {}));
206         drop_inner.sig.ident = ident;
207         drop_inner.sig.generics = item.generics.clone();
208         let self_pat = get_arg_pat(&mut drop_inner.sig.inputs[0]).unwrap();
209         prepend_underscore_to_self(&mut self_pat.ident);
210         let self_ty = if let Type::Path(ty) = &*item.self_ty { ty } else { unreachable!() };
211         let mut visitor = ReplaceReceiver(self_ty);
212         visitor.visit_signature_mut(&mut drop_inner.sig);
213         visitor.visit_block_mut(&mut drop_inner.block);
214         drop_inner
215     };
216 
217     // `fn drop(mut self: Pin<&mut Self>)` -> `unsafe fn drop(self: Pin<&mut Self>)`
218     method.sig.unsafety = Some(<Token![unsafe]>::default());
219     let self_pat = get_arg_pat(&mut method.sig.inputs[0]).unwrap();
220     self_pat.mutability = None;
221     let self_token = &self_pat.ident;
222 
223     method.block.stmts = parse_quote! {
224         #[allow(clippy::needless_pass_by_value)] // This lint does not warn the receiver.
225         #drop_inner
226         __drop_inner(#self_token);
227     };
228 }
229