From 89119d274562bec4dbaa759e75ea3a8fc1138b24 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Wed, 12 Feb 2020 01:35:34 +0000 Subject: [PATCH] Bug 1613517 - Make rust generate better code for derive(Animate) and derive(ComputeSquaredDistance). r=heycam See https://github.com/rust-lang/rust/issues/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 UltraBlame original commit: bd4711d31ec6ab173b77406cb4ede0fa7a484af3 --- servo/components/style/values/animated/mod.rs | 6 +- servo/components/style/values/distance.rs | 6 +- servo/components/style_derive/animate.rs | 255 ++++---- .../style_derive/compute_squared_distance.rs | 543 +++++++++++------- 4 files changed, 485 insertions(+), 325 deletions(-) diff --git a/servo/components/style/values/animated/mod.rs b/servo/components/style/values/animated/mod.rs index e487773ccfb7d..539b03a54078f 100644 --- a/servo/components/style/values/animated/mod.rs +++ b/servo/components/style/values/animated/mod.rs @@ -883,9 +883,9 @@ corresponding / match arm -is -not -generated +returns +an +error . / / diff --git a/servo/components/style/values/distance.rs b/servo/components/style/values/distance.rs index 9856544b70094..b0798833109e4 100644 --- a/servo/components/style/values/distance.rs +++ b/servo/components/style/values/distance.rs @@ -195,9 +195,9 @@ corresponding / match arm -is -not -generated +returns +an +error . / / diff --git a/servo/components/style_derive/animate.rs b/servo/components/style_derive/animate.rs index d5033d2e13cb9..aaf204caeaaa5 100644 --- a/servo/components/style_derive/animate.rs +++ b/servo/components/style_derive/animate.rs @@ -133,6 +133,23 @@ input ) ; let +input_attrs += +cg +: +: +parse_input_attrs +: +: +< +AnimateInputAttrs +> +( +& +input +) +; +let no_bound = animation_input_attrs @@ -225,7 +242,7 @@ let ( mut match_body -append_error_clause +needs_catchall_branch ) = { @@ -242,8 +259,7 @@ input ) ; let -mut -append_error_clause +needs_catchall_branch = s . @@ -284,7 +300,6 @@ variant let arm = -match derive_variant_arm ( variant @@ -292,31 +307,6 @@ variant mut where_clause ) -{ -Ok -( -arm -) -= -> -arm -Err -( -( -) -) -= -> -{ -append_error_clause -= -true -; -return -body -; -} -} ; quote ! @@ -331,7 +321,7 @@ arm ; ( match_body -append_error_clause +needs_catchall_branch ) } ; @@ -344,36 +334,38 @@ where_clause where_clause ; if -append_error_clause +needs_catchall_branch { -let -input_attrs -= -cg -: -: -parse_input_attrs -: +/ +/ +This +ideally +shouldn +' +t +be +needed +but +see +/ +/ +https : -< -AnimateInputAttrs -> -( -& -input -) -; -if -let -Some -( -fallback -) -= -input_attrs +/ +/ +github . -fallback -{ +com +/ +rust +- +lang +/ +rust +/ +issues +/ +68867 match_body . append_all @@ -381,45 +373,20 @@ append_all quote ! { -( -this -other -) +_ = > -# -fallback -( -this -other -procedure -) -} -) -; -} -else +unsafe { -match_body -. -append_all -( -quote +debug_unreachable ! -{ -_ -= -> -Err -( ( ) -) +} } ) ; } -} let name = @@ -443,6 +410,45 @@ split_for_impl ( ) ; +let +fallback += +match +input_attrs +. +fallback +{ +Some +( +fallback +) += +> +quote +! +{ +# +fallback +( +self +other +procedure +) +} +None += +> +quote +! +{ +Err +( +( +) +) +} +} +; quote ! { @@ -510,6 +516,35 @@ Self ) > { +if +std +: +: +mem +: +: +discriminant +( +self +) +! += +std +: +: +mem +: +: +discriminant +( +other +) +{ +return +# +fallback +; +} match ( self @@ -541,12 +576,7 @@ WhereClause ) - > -Result -< TokenStream -( -) -> { let variant_attrs @@ -569,19 +599,6 @@ ast ) ) ; -if -variant_attrs -. -error -{ -return -Err -( -( -) -) -; -} let ( this_pattern @@ -618,6 +635,33 @@ other " ) ; +if +variant_attrs +. +error +{ +return +quote +! +{ +( +& +# +this_pattern +& +# +other_pattern +) += +> +Err +( +( +) +) +} +; +} let ( result_value @@ -833,8 +877,6 @@ procedure ) ) ; -Ok -( quote ! { @@ -858,7 +900,6 @@ result_value ) } } -) } # [ diff --git a/servo/components/style_derive/compute_squared_distance.rs b/servo/components/style_derive/compute_squared_distance.rs index cc2dd50796c3b..7883dff929f27 100644 --- a/servo/components/style_derive/compute_squared_distance.rs +++ b/servo/components/style_derive/compute_squared_distance.rs @@ -94,6 +94,7 @@ syn { DeriveInput Path +WhereClause } ; use @@ -130,6 +131,23 @@ input ) ; let +input_attrs += +cg +: +: +parse_input_attrs +: +: +< +DistanceInputAttrs +> +( +& +input +) +; +let no_bound = animation_input_attrs @@ -222,7 +240,7 @@ let ( mut match_body -append_error_clause +needs_catchall_branch ) = { @@ -242,8 +260,7 @@ input ) ; let -mut -append_error_clause +needs_catchall_branch = s . @@ -282,7 +299,289 @@ variant | { let -attrs +arm += +derive_variant_arm +( +variant +& +mut +where_clause +) +; +quote +! +{ +# +body +# +arm +} +} +) +; +( +match_body +needs_catchall_branch +) +} +; +input +. +generics +. +where_clause += +where_clause +; +if +needs_catchall_branch +{ +/ +/ +This +ideally +shouldn +' +t +be +needed +but +see +: +/ +/ +https +: +/ +/ +github +. +com +/ +rust +- +lang +/ +rust +/ +issues +/ +68867 +match_body +. +append_all +( +quote +! +{ +_ += +> +unsafe +{ +debug_unreachable +! +( +) +} +} +) +; +} +let +fallback += +match +input_attrs +. +fallback +{ +Some +( +fallback +) += +> +quote +! +{ +# +fallback +( +self +other +) +} +None += +> +quote +! +{ +Err +( +( +) +) +} +} +; +let +name += +& +input +. +ident +; +let +( +impl_generics +ty_generics +where_clause +) += +input +. +generics +. +split_for_impl +( +) +; +quote +! +{ +impl +# +impl_generics +crate +: +: +values +: +: +distance +: +: +ComputeSquaredDistance +for +# +name +# +ty_generics +# +where_clause +{ +# +[ +allow +( +unused_variables +unused_imports +) +] +# +[ +inline +] +fn +compute_squared_distance +( +& +self +other +: +& +Self +) +- +> +Result +< +crate +: +: +values +: +: +distance +: +: +SquaredDistance +( +) +> +{ +if +std +: +: +mem +: +: +discriminant +( +self +) +! += +std +: +: +mem +: +: +discriminant +( +other +) +{ +return +# +fallback +; +} +match +( +self +other +) +{ +# +match_body +} +} +} +} +} +fn +derive_variant_arm +( +variant +: +& +synstructure +: +: +VariantInfo +mut +where_clause +: +& +mut +Option +< +WhereClause +> +) +- +> +TokenStream +{ +let +variant_attrs = cg : @@ -302,19 +601,6 @@ ast ) ) ; -if -attrs -. -error -{ -append_error_clause -= -true -; -return -body -; -} let ( this_pattern @@ -351,6 +637,33 @@ other " ) ; +if +variant_attrs +. +error +{ +return +quote +! +{ +( +& +# +this_pattern +& +# +other_pattern +) += +> +Err +( +( +) +) +} +; +} let sum = @@ -590,11 +903,10 @@ quote sum } ; +return quote ! { -# -body ( & # @@ -605,206 +917,13 @@ other_pattern ) = > -{ Ok ( # sum ) } -} -} -) ; -( -match_body -append_error_clause -) -} -; -input -. -generics -. -where_clause -= -where_clause -; -if -append_error_clause -{ -let -input_attrs -= -cg -: -: -parse_input_attrs -: -: -< -DistanceInputAttrs -> -( -& -input -) -; -if -let -Some -( -fallback -) -= -input_attrs -. -fallback -{ -match_body -. -append_all -( -quote -! -{ -( -this -other -) -= -> -# -fallback -( -this -other -) -} -) -; -} -else -{ -match_body -. -append_all -( -quote -! -{ -_ -= -> -Err -( -( -) -) -} -) -; -} -} -let -name -= -& -input -. -ident -; -let -( -impl_generics -ty_generics -where_clause -) -= -input -. -generics -. -split_for_impl -( -) -; -quote -! -{ -impl -# -impl_generics -crate -: -: -values -: -: -distance -: -: -ComputeSquaredDistance -for -# -name -# -ty_generics -# -where_clause -{ -# -[ -allow -( -unused_variables -unused_imports -) -] -# -[ -inline -] -fn -compute_squared_distance -( -& -self -other -: -& -Self -) -- -> -Result -< -crate -: -: -values -: -: -distance -: -: -SquaredDistance -( -) -> -{ -match -( -self -other -) -{ -# -match_body -} -} -} -} } # [