diff --git a/components/style_derive/animate.rs b/components/style_derive/animate.rs index 94d14336087..c95dcdc0e94 100644 --- a/components/style_derive/animate.rs +++ b/components/style_derive/animate.rs @@ -28,9 +28,7 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens { let mut computations = quote!(); let iter = result_info.iter().zip(this_info.iter().zip(&other_info)); computations.append_all(iter.map(|(result, (this, other))| { - where_clause.predicates.push( - cg::where_predicate(this.field.ty.clone(), trait_path), - ); + where_clause.add_trait_bound(this.field.ty.clone()); quote! { let #result = ::values::animated::Animate::animate(#this, #other, procedure)?; } diff --git a/components/style_derive/cg.rs b/components/style_derive/cg.rs index a16fad1bb56..ee826dd77ea 100644 --- a/components/style_derive/cg.rs +++ b/components/style_derive/cg.rs @@ -3,16 +3,39 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use darling::FromVariant; -use quote::Tokens; +use quote::{ToTokens, Tokens}; use std::borrow::Cow; +use std::collections::HashSet; use std::iter; -use syn::{AngleBracketedParameterData, Body, DeriveInput, Ident, ImplGenerics}; -use syn::{Path, PathParameters, PathSegment, PolyTraitRef, QSelf}; -use syn::{TraitBoundModifier, Ty, TyGenerics, TyParam, TyParamBound}; -use syn::{Variant, WhereBoundPredicate, WhereClause, WherePredicate}; +use syn::{self, AngleBracketedParameterData, Body, DeriveInput, Ident}; +use syn::{ImplGenerics, Path, PathParameters, PathSegment, PolyTraitRef}; +use syn::{QSelf, TraitBoundModifier, Ty, TyGenerics, TyParam, TyParamBound}; +use syn::{Variant, WhereBoundPredicate, WherePredicate}; use syn::visit::{self, Visitor}; use synstructure::{self, BindOpts, BindStyle, BindingInfo}; +pub struct WhereClause<'input, 'path> { + clause: syn::WhereClause, + params: &'input [TyParam], + trait_path: &'path [&'path str], + bounded_types: HashSet, +} + +impl<'input, 'path> ToTokens for WhereClause<'input, 'path> { + fn to_tokens(&self, tokens: &mut Tokens) { + self.clause.to_tokens(tokens); + } +} + +impl<'input, 'path> WhereClause<'input, 'path> { + pub fn add_trait_bound(&mut self, ty: Ty) { + if is_parameterized(&ty, self.params) && !self.bounded_types.contains(&ty) { + self.bounded_types.insert(ty.clone()); + self.clause.predicates.push(where_predicate(ty, self.trait_path)); + } + } +} + pub fn fmap_match( input: &DeriveInput, bind_style: BindStyle, @@ -35,11 +58,11 @@ where }) } -pub fn fmap_trait_parts<'a>( - input: &'a DeriveInput, - trait_path: &[&str], +pub fn fmap_trait_parts<'input, 'path>( + input: &'input DeriveInput, + trait_path: &'path [&'path str], trait_output: &str, -) -> (ImplGenerics<'a>, TyGenerics<'a>, WhereClause, Path) { +) -> (ImplGenerics<'input>, TyGenerics<'input>, WhereClause<'input, 'path>, Path) { let (impl_generics, ty_generics, where_clause) = trait_parts(input, trait_path); let output_ty = PathSegment { ident: input.ident.clone(), @@ -115,18 +138,17 @@ pub fn ref_pattern<'a>( ) } -pub fn trait_parts<'a>( - input: &'a DeriveInput, - trait_path: &[&str], -) -> (ImplGenerics<'a>, TyGenerics<'a>, WhereClause) { +pub fn trait_parts<'input, 'path>( + input: &'input DeriveInput, + trait_path: &'path [&'path str], +) -> (ImplGenerics<'input>, TyGenerics<'input>, WhereClause<'input, 'path>) { let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); - let mut where_clause = where_clause.clone(); - for param in &input.generics.ty_params { - where_clause.predicates.push(where_predicate( - Ty::Path(None, param.ident.clone().into()), - trait_path, - )); - } + let where_clause = WhereClause { + clause: where_clause.clone(), + params: &input.generics.ty_params, + trait_path, + bounded_types: HashSet::new() + }; (impl_generics, ty_generics, where_clause) } diff --git a/components/style_derive/compute_squared_distance.rs b/components/style_derive/compute_squared_distance.rs index 860e568ea88..f3b51f7dc04 100644 --- a/components/style_derive/compute_squared_distance.rs +++ b/components/style_derive/compute_squared_distance.rs @@ -30,9 +30,7 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens { } else { let mut sum = quote!(); sum.append_separated(this_info.iter().zip(&other_info).map(|(this, other)| { - where_clause.predicates.push( - cg::where_predicate(this.field.ty.clone(), trait_path), - ); + where_clause.add_trait_bound(this.field.ty.clone()); quote! { ::values::distance::ComputeSquaredDistance::compute_squared_distance(#this, #other)? } diff --git a/components/style_derive/has_viewport_percentage.rs b/components/style_derive/has_viewport_percentage.rs index 5f26d988b8a..07a1eb5b7bb 100644 --- a/components/style_derive/has_viewport_percentage.rs +++ b/components/style_derive/has_viewport_percentage.rs @@ -19,11 +19,10 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens { None => return Some(quote!(false)), Some(pair) => pair, }; + where_clause.add_trait_bound(first.field.ty.clone()); let mut expr = quote!(::style_traits::HasViewportPercentage::has_viewport_percentage(#first)); for binding in rest { - where_clause.predicates.push( - cg::where_predicate(binding.field.ty.clone(), trait_path), - ); + where_clause.add_trait_bound(binding.field.ty.clone()); expr = quote!(#expr || ::style_traits::HasViewportPercentage::has_viewport_percentage(#binding)); } Some(expr) diff --git a/components/style_derive/to_animated_value.rs b/components/style_derive/to_animated_value.rs index 5a66ddf65a4..3347116384d 100644 --- a/components/style_derive/to_animated_value.rs +++ b/components/style_derive/to_animated_value.rs @@ -9,18 +9,20 @@ use synstructure::BindStyle; pub fn derive(input: syn::DeriveInput) -> quote::Tokens { let name = &input.ident; - let (impl_generics, ty_generics, where_clause, animated_value_type) = - cg::fmap_trait_parts( - &input, - &["values", "animated", "ToAnimatedValue"], - "AnimatedValue", + let trait_path = &["values", "animated", "ToAnimatedValue"]; + let (impl_generics, ty_generics, mut where_clause, animated_value_type) = + cg::fmap_trait_parts(&input, trait_path, "AnimatedValue"); + for param in &input.generics.ty_params { + where_clause.add_trait_bound( + syn::Ty::Path(None, param.ident.clone().into()), ); + } - let to_body = cg::fmap_match(&input, BindStyle::Move, |field| { - quote!(::values::animated::ToAnimatedValue::to_animated_value(#field)) + let to_body = cg::fmap_match(&input, BindStyle::Move, |binding| { + quote!(::values::animated::ToAnimatedValue::to_animated_value(#binding)) }); - let from_body = cg::fmap_match(&input, BindStyle::Move, |field| { - quote!(::values::animated::ToAnimatedValue::from_animated_value(#field)) + let from_body = cg::fmap_match(&input, BindStyle::Move, |binding| { + quote!(::values::animated::ToAnimatedValue::from_animated_value(#binding)) }); quote! { diff --git a/components/style_derive/to_animated_zero.rs b/components/style_derive/to_animated_zero.rs index d9e67a8a9fe..94b68c7ce9a 100644 --- a/components/style_derive/to_animated_zero.rs +++ b/components/style_derive/to_animated_zero.rs @@ -9,13 +9,13 @@ use synstructure::BindStyle; pub fn derive(input: syn::DeriveInput) -> quote::Tokens { let name = &input.ident; - let (impl_generics, ty_generics, where_clause) = cg::trait_parts( - &input, - &["values", "animated", "ToAnimatedZero"], - ); + let trait_path = &["values", "animated", "ToAnimatedZero"]; + let (impl_generics, ty_generics, mut where_clause) = + cg::trait_parts(&input, trait_path); - let to_body = cg::fmap_match(&input, BindStyle::Ref, |field| { - quote! { ::values::animated::ToAnimatedZero::to_animated_zero(#field)? } + let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| { + where_clause.add_trait_bound(binding.field.ty.clone()); + quote! { ::values::animated::ToAnimatedZero::to_animated_zero(#binding)? } }); quote! { diff --git a/components/style_derive/to_computed_value.rs b/components/style_derive/to_computed_value.rs index 967f3320a37..ca84265ceeb 100644 --- a/components/style_derive/to_computed_value.rs +++ b/components/style_derive/to_computed_value.rs @@ -9,18 +9,20 @@ use synstructure::BindStyle; pub fn derive(input: syn::DeriveInput) -> quote::Tokens { let name = &input.ident; - let (impl_generics, ty_generics, where_clause, computed_value_type) = - cg::fmap_trait_parts( - &input, - &["values", "computed", "ToComputedValue"], - "ComputedValue", + let trait_path = &["values", "computed", "ToComputedValue"]; + let (impl_generics, ty_generics, mut where_clause, computed_value_type) = + cg::fmap_trait_parts(&input, trait_path, "ComputedValue"); + for param in &input.generics.ty_params { + where_clause.add_trait_bound( + syn::Ty::Path(None, param.ident.clone().into()), ); + } - let to_body = cg::fmap_match(&input, BindStyle::Ref, |field| { - quote!(::values::computed::ToComputedValue::to_computed_value(#field, context)) + let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| { + quote!(::values::computed::ToComputedValue::to_computed_value(#binding, context)) }); - let from_body = cg::fmap_match(&input, BindStyle::Ref, |field| { - quote!(::values::computed::ToComputedValue::from_computed_value(#field)) + let from_body = cg::fmap_match(&input, BindStyle::Ref, |binding| { + quote!(::values::computed::ToComputedValue::from_computed_value(#binding)) }); quote! { diff --git a/components/style_derive/to_css.rs b/components/style_derive/to_css.rs index 444c42e37da..6dd7567a5fb 100644 --- a/components/style_derive/to_css.rs +++ b/components/style_derive/to_css.rs @@ -21,11 +21,7 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens { let mut expr = if !bindings.is_empty() { let mut expr = quote! {}; for binding in bindings { - if cg::is_parameterized(&binding.field.ty, &input.generics.ty_params) { - where_clause.predicates.push( - cg::where_predicate(binding.field.ty.clone(), trait_path), - ); - } + where_clause.add_trait_bound(binding.field.ty.clone()); expr = quote! { #expr writer.item(#binding)?;