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 bail!(attr, "duplicate #[pinned_drop] attribute");
57 }
58
59 if let Some((_, path, _)) = &item.trait_ {
60 if !path.is_ident("PinnedDrop") {
61 bail!(path, INVALID_ITEM);
62 }
63 } else {
64 bail!(item.self_ty, INVALID_ITEM);
65 }
66
67 if item.unsafety.is_some() {
68 bail!(item.unsafety, "implementing the trait `PinnedDrop` is not unsafe");
69 }
70 if item.items.is_empty() {
71 bail!(item, "not all trait items implemented, missing: `drop`");
72 }
73
74 match &*item.self_ty {
75 Type::Path(_) => {}
76 ty => {
77 bail!(ty, "implementing the trait `PinnedDrop` on this type is unsupported");
78 }
79 }
80
81 item.items.iter().enumerate().try_for_each(|(i, item)| match item {
82 ImplItem::Const(item) => {
83 bail!(item, "const `{}` is not a member of trait `PinnedDrop`", item.ident)
84 }
85 ImplItem::Type(item) => {
86 bail!(item, "type `{}` is not a member of trait `PinnedDrop`", item.ident)
87 }
88 ImplItem::Method(method) => {
89 validate_sig(&method.sig)?;
90 if i == 0 {
91 Ok(())
92 } else {
93 bail!(method, "duplicate definitions with name `drop`")
94 }
95 }
96 _ => unreachable!("unexpected ImplItem"),
97 })
98 }
99
100 /// Validates the signature of given `PinnedDrop::drop` method.
101 ///
102 /// The correct signature is: `(mut) self: (<path>::)Pin<&mut Self>`
validate_sig(sig: &Signature) -> Result<()>103 fn validate_sig(sig: &Signature) -> Result<()> {
104 fn get_ty_path(ty: &Type) -> Option<&Path> {
105 if let Type::Path(TypePath { qself: None, path }) = ty {
106 Some(path)
107 } else {
108 None
109 }
110 }
111
112 const INVALID_ARGUMENT: &str = "method `drop` must take an argument `self: Pin<&mut Self>`";
113
114 if sig.ident != "drop" {
115 bail!(sig.ident, "method `{}` is not a member of trait `PinnedDrop", sig.ident,);
116 }
117
118 if let ReturnType::Type(_, ty) = &sig.output {
119 match &**ty {
120 Type::Tuple(ty) if ty.elems.is_empty() => {}
121 _ => bail!(ty, "method `drop` must return the unit type"),
122 }
123 }
124
125 match sig.inputs.len() {
126 1 => {}
127 0 => return Err(Error::new(sig.paren_token.span, INVALID_ARGUMENT)),
128 _ => bail!(sig.inputs, INVALID_ARGUMENT),
129 }
130
131 if let Some(FnArg::Typed(arg)) = sig.receiver() {
132 // (mut) self: <path>
133 if let Some(path) = get_ty_path(&arg.ty) {
134 let ty = path.segments.last().unwrap();
135 if let PathArguments::AngleBracketed(args) = &ty.arguments {
136 // (mut) self: (<path>::)<ty><&mut <elem>..>
137 if let Some(GenericArgument::Type(Type::Reference(TypeReference {
138 mutability: Some(_),
139 elem,
140 ..
141 }))) = args.args.first()
142 {
143 // (mut) self: (<path>::)Pin<&mut Self>
144 if args.args.len() == 1
145 && ty.ident == "Pin"
146 && get_ty_path(elem).map_or(false, |path| path.is_ident("Self"))
147 {
148 if sig.unsafety.is_some() {
149 bail!(sig.unsafety, "implementing the method `drop` is not unsafe");
150 }
151 return Ok(());
152 }
153 }
154 }
155 }
156 }
157
158 bail!(sig.inputs[0], INVALID_ARGUMENT)
159 }
160
161 // from:
162 //
163 // fn drop(self: Pin<&mut Self>) {
164 // // ...
165 // }
166 //
167 // into:
168 //
169 // unsafe fn drop(self: Pin<&mut Self>) {
170 // fn __drop_inner<T>(__self: Pin<&mut Foo<'_, T>>) {
171 // fn __drop_inner() {}
172 // // ...
173 // }
174 // __drop_inner(self);
175 // }
176 //
expand_impl(item: &mut ItemImpl)177 fn expand_impl(item: &mut ItemImpl) {
178 fn get_arg_pat(arg: &mut FnArg) -> Option<&mut PatIdent> {
179 if let FnArg::Typed(arg) = arg {
180 if let Pat::Ident(ident) = &mut *arg.pat {
181 return Some(ident);
182 }
183 }
184 None
185 }
186
187 // `PinnedDrop` is a private trait and should not appear in docs.
188 item.attrs.push(parse_quote!(#[doc(hidden)]));
189
190 let path = &mut item.trait_.as_mut().unwrap().1;
191 *path = parse_quote_spanned! { path.span() =>
192 ::pin_project::__private::PinnedDrop
193 };
194
195 let method =
196 if let ImplItem::Method(method) = &mut item.items[0] { method } else { unreachable!() };
197
198 // `fn drop(mut self: Pin<&mut Self>)` -> `fn __drop_inner<T>(mut __self: Pin<&mut Receiver>)`
199 let drop_inner = {
200 let mut drop_inner = method.clone();
201 let ident = format_ident!("__drop_inner");
202 // Add a dummy `__drop_inner` function to prevent users call outer `__drop_inner`.
203 drop_inner.block.stmts.insert(0, parse_quote!(fn #ident() {}));
204 drop_inner.sig.ident = ident;
205 drop_inner.sig.generics = item.generics.clone();
206 let self_pat = get_arg_pat(&mut drop_inner.sig.inputs[0]).unwrap();
207 prepend_underscore_to_self(&mut self_pat.ident);
208 let self_ty = if let Type::Path(ty) = &*item.self_ty { ty } else { unreachable!() };
209 let mut visitor = ReplaceReceiver(self_ty);
210 visitor.visit_signature_mut(&mut drop_inner.sig);
211 visitor.visit_block_mut(&mut drop_inner.block);
212 drop_inner
213 };
214
215 // `fn drop(mut self: Pin<&mut Self>)` -> `unsafe fn drop(self: Pin<&mut Self>)`
216 method.sig.unsafety = Some(<Token![unsafe]>::default());
217 let self_pat = get_arg_pat(&mut method.sig.inputs[0]).unwrap();
218 self_pat.mutability = None;
219 let self_token = &self_pat.ident;
220
221 method.block.stmts = parse_quote! {
222 #[allow(clippy::needless_pass_by_value)] // This lint does not warn the receiver.
223 #drop_inner
224 __drop_inner(#self_token);
225 };
226 }
227