From 5432d13bb04cecf213c2753b25cfb1e366cb8026 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Fri, 6 Oct 2023 01:41:48 -0700 Subject: [PATCH] Reuse existing `Some`s 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: --- library/core/src/option.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 9899014e85a..bdaeea66622 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1475,7 +1475,7 @@ pub fn filter

(self, predicate: P) -> Self #[stable(feature = "rust1", since = "1.0.0")] pub fn or(self, optb: Option) -> Option { match self { - Some(x) => Some(x), + x @ Some(_) => x, None => optb, } } @@ -1500,7 +1500,7 @@ pub fn or_else(self, f: F) -> Option F: FnOnce() -> Option, { match self { - Some(x) => Some(x), + x @ Some(_) => x, None => f(), } } @@ -1530,8 +1530,8 @@ pub fn or_else(self, f: F) -> Option #[stable(feature = "option_xor", since = "1.37.0")] pub fn xor(self, optb: Option) -> Option { match (self, optb) { - (Some(a), None) => Some(a), - (None, Some(b)) => Some(b), + (a @ Some(_), None) => a, + (None, b @ Some(_)) => b, _ => None, } }