From c208c8c8e6eeec6f0e1fc1105271692e586be755 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Mon, 11 May 2020 09:17:40 +0900 Subject: [PATCH] Prevent user call `__drop_inner` --- examples/pinned_drop-expanded.rs | 3 +++ pin-project-internal/src/pinned_drop.rs | 11 +++++++---- .../expand/tests/expand/pinned_drop-enum.expanded.rs | 1 + .../tests/expand/pinned_drop-struct.expanded.rs | 1 + .../tests/expand/pinned_drop-tuple_struct.expanded.rs | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/pinned_drop-expanded.rs b/examples/pinned_drop-expanded.rs index 9bee33bc..583bf111 100644 --- a/examples/pinned_drop-expanded.rs +++ b/examples/pinned_drop-expanded.rs @@ -142,6 +142,9 @@ impl ::pin_project::__private::PinnedDrop for Struct<'_, T> { unsafe fn drop(self: Pin<&mut Self>) { #[allow(clippy::needless_pass_by_value)] fn __drop_inner(__self: Pin<&mut Struct<'_, T>>) { + // Added `__drop_inner` function to prevent user call outer `__drop_inner`. + fn __drop_inner() {} + **__self.project().was_dropped = true; } __drop_inner(self); diff --git a/pin-project-internal/src/pinned_drop.rs b/pin-project-internal/src/pinned_drop.rs index 8a6f18d8..ebe79ffe 100644 --- a/pin-project-internal/src/pinned_drop.rs +++ b/pin-project-internal/src/pinned_drop.rs @@ -53,10 +53,10 @@ fn parse_method(method: &ImplItemMethod) -> Result<()> { match method.sig.inputs.len() { 1 => {} 0 => return Err(Error::new(method.sig.paren_token.span, INVALID_ARGUMENT)), - _ => return Err(error!(&method.sig.inputs, INVALID_ARGUMENT)), + _ => return Err(error!(method.sig.inputs, INVALID_ARGUMENT)), } - if let Some(FnArg::Typed(pat)) = &method.sig.receiver() { + if let Some(FnArg::Typed(pat)) = method.sig.receiver() { // (mut) self: if let Some(path) = get_ty_path(&pat.ty) { let ty = path.segments.last().unwrap(); @@ -149,6 +149,7 @@ fn parse(item: &mut ItemImpl) -> Result<()> { // // unsafe fn drop(self: Pin<&mut Self>) { // fn __drop_inner(__self: Pin<&mut Foo<'_, T>>) { +// fn __drop_inner() {} // // something // } // __drop_inner(self); @@ -160,14 +161,16 @@ fn expand_item(item: &mut ItemImpl) { let mut drop_inner = method.clone(); // `fn drop(mut self: Pin<&mut Self>)` -> `fn __drop_inner(mut __self: Pin<&mut Receiver>)` - drop_inner.sig.ident = Ident::new("__drop_inner", drop_inner.sig.ident.span()); + let ident = Ident::new("__drop_inner", drop_inner.sig.ident.span()); + // Add `__drop_inner` function to prevent user call outer `__drop_inner`. + drop_inner.block.stmts.insert(0, parse_quote!(fn #ident() {})); + drop_inner.sig.ident = ident; drop_inner.sig.generics = item.generics.clone(); if let FnArg::Typed(arg) = &mut drop_inner.sig.inputs[0] { if let Pat::Ident(ident) = &mut *arg.pat { prepend_underscore_to_self(&mut ident.ident); } } - let mut visitor = ReplaceReceiver(&item.self_ty); visitor.visit_signature_mut(&mut drop_inner.sig); visitor.visit_block_mut(&mut drop_inner.block); diff --git a/tests/expand/tests/expand/pinned_drop-enum.expanded.rs b/tests/expand/tests/expand/pinned_drop-enum.expanded.rs index d445f5f0..f29a9dc3 100644 --- a/tests/expand/tests/expand/pinned_drop-enum.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-enum.expanded.rs @@ -105,6 +105,7 @@ impl ::pin_project::__private::PinnedDrop for Enum { unsafe fn drop(self: Pin<&mut Self>) { #[allow(clippy::needless_pass_by_value)] fn __drop_inner(__self: Pin<&mut Enum>) { + fn __drop_inner() {} let _this = __self; } __drop_inner(self); diff --git a/tests/expand/tests/expand/pinned_drop-struct.expanded.rs b/tests/expand/tests/expand/pinned_drop-struct.expanded.rs index b103d095..ec43e13b 100644 --- a/tests/expand/tests/expand/pinned_drop-struct.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-struct.expanded.rs @@ -82,6 +82,7 @@ impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: Pin<&mut Self>) { #[allow(clippy::needless_pass_by_value)] fn __drop_inner(__self: Pin<&mut Struct>) { + fn __drop_inner() {} let _this = __self; } __drop_inner(self); diff --git a/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs b/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs index 1d9dbfb0..1177dd6f 100644 --- a/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs @@ -73,6 +73,7 @@ impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: Pin<&mut Self>) { #[allow(clippy::needless_pass_by_value)] fn __drop_inner(__self: Pin<&mut TupleStruct>) { + fn __drop_inner() {} let _this = __self; } __drop_inner(self);