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