Skip to content

Commit

Permalink
Properly handle where clauses on tuple structs
Browse files Browse the repository at this point in the history
Previously, we were generating the `where` clause in the wrong position
for the generated projection struct, leading to a compilation error.
  • Loading branch information
Aaron1011 committed Apr 11, 2020
1 parent 7081486 commit fa09b09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 10 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,20 @@ 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),
}

0 comments on commit fa09b09

Please sign in to comment.