style: Ensure that derived types are right for optimized-away implementations.

We have this optimization where, for non-generic structs, we generate just a
clone / move as the ToComputedValue / ToResolvedValue implementation.

This moves the optimization a bit further down, and refines it so that we still
generate all the relevant where clauses that make it sound, that is, that all
the ToComputedValue implementations of the fields return the same type.

Otherwise this wouldn't be sound and the type would need to become generic.

We add an escape hatch (no_field_bound) for fields that need to be cloned but
which don't implement the trait. This is right now only for the RefPtr<> in the
shared font-family list, and a piece of code in PaintWorklet which looks kinda
fishy, and probably should be fixed (but we don't ship it in Firefox and there's
a pre-existing FIXME for servo, so I punted on it for now).

The other thing this patch does is adding a bunch of ToComputedValue /
ToResolvedValue implementations that are trivial and were missing.

Differential Revision: https://phabricator.services.mozilla.com/D67913
This commit is contained in:
Emilio Cobos Álvarez 2020-03-26 13:04:20 +00:00
parent 0514059618
commit 7d438cd816
18 changed files with 295 additions and 146 deletions

View file

@ -12,10 +12,8 @@ pub fn derive(input: DeriveInput) -> TokenStream {
let trait_impl = |from_body, to_body| {
quote! {
#[inline]
fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
match resolved {
#from_body
}
fn from_resolved_value(from: Self::ResolvedValue) -> Self {
#from_body
}
#[inline]
@ -23,42 +21,26 @@ pub fn derive(input: DeriveInput) -> TokenStream {
self,
context: &crate::values::resolved::Context,
) -> Self::ResolvedValue {
match self {
#to_body
}
#to_body
}
}
};
let non_generic_implementation = || {
Some(quote! {
type ResolvedValue = Self;
#[inline]
fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
resolved
}
#[inline]
fn to_resolved_value(
self,
context: &crate::values::resolved::Context,
) -> Self {
self
}
})
};
to_computed_value::derive_to_value(
input,
parse_quote!(crate::values::resolved::ToResolvedValue),
parse_quote!(ResolvedValue),
BindStyle::Move,
|binding| cg::parse_field_attrs::<ResolvedValueAttrs>(&binding.ast()).field_bound,
|binding| {
let attrs = cg::parse_field_attrs::<ResolvedValueAttrs>(&binding.ast());
to_computed_value::ToValueAttrs {
field_bound: attrs.field_bound,
no_field_bound: attrs.no_field_bound,
}
},
|binding| quote!(crate::values::resolved::ToResolvedValue::from_resolved_value(#binding)),
|binding| quote!(crate::values::resolved::ToResolvedValue::to_resolved_value(#binding, context)),
trait_impl,
non_generic_implementation,
)
}
@ -66,4 +48,5 @@ pub fn derive(input: DeriveInput) -> TokenStream {
#[derive(Default, FromField)]
struct ResolvedValueAttrs {
field_bound: bool,
no_field_bound: bool,
}