Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle where clauses on tuple structs #186

Merged
merged 1 commit into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pin-project-internal/src/pin_project/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,24 @@ impl Context {
let proj_generics = &self.proj.generics;
let where_clause = self.orig.generics.split_for_impl().2;

// For tuple structs, we need to generate `(T1, T2) where Foo: Bar`
// For non-tuple structs, we need to generate `where Foo: Bar { field1: T }`
let (where_clause_fields, where_clause_ref_fields) = match fields {
Fields::Named(_) => {
(quote!(#where_clause #proj_fields), quote!(#where_clause #proj_ref_fields))
}
Fields::Unnamed(_) => {
(quote!(#proj_fields #where_clause), quote!(#proj_ref_fields #where_clause))
}
Fields::Unit => unreachable!(),
};

let mut proj_items = quote! {
#[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`.
#[allow(dead_code)] // This lint warns unused fields/variants.
#vis struct #proj_ident #proj_generics #where_clause #proj_fields
#vis struct #proj_ident #proj_generics #where_clause_fields
#[allow(dead_code)] // This lint warns unused fields/variants.
#vis struct #proj_ref_ident #proj_generics #where_clause #proj_ref_fields
#vis struct #proj_ref_ident #proj_generics #where_clause_ref_fields
};

let proj_body = quote! {
Expand Down
21 changes: 21 additions & 0 deletions tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,24 @@ mod project_use_2 {
fn project_use(self) {}
}
}

#[pin_project]
struct StructWhereClause<T>
where
T: Copy,
{
field: T,
}

#[pin_project]
struct TupleStructWhereClause<T>(T)
where
T: Copy;

#[pin_project]
enum EnumWhereClause<T>
where
T: Copy,
{
Variant(T),
}