In Option::get_or_insert_with(), forget the None instead of dropping it. by kpreid · Pull Request #148562 · rust-lang/rust (original) (raw)
I get the maintenance principle you're going for, but on further consideration of this code, const_precise_live_drops is actually a red herring and I should not have brought it up. const_precise_live_drops is IIUC about distinguishing places that are possibly dropped from places that are always moved out of (where the compiler currently assumes all places are possibly dropped), but in this case, the compiler would need to look not just at the data flow in the code, but also the values in those places — the fact that self == None at this point. It would have to, stably, follow the logic that:
- The compiler analyzed the body and proved that
drop_in_place::<T>()was unreachable, - therefore it's valid to omit
T: [const] Destructfrom this function signature.
Isn't that the sort of hard-to-have-guarantees-about static analysis tarpit we don’t want in Rust's type system? Don't we want to stick to types and data-flow and not rely on statically knowing properties of values except for lints?
Accordingly, I have deleted the mention of const_precise_live_drops, and rephrased the comment to express that:
- This implementation allows the signature to be relaxed to
T: [const] Destruct, benefiting const callers. - Separately, this implementation avoids needless drop code, which is a performance benefit outside of const.