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 582bd3c

Browse files
emilioIstvan Miklos
authored and
Istvan Miklos
committedFeb 12, 2020
style: Make rust generate better code for some of the functions introduced here.
See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760
1 parent 22e4a0e commit 582bd3c

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed
 

‎components/style/values/specified/length.rs

+65-19
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,22 @@ impl FontRelativeLength {
9898

9999
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
100100
use self::FontRelativeLength::*;
101+
102+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
103+
return Err(());
104+
}
105+
101106
Ok(match (self, other) {
102107
(&Em(one), &Em(other)) => Em(one + other),
103108
(&Ex(one), &Ex(other)) => Ex(one + other),
104109
(&Ch(one), &Ch(other)) => Ch(one + other),
105110
(&Rem(one), &Rem(other)) => Rem(one + other),
106-
_ => return Err(()),
111+
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
112+
// able to figure it own on its own so we help.
113+
_ => unsafe {
114+
match *self { Em(..) | Ex(..) | Ch(..) | Rem(..) => {} }
115+
debug_unreachable!("Forgot to handle unit in try_sum()")
116+
},
107117
})
108118
}
109119

@@ -260,12 +270,22 @@ impl ViewportPercentageLength {
260270

261271
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
262272
use self::ViewportPercentageLength::*;
273+
274+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
275+
return Err(());
276+
}
277+
263278
Ok(match (self, other) {
264279
(&Vw(one), &Vw(other)) => Vw(one + other),
265280
(&Vh(one), &Vh(other)) => Vh(one + other),
266281
(&Vmin(one), &Vmin(other)) => Vmin(one + other),
267282
(&Vmax(one), &Vmax(other)) => Vmax(one + other),
268-
_ => return Err(()),
283+
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
284+
// able to figure it own on its own so we help.
285+
_ => unsafe {
286+
match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} }
287+
debug_unreachable!("Forgot to handle unit in try_sum()")
288+
},
269289
})
270290
}
271291

@@ -500,14 +520,23 @@ impl NoCalcLength {
500520
pub(crate) fn try_sum(&self, other: &Self) -> Result<Self, ()> {
501521
use self::NoCalcLength::*;
502522

523+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
524+
return Err(());
525+
}
526+
503527
Ok(match (self, other) {
504528
(&Absolute(ref one), &Absolute(ref other)) => Absolute(*one + *other),
505529
(&FontRelative(ref one), &FontRelative(ref other)) => FontRelative(one.try_sum(other)?),
506530
(&ViewportPercentage(ref one), &ViewportPercentage(ref other)) => ViewportPercentage(one.try_sum(other)?),
507531
(&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => {
508532
ServoCharacterWidth(CharacterWidth(one.0 + other.0))
509533
},
510-
_ => return Err(()),
534+
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
535+
// able to figure it own on its own so we help.
536+
_ => unsafe {
537+
match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} }
538+
debug_unreachable!("Forgot to handle unit in try_sum()")
539+
},
511540
})
512541
}
513542

@@ -532,17 +561,22 @@ impl SpecifiedValueInfo for NoCalcLength {}
532561
impl PartialOrd for NoCalcLength {
533562
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
534563
use self::NoCalcLength::*;
564+
565+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
566+
return None;
567+
}
568+
535569
match (self, other) {
536570
(&Absolute(ref one), &Absolute(ref other)) => one.to_px().partial_cmp(&other.to_px()),
537571
(&FontRelative(ref one), &FontRelative(ref other)) => one.partial_cmp(other),
538572
(&ViewportPercentage(ref one), &ViewportPercentage(ref other)) => one.partial_cmp(other),
539573
(&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => one.0.partial_cmp(&other.0),
540-
_ => {
541-
// This will at least catch issues some of the time... Maybe
542-
// could be worth a derive thing?
543-
debug_assert_ne!(self, other, "Forgot a match arm?");
544-
None
545-
}
574+
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
575+
// able to figure it own on its own so we help.
576+
_ => unsafe {
577+
match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} }
578+
debug_unreachable!("Forgot an arm in partial_cmp?")
579+
},
546580
}
547581
}
548582
}
@@ -598,16 +632,22 @@ impl Mul<CSSFloat> for Length {
598632
impl PartialOrd for FontRelativeLength {
599633
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
600634
use self::FontRelativeLength::*;
635+
636+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
637+
return None;
638+
}
639+
601640
match (self, other) {
602641
(&Em(ref one), &Em(ref other)) => one.partial_cmp(other),
603642
(&Ex(ref one), &Ex(ref other)) => one.partial_cmp(other),
604643
(&Ch(ref one), &Ch(ref other)) => one.partial_cmp(other),
605644
(&Rem(ref one), &Rem(ref other)) => one.partial_cmp(other),
606-
_ => {
607-
// This will at least catch issues some of the time.
608-
debug_assert_ne!(self, other, "Forgot a match arm?");
609-
None
610-
}
645+
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
646+
// able to figure it own on its own so we help.
647+
_ => unsafe {
648+
match *self { Em(..) | Ex(..) | Ch(..) | Rem(..) => {} }
649+
debug_unreachable!("Forgot an arm in partial_cmp?")
650+
},
611651
}
612652
}
613653
}
@@ -643,16 +683,22 @@ impl Mul<CSSFloat> for ViewportPercentageLength {
643683
impl PartialOrd for ViewportPercentageLength {
644684
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
645685
use self::ViewportPercentageLength::*;
686+
687+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
688+
return None;
689+
}
690+
646691
match (self, other) {
647692
(&Vw(ref one), &Vw(ref other)) => one.partial_cmp(other),
648693
(&Vh(ref one), &Vh(ref other)) => one.partial_cmp(other),
649694
(&Vmin(ref one), &Vmin(ref other)) => one.partial_cmp(other),
650695
(&Vmax(ref one), &Vmax(ref other)) => one.partial_cmp(other),
651-
_ => {
652-
// This will at least catch issues some of the time.
653-
debug_assert_ne!(self, other, "Forgot a match arm?");
654-
None
655-
}
696+
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
697+
// able to figure it own on its own so we help.
698+
_ => unsafe {
699+
match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} }
700+
debug_unreachable!("Forgot an arm in partial_cmp?")
701+
},
656702
}
657703
}
658704
}

0 commit comments

Comments
 (0)
Please sign in to comment.