Skip to content

Commit 5432d13

Browse files
committed
Reuse existing Somes in Option::(x)or
LLVM still has trouble re-using discriminants sometimes when rebuilding a two-variant enum, so when we have the correct variant already built, just use it. That's simpler in LLVM *and* in MIR, so might as well: <https://rust.godbolt.org/z/KhdE8eToW>
1 parent 579be69 commit 5432d13

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

library/core/src/option.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ impl<T> Option<T> {
14751475
#[stable(feature = "rust1", since = "1.0.0")]
14761476
pub fn or(self, optb: Option<T>) -> Option<T> {
14771477
match self {
1478-
Some(x) => Some(x),
1478+
x @ Some(_) => x,
14791479
None => optb,
14801480
}
14811481
}
@@ -1500,7 +1500,7 @@ impl<T> Option<T> {
15001500
F: FnOnce() -> Option<T>,
15011501
{
15021502
match self {
1503-
Some(x) => Some(x),
1503+
x @ Some(_) => x,
15041504
None => f(),
15051505
}
15061506
}
@@ -1530,8 +1530,8 @@ impl<T> Option<T> {
15301530
#[stable(feature = "option_xor", since = "1.37.0")]
15311531
pub fn xor(self, optb: Option<T>) -> Option<T> {
15321532
match (self, optb) {
1533-
(Some(a), None) => Some(a),
1534-
(None, Some(b)) => Some(b),
1533+
(a @ Some(_), None) => a,
1534+
(None, b @ Some(_)) => b,
15351535
_ => None,
15361536
}
15371537
}

0 commit comments

Comments
 (0)