-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Remove calls to mem::forget
and mem::replace
in Option::get_or_insert_with
.
#111301
Remove calls to mem::forget
and mem::replace
in Option::get_or_insert_with
.
#111301
Conversation
r? @scottmcm (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
library/core/src/option.rs
Outdated
@@ -1554,8 +1554,11 @@ impl<T> Option<T> { | |||
pub fn insert(&mut self, value: T) -> &mut T { | |||
*self = Some(value); | |||
|
|||
// SAFETY: the code above just filled the option | |||
unsafe { self.as_mut().unwrap_unchecked() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there some problem you were hitting because of the function calls? Is there a test you could add to detect whatever it was, to keep it from happening again in future?
Especially with MIR inlining on for core
now, it's not clear to me that open-coding these is better than leaving them alone.
@rustbot author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't having any specific problems, i just thought it might be better to merge the two functions into a single match statement, but if you think it would be better to just leave them, i can get rid of those changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without any specific reason, I'd like to just leave it as-is. Otherwise it's equally valid to "might be better to use the clearer combinators instead of writing them out by hand", and could flip-flop back and forth in the implementation.
If you cut this down to just the forget-replace cleanup, then I'd approve it.
@rustbot author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair enough, i have reverted the other changes.
// here and wants us to ensure `T` can be dropped at compile time. | ||
mem::forget(mem::replace(self, Some(f()))) | ||
if let None = self { | ||
*self = Some(f()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes sense to me, though -- the forget
+replace
is a residue from when this was unstably const
, before that was undone.
@rustbot ready |
and `mem::replace` in `Option::get_or_insert_with`.
core::option
with match statements.mem::forget
and mem::replace
in Option::get_or_insert_with
.
@rustbot ready |
Thanks! @bors r+ rollup |
Rollup of 7 pull requests Successful merges: - rust-lang#105583 (Operand::extract_field: only cast llval if it's a pointer and replace bitcast w/ pointercast.) - rust-lang#110094 (clean up `transmute`s in `core`) - rust-lang#111150 (added TraitAlias to check_item() for missing_docs) - rust-lang#111293 (rustc --explain E0726 - grammar fixing (it's => its + add a `the` where it felt right to do so)) - rust-lang#111300 (Emit while_true lint spanning the entire loop condition) - rust-lang#111301 (Remove calls to `mem::forget` and `mem::replace` in `Option::get_or_insert_with`.) - rust-lang#111303 (update Rust Unstable Book docs for `--extern force`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
This removes the unneeded calls to
mem::forget
andmem::replace
inOption::get_or_insert_with
.