Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse existing Somes in Option::(x)or #116481

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
  • Loading branch information
scottmcm committed Oct 6, 2023
commit 5432d13bb04cecf213c2753b25cfb1e366cb8026
8 changes: 4 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ impl<T> Option<T> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn or(self, optb: Option<T>) -> Option<T> {
match self {
Some(x) => Some(x),
x @ Some(_) => x,
None => optb,
}
}
Expand All @@ -1500,7 +1500,7 @@ impl<T> Option<T> {
F: FnOnce() -> Option<T>,
{
match self {
Some(x) => Some(x),
x @ Some(_) => x,
None => f(),
}
}
Expand Down Expand Up @@ -1530,8 +1530,8 @@ impl<T> Option<T> {
#[stable(feature = "option_xor", since = "1.37.0")]
pub fn xor(self, optb: Option<T>) -> Option<T> {
match (self, optb) {
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(a @ Some(_), None) => a,
(None, b @ Some(_)) => b,
_ => None,
}
}
Expand Down
Loading