Skip to content

Commit

Permalink
Merge #214
Browse files Browse the repository at this point in the history
214: Various cleanup r=taiki-e a=taiki-e

Separated from #202 as unrelated changes.

Co-authored-by: Taiki Endo <[email protected]>
  • Loading branch information
bors[bot] and taiki-e authored May 8, 2020
2 parents 7411440 + cf3d4f0 commit 440e8cd
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 150 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ jobs:
- name: cargo clippy
if: matrix.component == 'clippy'
run: |
cargo clippy --all --all-features
cargo clippy -p pin-project --all-features --test clippy
cargo clippy --all --all-features --all-targets
- name: cargo fmt -- --check
if: matrix.component == 'rustfmt'
run: |
Expand Down
1 change: 1 addition & 0 deletions examples/enum-default-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// ```

#![allow(dead_code, unused_imports, unused_parens)]
#![allow(clippy::no_effect, clippy::just_underscores_and_digits)]

use pin_project::pin_project;

Expand Down
1 change: 1 addition & 0 deletions examples/pinned_drop-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// ```

#![allow(dead_code, unused_imports, unused_parens)]
#![allow(clippy::no_effect)]

use pin_project::{pin_project, pinned_drop};
use std::pin::Pin;
Expand Down
1 change: 1 addition & 0 deletions examples/struct-default-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// ```

#![allow(dead_code, unused_imports, unused_parens)]
#![allow(clippy::no_effect)]

use pin_project::pin_project;

Expand Down
1 change: 1 addition & 0 deletions examples/unsafe_unpin-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// ```

#![allow(dead_code, unused_imports, unused_parens)]
#![allow(clippy::no_effect)]

use pin_project::{pin_project, UnsafeUnpin};

Expand Down
128 changes: 48 additions & 80 deletions pin-project-internal/src/pin_project/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use super::PIN;
use crate::utils::{
determine_lifetime_name, determine_visibility, insert_lifetime_and_bound, proj_ident,
Immutable, Mutable, Owned, ParseBufferExt, ReplaceReceiver, SliceExt, Variants,
DEFAULT_LIFETIME_NAME,
};

pub(super) fn parse_derive(input: TokenStream) -> Result<TokenStream> {
Expand Down Expand Up @@ -242,19 +241,19 @@ struct OriginalType<'a> {
}

struct ProjectedType {
/// Visibility of the projected type.
/// Visibility of the projected types.
vis: Visibility,
/// Name of the projected type returned by `project` method.
mut_ident: Ident,
/// Name of the projected type returned by `project_ref` method.
ref_ident: Ident,
/// Name of the projected type returned by `project_replace` method.
own_ident: Ident,
/// Lifetime on the generated projected type.
/// Lifetime on the generated projected types.
lifetime: Lifetime,
/// Generics of the projected type.
/// Generics of the projected types.
generics: Generics,
/// `where` clause of the projected type. This has an additional
/// `where` clause of the projected types. This has an additional
/// bound generated by `insert_lifetime_and_bound`
where_clause: WhereClause,
}
Expand All @@ -276,11 +275,13 @@ struct ProjectedFields {
proj_ref_fields: TokenStream,
proj_own_fields: TokenStream,
proj_move: TokenStream,
proj_drop: TokenStream,
proj_drop: Vec<Ident>,
}

struct Context<'a> {
/// The original type.
orig: OriginalType<'a>,
/// The projected types.
proj: ProjectedType,
/// Types of the pinned fields.
pinned_fields: Vec<Type>,
Expand All @@ -306,7 +307,7 @@ impl<'a> Context<'a> {
let mut visitor = ReplaceReceiver::new(&self_ty);
visitor.visit_where_clause_mut(generics.make_where_clause());

let mut lifetime_name = String::from(DEFAULT_LIFETIME_NAME);
let mut lifetime_name = String::from("'pin");
determine_lifetime_name(&mut lifetime_name, &generics.params);
let lifetime = Lifetime::new(&lifetime_name, Span::call_site());

Expand Down Expand Up @@ -425,7 +426,9 @@ impl<'a> Context<'a> {
// Due to a compiler bug (https://github.com/rust-lang/rust/issues/47949)
// this must be in its own scope, or else `__result` will not be dropped
// if any of the destructors panic.
{ #proj_drop }
{
#( let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard(#proj_drop); )*
}

// Finally, return the result
__result
Expand Down Expand Up @@ -562,7 +565,9 @@ impl<'a> Context<'a> {
// Due to a compiler bug (https://github.com/rust-lang/rust/issues/47949)
// this must be in its own scope, or else `__result` will not be dropped
// if any of the destructors panic.
{ #proj_drop }
{
#( let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard(#proj_drop); )*
}

// Finally, return the result
__result
Expand Down Expand Up @@ -595,43 +600,28 @@ impl<'a> Context<'a> {
for Field { attrs, vis, ident, ty, .. } in fields {
if attrs.position_exact(PIN)?.is_some() {
self.pinned_fields.push(ty.clone());
proj_drop.push(ident.as_ref().cloned().unwrap());

let lifetime = &self.proj.lifetime;
proj_fields.push(quote! {
#vis #ident: ::pin_project::__reexport::pin::Pin<&#lifetime mut (#ty)>
});
proj_ref_fields.push(quote! {
#vis #ident: ::pin_project::__reexport::pin::Pin<&#lifetime (#ty)>
});
proj_own_fields.push(quote! {
#vis #ident: ::pin_project::__reexport::marker::PhantomData<#ty>
});
proj_body.push(quote! {
#ident: ::pin_project::__reexport::pin::Pin::new_unchecked(#ident)
});
proj_move.push(quote! {
#ident: ::pin_project::__reexport::marker::PhantomData
});
proj_drop.push(quote! {
let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard(#ident);
});
proj_fields.push(
quote!(#vis #ident: ::pin_project::__reexport::pin::Pin<&#lifetime mut (#ty)>),
);
proj_ref_fields.push(
quote!(#vis #ident: ::pin_project::__reexport::pin::Pin<&#lifetime (#ty)>),
);
proj_own_fields
.push(quote!(#vis #ident: ::pin_project::__reexport::marker::PhantomData<#ty>));
proj_body.push(
quote!(#ident: ::pin_project::__reexport::pin::Pin::new_unchecked(#ident)),
);
proj_move.push(quote!(#ident: ::pin_project::__reexport::marker::PhantomData));
} else {
let lifetime = &self.proj.lifetime;
proj_fields.push(quote! {
#vis #ident: &#lifetime mut (#ty)
});
proj_ref_fields.push(quote! {
#vis #ident: &#lifetime (#ty)
});
proj_own_fields.push(quote! {
#vis #ident: #ty
});
proj_body.push(quote! {
#ident
});
proj_move.push(quote! {
#ident: ::pin_project::__reexport::ptr::read(#ident)
});
proj_fields.push(quote!(#vis #ident: &#lifetime mut (#ty)));
proj_ref_fields.push(quote!(#vis #ident: &#lifetime (#ty)));
proj_own_fields.push(quote!(#vis #ident: #ty));
proj_body.push(quote!(#ident));
proj_move.push(quote!(#ident: ::pin_project::__reexport::ptr::read(#ident)));
}
proj_pat.push(ident);
}
Expand All @@ -642,7 +632,6 @@ impl<'a> Context<'a> {
let proj_ref_fields = quote!({ #(#proj_ref_fields),* });
let proj_own_fields = quote!({ #(#proj_own_fields),* });
let proj_move = quote!({ #(#proj_move),* });
let proj_drop = quote!(#(#proj_drop)*);

Ok(ProjectedFields {
proj_pat,
Expand Down Expand Up @@ -671,43 +660,24 @@ impl<'a> Context<'a> {
let id = format_ident!("_{}", i);
if attrs.position_exact(PIN)?.is_some() {
self.pinned_fields.push(ty.clone());
proj_drop.push(id.clone());

let lifetime = &self.proj.lifetime;
proj_fields.push(quote! {
#vis ::pin_project::__reexport::pin::Pin<&#lifetime mut (#ty)>
});
proj_ref_fields.push(quote! {
#vis ::pin_project::__reexport::pin::Pin<&#lifetime (#ty)>
});
proj_own_fields.push(quote! {
#vis ::pin_project::__reexport::marker::PhantomData<#ty>
});
proj_body.push(quote! {
::pin_project::__reexport::pin::Pin::new_unchecked(#id)
});
proj_move.push(quote! {
::pin_project::__reexport::marker::PhantomData
});
proj_drop.push(quote! {
let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard(#id);
});
proj_fields
.push(quote!(#vis ::pin_project::__reexport::pin::Pin<&#lifetime mut (#ty)>));
proj_ref_fields
.push(quote!(#vis ::pin_project::__reexport::pin::Pin<&#lifetime (#ty)>));
proj_own_fields
.push(quote!(#vis ::pin_project::__reexport::marker::PhantomData<#ty>));
proj_body.push(quote!(::pin_project::__reexport::pin::Pin::new_unchecked(#id)));
proj_move.push(quote!(::pin_project::__reexport::marker::PhantomData));
} else {
let lifetime = &self.proj.lifetime;
proj_fields.push(quote! {
#vis &#lifetime mut (#ty)
});
proj_ref_fields.push(quote! {
#vis &#lifetime (#ty)
});
proj_own_fields.push(quote! {
#vis #ty
});
proj_body.push(quote! {
#id
});
proj_move.push(quote! {
::pin_project::__reexport::ptr::read(#id)
});
proj_fields.push(quote!(#vis &#lifetime mut (#ty)));
proj_ref_fields.push(quote!(#vis &#lifetime (#ty)));
proj_own_fields.push(quote!(#vis #ty));
proj_body.push(quote!(#id));
proj_move.push(quote!(::pin_project::__reexport::ptr::read(#id)));
}
proj_pat.push(id);
}
Expand All @@ -718,7 +688,6 @@ impl<'a> Context<'a> {
let proj_ref_fields = quote!((#(#proj_ref_fields),*));
let proj_own_fields = quote!((#(#proj_own_fields),*));
let proj_move = quote!((#(#proj_move),*));
let proj_drop = quote!(#(#proj_drop)*);

Ok(ProjectedFields {
proj_pat,
Expand All @@ -740,10 +709,9 @@ impl<'a> Context<'a> {

proj_generics.make_where_clause().predicates.push(
// Make the error message highlight `UnsafeUnpin` argument.
syn::parse2(quote_spanned! { unsafe_unpin =>
parse_quote_spanned! { unsafe_unpin =>
::pin_project::__private::Wrapper<#lifetime, Self>: ::pin_project::UnsafeUnpin
})
.unwrap(),
},
);

let (impl_generics, _, where_clause) = proj_generics.split_for_impl();
Expand Down
57 changes: 27 additions & 30 deletions pin-project-internal/src/pinned_drop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use quote::{quote, ToTokens};
use syn::{spanned::Spanned, visit_mut::VisitMut, *};

use crate::utils::{parse_as_empty, prepend_underscore_to_self, ReplaceReceiver, SliceExt};
Expand Down Expand Up @@ -50,12 +50,10 @@ fn parse_method(method: &ImplItemMethod) -> Result<()> {
}
}

if method.sig.inputs.len() != 1 {
if method.sig.inputs.is_empty() {
return Err(Error::new(method.sig.paren_token.span, INVALID_ARGUMENT));
} else {
return Err(error!(&method.sig.inputs, INVALID_ARGUMENT));
}
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)),
}

if let Some(FnArg::Typed(pat)) = &method.sig.receiver() {
Expand Down Expand Up @@ -101,10 +99,9 @@ fn parse(item: &mut ItemImpl) -> Result<()> {

if let Some((_, path, _)) = &mut item.trait_ {
if path.is_ident("PinnedDrop") {
*path = syn::parse2(quote_spanned! { path.span() =>
*path = parse_quote_spanned! { path.span() =>
::pin_project::__private::PinnedDrop
})
.unwrap();
};
} else {
return Err(error!(path, INVALID_ITEM));
}
Expand All @@ -119,27 +116,27 @@ fn parse(item: &mut ItemImpl) -> Result<()> {
return Err(error!(item, "not all trait items implemented, missing: `drop`"));
}

item.items.iter().enumerate().try_for_each(|(i, item)| match item {
ImplItem::Const(item) => {
Err(error!(item, "const `{}` is not a member of trait `PinnedDrop`", item.ident))
}
ImplItem::Type(item) => {
Err(error!(item, "type `{}` is not a member of trait `PinnedDrop`", item.ident))
}
ImplItem::Method(method) => {
parse_method(method)?;
if i == 0 {
Ok(())
} else {
Err(error!(method, "duplicate definitions with name `drop`"))
item.items
.iter()
.enumerate()
.try_for_each(|(i, item)| match item {
ImplItem::Const(item) => {
Err(error!(item, "const `{}` is not a member of trait `PinnedDrop`", item.ident))
}
}
_ => unreachable!("unexpected ImplItem"),
})?;

expand_item(item);

Ok(())
ImplItem::Type(item) => {
Err(error!(item, "type `{}` is not a member of trait `PinnedDrop`", item.ident))
}
ImplItem::Method(method) => {
parse_method(method)?;
if i == 0 {
Ok(())
} else {
Err(error!(method, "duplicate definitions with name `drop`"))
}
}
_ => unreachable!("unexpected ImplItem"),
})
.map(|()| expand_item(item))
}

// from:
Expand Down
4 changes: 2 additions & 2 deletions pin-project-internal/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syn::{

use crate::utils::{
determine_lifetime_name, insert_lifetime, parse_as_empty, proj_ident, Immutable, Mutability,
Mutable, Owned, VecExt, DEFAULT_LIFETIME_NAME,
Mutable, Owned, VecExt,
};

pub(crate) fn attribute(args: &TokenStream, input: Stmt, mutability: Mutability) -> TokenStream {
Expand Down Expand Up @@ -169,7 +169,7 @@ fn replace_item_impl(item: &mut ItemImpl, mutability: Mutability) {

replace_ident(ident, mutability);

let mut lifetime_name = String::from(DEFAULT_LIFETIME_NAME);
let mut lifetime_name = String::from("'pin");
determine_lifetime_name(&mut lifetime_name, &item.generics.params);
item.items
.iter_mut()
Expand Down
Loading

0 comments on commit 440e8cd

Please sign in to comment.