Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ef6c9b8

Browse files
committedFeb 11, 2020
style: Make rust generate better code for derive(Animate) and derive(ComputeSquaredDistance).
See rust-lang/rust#68867. This technically changes the semantics of #[animate(fallback)] and such when combined with #[animate(error)]. But no such combination exists and the new semantics are perfectly reasonable as well, IMHO. Differential Revision: https://phabricator.services.mozilla.com/D61761
1 parent 88d3e0c commit ef6c9b8

File tree

4 files changed

+105
-89
lines changed

4 files changed

+105
-89
lines changed
 

‎components/style/values/animated/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn animate_multiplicative_factor(
107107
/// be equal or an error is returned.
108108
///
109109
/// If a variant is annotated with `#[animation(error)]`, the corresponding
110-
/// `match` arm is not generated.
110+
/// `match` arm returns an error.
111111
///
112112
/// If the two values are not similar, an error is returned unless a fallback
113113
/// function has been specified through `#[animate(fallback)]`.

‎components/style/values/distance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::ops::Add;
1717
/// on each fields of the values.
1818
///
1919
/// If a variant is annotated with `#[animation(error)]`, the corresponding
20-
/// `match` arm is not generated.
20+
/// `match` arm returns an error.
2121
///
2222
/// If the two values are not similar, an error is returned unless a fallback
2323
/// function has been specified through `#[distance(fallback)]`.

‎components/style_derive/animate.rs

+29-26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use synstructure::{Structure, VariantInfo};
1111

1212
pub fn derive(mut input: DeriveInput) -> TokenStream {
1313
let animation_input_attrs = cg::parse_input_attrs::<AnimationInputAttrs>(&input);
14+
let input_attrs = cg::parse_input_attrs::<AnimateInputAttrs>(&input);
15+
1416
let no_bound = animation_input_attrs.no_bound.unwrap_or_default();
1517
let mut where_clause = input.generics.where_clause.take();
1618
for param in input.generics.type_params() {
@@ -21,39 +23,32 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
2123
);
2224
}
2325
}
24-
let (mut match_body, append_error_clause) = {
26+
let (mut match_body, needs_catchall_branch) = {
2527
let s = Structure::new(&input);
26-
let mut append_error_clause = s.variants().len() > 1;
27-
28+
let needs_catchall_branch = s.variants().len() > 1;
2829
let match_body = s.variants().iter().fold(quote!(), |body, variant| {
29-
let arm = match derive_variant_arm(variant, &mut where_clause) {
30-
Ok(arm) => arm,
31-
Err(()) => {
32-
append_error_clause = true;
33-
return body;
34-
},
35-
};
30+
let arm = derive_variant_arm(variant, &mut where_clause);
3631
quote! { #body #arm }
3732
});
38-
(match_body, append_error_clause)
33+
(match_body, needs_catchall_branch)
3934
};
4035

4136
input.generics.where_clause = where_clause;
4237

43-
if append_error_clause {
44-
let input_attrs = cg::parse_input_attrs::<AnimateInputAttrs>(&input);
45-
if let Some(fallback) = input_attrs.fallback {
46-
match_body.append_all(quote! {
47-
(this, other) => #fallback(this, other, procedure)
48-
});
49-
} else {
50-
match_body.append_all(quote! { _ => Err(()) });
51-
}
38+
if needs_catchall_branch {
39+
// This ideally shouldn't be needed, but see
40+
// https://github.com/rust-lang/rust/issues/68867
41+
match_body.append_all(quote! { _ => unsafe { debug_unreachable!() } });
5242
}
5343

5444
let name = &input.ident;
5545
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
5646

47+
let fallback = match input_attrs.fallback {
48+
Some(fallback) => quote! { #fallback(self, other, procedure) },
49+
None => quote! { Err(()) },
50+
};
51+
5752
quote! {
5853
impl #impl_generics crate::values::animated::Animate for #name #ty_generics #where_clause {
5954
#[allow(unused_variables, unused_imports)]
@@ -63,6 +58,9 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
6358
other: &Self,
6459
procedure: crate::values::animated::Procedure,
6560
) -> Result<Self, ()> {
61+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
62+
return #fallback;
63+
}
6664
match (self, other) {
6765
#match_body
6866
}
@@ -74,13 +72,17 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
7472
fn derive_variant_arm(
7573
variant: &VariantInfo,
7674
where_clause: &mut Option<WhereClause>,
77-
) -> Result<TokenStream, ()> {
75+
) -> TokenStream {
7876
let variant_attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
79-
if variant_attrs.error {
80-
return Err(());
81-
}
8277
let (this_pattern, this_info) = cg::ref_pattern(&variant, "this");
8378
let (other_pattern, other_info) = cg::ref_pattern(&variant, "other");
79+
80+
if variant_attrs.error {
81+
return quote! {
82+
(&#this_pattern, &#other_pattern) => Err(()),
83+
};
84+
}
85+
8486
let (result_value, result_info) = cg::value(&variant, "result");
8587
let mut computations = quote!();
8688
let iter = result_info.iter().zip(this_info.iter().zip(&other_info));
@@ -107,12 +109,13 @@ fn derive_variant_arm(
107109
}
108110
}
109111
}));
110-
Ok(quote! {
112+
113+
quote! {
111114
(&#this_pattern, &#other_pattern) => {
112115
#computations
113116
Ok(#result_value)
114117
}
115-
})
118+
}
116119
}
117120

118121
#[darling(attributes(animate), default)]

‎components/style_derive/compute_squared_distance.rs

+74-61
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use crate::animate::{AnimationFieldAttrs, AnimationInputAttrs, AnimationVariantA
66
use derive_common::cg;
77
use proc_macro2::TokenStream;
88
use quote::TokenStreamExt;
9-
use syn::{DeriveInput, Path};
9+
use syn::{DeriveInput, Path, WhereClause};
1010
use synstructure;
1111

1212
pub fn derive(mut input: DeriveInput) -> TokenStream {
1313
let animation_input_attrs = cg::parse_input_attrs::<AnimationInputAttrs>(&input);
14+
let input_attrs = cg::parse_input_attrs::<DistanceInputAttrs>(&input);
1415
let no_bound = animation_input_attrs.no_bound.unwrap_or_default();
1516
let mut where_clause = input.generics.where_clause.take();
1617
for param in input.generics.type_params() {
@@ -22,76 +23,31 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
2223
}
2324
}
2425

25-
let (mut match_body, append_error_clause) = {
26+
let (mut match_body, needs_catchall_branch) = {
2627
let s = synstructure::Structure::new(&input);
27-
let mut append_error_clause = s.variants().len() > 1;
28+
let needs_catchall_branch = s.variants().len() > 1;
2829

2930
let match_body = s.variants().iter().fold(quote!(), |body, variant| {
30-
let attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
31-
if attrs.error {
32-
append_error_clause = true;
33-
return body;
34-
}
35-
36-
let (this_pattern, this_info) = cg::ref_pattern(&variant, "this");
37-
let (other_pattern, other_info) = cg::ref_pattern(&variant, "other");
38-
let sum = if this_info.is_empty() {
39-
quote! { crate::values::distance::SquaredDistance::from_sqrt(0.) }
40-
} else {
41-
let mut sum = quote!();
42-
sum.append_separated(this_info.iter().zip(&other_info).map(|(this, other)| {
43-
let field_attrs = cg::parse_field_attrs::<DistanceFieldAttrs>(&this.ast());
44-
if field_attrs.field_bound {
45-
let ty = &this.ast().ty;
46-
cg::add_predicate(
47-
&mut where_clause,
48-
parse_quote!(#ty: crate::values::distance::ComputeSquaredDistance),
49-
);
50-
}
51-
52-
let animation_field_attrs =
53-
cg::parse_field_attrs::<AnimationFieldAttrs>(&this.ast());
54-
55-
if animation_field_attrs.constant {
56-
quote! {
57-
{
58-
if #this != #other {
59-
return Err(());
60-
}
61-
crate::values::distance::SquaredDistance::from_sqrt(0.)
62-
}
63-
}
64-
} else {
65-
quote! {
66-
crate::values::distance::ComputeSquaredDistance::compute_squared_distance(#this, #other)?
67-
}
68-
}
69-
}), quote!(+));
70-
sum
71-
};
72-
quote! {
73-
#body
74-
(&#this_pattern, &#other_pattern) => {
75-
Ok(#sum)
76-
}
77-
}
31+
let arm = derive_variant_arm(variant, &mut where_clause);
32+
quote! { #body #arm }
7833
});
7934

80-
(match_body, append_error_clause)
35+
(match_body, needs_catchall_branch)
8136
};
37+
8238
input.generics.where_clause = where_clause;
8339

84-
if append_error_clause {
85-
let input_attrs = cg::parse_input_attrs::<DistanceInputAttrs>(&input);
86-
if let Some(fallback) = input_attrs.fallback {
87-
match_body.append_all(quote! {
88-
(this, other) => #fallback(this, other)
89-
});
90-
} else {
91-
match_body.append_all(quote! { _ => Err(()) });
92-
}
40+
if needs_catchall_branch {
41+
// This ideally shouldn't be needed, but see:
42+
// https://github.com/rust-lang/rust/issues/68867
43+
match_body.append_all(quote! { _ => unsafe { debug_unreachable!() } });
9344
}
9445

46+
let fallback = match input_attrs.fallback {
47+
Some(fallback) => quote! { #fallback(self, other) },
48+
None => quote! { Err(()) },
49+
};
50+
9551
let name = &input.ident;
9652
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
9753

@@ -103,6 +59,9 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
10359
&self,
10460
other: &Self,
10561
) -> Result<crate::values::distance::SquaredDistance, ()> {
62+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
63+
return #fallback;
64+
}
10665
match (self, other) {
10766
#match_body
10867
}
@@ -111,6 +70,60 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
11170
}
11271
}
11372

73+
fn derive_variant_arm(
74+
variant: &synstructure::VariantInfo,
75+
mut where_clause: &mut Option<WhereClause>,
76+
) -> TokenStream {
77+
let variant_attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
78+
let (this_pattern, this_info) = cg::ref_pattern(&variant, "this");
79+
let (other_pattern, other_info) = cg::ref_pattern(&variant, "other");
80+
81+
if variant_attrs.error {
82+
return quote! {
83+
(&#this_pattern, &#other_pattern) => Err(()),
84+
};
85+
}
86+
87+
let sum = if this_info.is_empty() {
88+
quote! { crate::values::distance::SquaredDistance::from_sqrt(0.) }
89+
} else {
90+
let mut sum = quote!();
91+
sum.append_separated(this_info.iter().zip(&other_info).map(|(this, other)| {
92+
let field_attrs = cg::parse_field_attrs::<DistanceFieldAttrs>(&this.ast());
93+
if field_attrs.field_bound {
94+
let ty = &this.ast().ty;
95+
cg::add_predicate(
96+
&mut where_clause,
97+
parse_quote!(#ty: crate::values::distance::ComputeSquaredDistance),
98+
);
99+
}
100+
101+
let animation_field_attrs =
102+
cg::parse_field_attrs::<AnimationFieldAttrs>(&this.ast());
103+
104+
if animation_field_attrs.constant {
105+
quote! {
106+
{
107+
if #this != #other {
108+
return Err(());
109+
}
110+
crate::values::distance::SquaredDistance::from_sqrt(0.)
111+
}
112+
}
113+
} else {
114+
quote! {
115+
crate::values::distance::ComputeSquaredDistance::compute_squared_distance(#this, #other)?
116+
}
117+
}
118+
}), quote!(+));
119+
sum
120+
};
121+
122+
return quote! {
123+
(&#this_pattern, &#other_pattern) => Ok(#sum),
124+
};
125+
}
126+
114127
#[darling(attributes(distance), default)]
115128
#[derive(Default, FromDeriveInput)]
116129
struct DistanceInputAttrs {

0 commit comments

Comments
 (0)
Please sign in to comment.