servo: Merge #19227 - Work around "this expression will panic at run-time" warnings (from servo:array-warn); r=emilio
authorSimon Sapin <simon.sapin@exyr.org>
Wed, 15 Nov 2017 11:00:29 -0600
changeset 436571 87eba7dbe8434f7f55b2967d83295aacd6edab41
parent 436570 5ccd0ffe9bc773edd8745a2aefb7ca85c93e0c74
child 436572 dbe88ff91a34c28eec2c9ec6473351b94755759c
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersemilio
milestone59.0a1
servo: Merge #19227 - Work around "this expression will panic at run-time" warnings (from servo:array-warn); r=emilio CC https://github.com/rust-lang/rust/issues/45850 Source-Repo: https://github.com/servo/servo Source-Revision: 9863d514512aabbcfaeabba7e530b87f61a8d489
servo/components/style/properties/gecko.mako.rs
--- a/servo/components/style/properties/gecko.mako.rs
+++ b/servo/components/style/properties/gecko.mako.rs
@@ -1326,50 +1326,50 @@ pub fn clone_transform_from_list(
 
 <%def name="impl_transform_origin(ident, gecko_ffi_name)">
     #[allow(non_snake_case)]
     pub fn set_${ident}(&mut self, v: values::computed::TransformOrigin) {
         self.gecko.${gecko_ffi_name}[0].set(v.horizontal);
         self.gecko.${gecko_ffi_name}[1].set(v.vertical);
         // transform-origin supports the third value for depth, while
         // -moz-window-transform-origin doesn't. The following code is
-        // for handling this difference. Rust (incorrectly) generates
-        // an unsuppressible warning, but we know it's safe here.
-        // See rust-lang/rust#45850. Also if we can have more knowledge
+        // for handling this difference. If we can have more knowledge
         // about the type here, we may want to check that the length is
         // exactly either 2 or 3 in compile time.
-        if self.gecko.${gecko_ffi_name}.len() == 3 {
-            self.gecko.${gecko_ffi_name}[2].set(v.depth);
+        if let Some(third) = self.gecko.${gecko_ffi_name}.get_mut(2) {
+            third.set(v.depth);
         }
     }
 
     #[allow(non_snake_case)]
     pub fn copy_${ident}_from(&mut self, other: &Self) {
         self.gecko.${gecko_ffi_name}[0].copy_from(&other.gecko.${gecko_ffi_name}[0]);
         self.gecko.${gecko_ffi_name}[1].copy_from(&other.gecko.${gecko_ffi_name}[1]);
-        if self.gecko.${gecko_ffi_name}.len() == 3 {
-            self.gecko.${gecko_ffi_name}[2].copy_from(&other.gecko.${gecko_ffi_name}[2]);
+        if let (Some(self_third), Some(other_third)) =
+            (self.gecko.${gecko_ffi_name}.get_mut(2), other.gecko.${gecko_ffi_name}.get(2))
+        {
+            self_third.copy_from(other_third)
         }
     }
 
     #[allow(non_snake_case)]
     pub fn reset_${ident}(&mut self, other: &Self) {
         self.copy_${ident}_from(other)
     }
 
     #[allow(non_snake_case)]
     pub fn clone_${ident}(&self) -> values::computed::TransformOrigin {
         use values::computed::{Length, LengthOrPercentage, TransformOrigin};
         TransformOrigin {
             horizontal: LengthOrPercentage::from_gecko_style_coord(&self.gecko.${gecko_ffi_name}[0])
                 .expect("clone for LengthOrPercentage failed"),
             vertical: LengthOrPercentage::from_gecko_style_coord(&self.gecko.${gecko_ffi_name}[1])
                 .expect("clone for LengthOrPercentage failed"),
-            depth: if self.gecko.${gecko_ffi_name}.len() == 3 {
-                Length::from_gecko_style_coord(&self.gecko.${gecko_ffi_name}[2])
+            depth: if let Some(third) = self.gecko.${gecko_ffi_name}.get(2) {
+                Length::from_gecko_style_coord(third)
                     .expect("clone for Length failed")
             } else {
                 Length::new(0.)
             },
         }
     }
 </%def>