Skip to content

Commit

Permalink
Merge #186
Browse files Browse the repository at this point in the history
186: Properly handle `where` clauses on tuple structs r=taiki-e a=Aaron1011

Previously, we were generating the `where` clause in the wrong position
for the generated projection struct, leading to a compilation error.

Co-authored-by: Aaron Hill <aa1ronham@gmail.com>
  • Loading branch information
bors[bot] and Aaron1011 committed Apr 11, 2020
2 parents e87ff66 + 9bf1917 commit 389ce9d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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),
}

0 comments on commit 389ce9d

Please sign in to comment.