Skip to content

Commit

Permalink
Specify derivable trait bounds in where clause.
Browse files Browse the repository at this point in the history
  • Loading branch information
adetaylor committed Feb 5, 2021
1 parent 8bb23ad commit c829066
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 43 deletions.
52 changes: 18 additions & 34 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,17 +740,9 @@ impl CodeGenerator for Type {
let traitname = ctx.inner_type_trait_ident(field_name);
if result.inner_type_traits_generated.insert(traitname.clone())
{
let mut type_definition = quote! { #shortname };
// Buglet here.
let derivable_traits = derives_of_item(item, ctx);
append_associated_type_constraints(
ctx,
&derivable_traits,
&mut type_definition,
);
result.push(quote! {
pub trait #traitname {
type #type_definition;
type #shortname;
}
});
}
Expand Down Expand Up @@ -2007,14 +1999,30 @@ impl CodeGenerator for CompInfo {

let mut where_constraints_ts = quote! {};
if !where_constraints.is_empty() {
let derivable_traits: Vec<&'static str> = derivable_traits.into();
let derivable_trait_bounds: Vec<_> = derivable_traits
.into_iter()
.map(|id| match id {
"Debug" => quote! { std::fmt::Debug },
_ => {
let id = ctx.rust_ident(id);
quote! { #id }
}
})
.collect();

for (i, (k, traits)) in where_constraints.into_iter().enumerate() {
let prefix = if i == 0 {
quote! { where }
} else {
quote! { , }
};
where_constraints_ts.extend(quote! { #prefix #k });
for (j, v) in traits.into_iter().enumerate() {
let all_traits = traits
.into_iter()
.map(|t| quote! { #t })
.chain(derivable_trait_bounds.clone().into_iter());
for (j, v) in all_traits.enumerate() {
let sep = if j == 0 {
quote! {:}
} else {
Expand Down Expand Up @@ -2287,30 +2295,6 @@ impl CodeGenerator for CompInfo {
}
}

fn append_associated_type_constraints(
ctx: &BindgenContext,
derivable_traits: &DerivableTraits,
ts: &mut proc_macro2::TokenStream,
) {
let trait_bounds: Vec<&'static str> = derivable_traits.clone().into();
let mut done_first = false;
for id in trait_bounds.into_iter() {
let id = match id {
"Debug" => quote! { std::fmt::Debug },
_ => {
let id = ctx.rust_ident(id);
quote! { #id }
}
};
ts.append_all(if done_first {
quote! { + #id }
} else {
quote! { : #id }
});
done_first = true;
}
}

trait MethodCodegen {
fn codegen_method<'a>(
&self,
Expand Down
8 changes: 6 additions & 2 deletions tests/expectations/tests/inner_type_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ fn bindgen_test_layout_InnerType() {
#[derive(Debug, Default, Copy, Clone)]
pub struct Container<ContainedType>
where
ContainedType: __bindgen_has_inner_type_related_type,
ContainedType: __bindgen_has_inner_type_related_type
+ std::fmt::Debug
+ Default
+ Copy
+ Clone,
{
pub contents_: Container_content_ty<ContainedType>,
pub _phantom_0:
Expand Down Expand Up @@ -97,7 +101,7 @@ fn bindgen_test_layout_LaterContainingType() {
);
}
pub trait __bindgen_has_inner_type_related_type {
type related_type: std::fmt::Debug + Default + Copy + Clone;
type related_type;
}
#[test]
fn __bindgen_test_layout_Container_open0_InnerType_close0_instantiation() {
Expand Down
8 changes: 6 additions & 2 deletions tests/expectations/tests/inner_type_simpler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ fn bindgen_test_layout_InnerType() {
#[derive(Debug, Default, Copy, Clone)]
pub struct Container<ContainedType>
where
ContainedType: __bindgen_has_inner_type_related_type,
ContainedType: __bindgen_has_inner_type_related_type
+ std::fmt::Debug
+ Default
+ Copy
+ Clone,
{
pub contents_:
<ContainedType as __bindgen_has_inner_type_related_type>::related_type,
Expand All @@ -52,5 +56,5 @@ where
}
pub type Concrete = Container<InnerType>;
pub trait __bindgen_has_inner_type_related_type {
type related_type: std::fmt::Debug + Default + Copy + Clone;
type related_type;
}
8 changes: 6 additions & 2 deletions tests/expectations/tests/inner_type_two.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ fn bindgen_test_layout_InnerTypeB() {
#[derive(Debug, Default, Copy, Clone)]
pub struct Container<ContainedType>
where
ContainedType: __bindgen_has_inner_type_related_type,
ContainedType: __bindgen_has_inner_type_related_type
+ std::fmt::Debug
+ Default
+ Copy
+ Clone,
{
pub contents_: Container_content_ty<ContainedType>,
pub _phantom_0:
Expand Down Expand Up @@ -145,7 +149,7 @@ fn bindgen_test_layout_LaterContainingType() {
);
}
pub trait __bindgen_has_inner_type_related_type {
type related_type: std::fmt::Debug + Default + Copy + Clone;
type related_type;
}
#[test]
fn __bindgen_test_layout_Container_open0_InnerTypeA_close0_instantiation() {
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/issue-544-stylo-creduce-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[derive(Debug, Copy, Clone)]
pub struct Foo<T>
where
T: __bindgen_has_inner_type_Associated,
T: __bindgen_has_inner_type_Associated + std::fmt::Debug + Copy + Clone,
{
pub member: Foo_SecondAlias,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
Expand All @@ -23,5 +23,5 @@ impl<T> Default for Foo<T> {
}
}
pub trait __bindgen_has_inner_type_Associated {
type Associated: std::fmt::Debug + Default + Copy + Clone;
type Associated;
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/nsBaseHashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ impl Default for nsBaseHashtable {
}
}
pub trait __bindgen_has_inner_type_KeyType {
type KeyType: std::fmt::Debug + Default + Copy + Clone;
type KeyType;
}

0 comments on commit c829066

Please sign in to comment.