-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Document lack of panic safety guarantees of Clone::clone_from
#98461
Document lack of panic safety guarantees of Clone::clone_from
#98461
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
r? @m-ou-se |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns: Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
The term "panic safe" generally refers to ensure unsafe code remains sound in the face of panics. Its use here is not completely accurate: |
library/core/src/clone.rs
Outdated
/// ```no_run | ||
/// use std::sync::atomic::{AtomicBool, Ordering}; | ||
/// | ||
/// // Ensure that static vector accessed only once | ||
/// static WAS_CALLED: AtomicBool = AtomicBool::new(false); | ||
/// if WAS_CALLED.fetch_or(true, Ordering::Acquire){ | ||
/// return; | ||
/// } | ||
/// | ||
/// #[derive(PartialEq, Eq, Debug)] | ||
/// struct IAmPanicDuringClone; | ||
/// | ||
/// impl Clone for IAmPanicDuringClone { | ||
/// fn clone(&self) -> Self { | ||
/// panic!() | ||
/// } | ||
/// } | ||
/// | ||
/// // Here little complicated | ||
/// // because we need to pass it through panic boundary for demo purposes. | ||
/// // Hacky and improper way to do this is usage of static variable. | ||
/// // Don't write like that in production. | ||
/// static mut A: Vec<IAmPanicDuringClone> = Vec::new(); | ||
/// let a = unsafe{ | ||
/// // Safe because we have AtomicBool check before. | ||
/// &mut A | ||
/// }; | ||
/// | ||
/// *a = vec![ | ||
/// IAmPanicDuringClone, | ||
/// IAmPanicDuringClone, | ||
/// IAmPanicDuringClone, | ||
/// ]; | ||
/// let b = vec![ | ||
/// IAmPanicDuringClone, | ||
/// IAmPanicDuringClone, | ||
/// IAmPanicDuringClone, | ||
/// ]; | ||
/// let c = vec![IAmPanicDuringClone, IAmPanicDuringClone]; | ||
/// | ||
/// // This succeedes. | ||
/// assert_eq!(a, &b); | ||
/// std::panic::catch_unwind(|| { | ||
/// unsafe { A.clone_from(&c) }; | ||
/// }).expect_err("Must panic"); | ||
/// | ||
/// // This fails at Rust 1.61. | ||
/// assert_eq!(unsafe { &A }, &b); | ||
/// ``` |
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'm not comfortable with using this as an example (it feels a bit contrived), how do you feel about using this version instead?
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.
Absolutely agree, your version is much more cleaner and has less noise in it.
Would fix PR later.
c51267f
to
67251ab
Compare
It isn't panic safe de-facto (e.g. for `Vec`) so I just document current behaviour. Panic safety was mentioned by @scottmcm when discussing [PR with deriving clone_from](rust-lang#98445 (comment)). Co-authored-by: Jane Losare-Lusby <[email protected]>
67251ab
to
d665f28
Compare
/// let dest = Mutex::new(vec![]); | ||
/// | ||
/// // clone from src into dest | ||
/// std::panic::catch_unwind(|| { |
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.
Instead of a Mutex
, you can use catch_unwind(AssertUnwindSafe(..))
.
/// // dest is left in an incomplete state with only half of the clone having | ||
/// // completed successfully | ||
/// assert_eq!(vec![PanicAtTheClone(false)], *guard); |
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.
Maybe change this to just assert_ne!(src, dest);
, since we don't want to make promises about the resulting state.
/// so it cannot preserve old data stored in those resources. | ||
/// | ||
/// That example shows one case of such behaviour: | ||
/// ```no_run |
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.
Why is this no_run?
ping @joshtriplett for your rfcbot concern. |
@AngelicosPhosphoros any updates on this? thanks |
@rfcbot resolve reword Sorry to have missed the ping here. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
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.
Just a few tiny drive-by suggestions!
/// | ||
/// # Panic behaviour | ||
/// | ||
/// Due to it's nature, this method cannot guarantee that it would be transactional. |
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.
Tiny grammar nit: This should read "Due to its" (not "it's" with an apostrophe)
/// # Panic behaviour | ||
/// | ||
/// Due to it's nature, this method cannot guarantee that it would be transactional. | ||
/// If call panics, `self` can be left in inconsistent state. |
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.
Tiny nit: "If a call panics"
/// `clone_from` is intended to preserve resources owned by `self` | ||
/// so it cannot preserve old data stored in those resources. | ||
/// | ||
/// That example shows one case of such behaviour: |
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.
Tiny nit: "This example"
/// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality except cases | ||
/// when it panics in the middle of operation. It can be overridden to reuse the resources |
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 think this sentence could be readily misinterpreted as “clone_from
is equivalent to clone
except that in some cases it may panic”, since this is the first mention of panicking in the text. I'm not sure exactly what to do about that, but how about "…in functionality (except in the event of a panic).” which emphasizes that it's about the consequences of a panic, not causing a panic. The “in the middle” is explained below so it doesn't need to be brought up here.
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@AngelicosPhosphoros seems this PR needs a tiny bit of polish and then it's gold. Any capacity to drive this to the finish line? (No pressure, just asking as part of the periodic reviews we do on pending contributions). Thanks 🙂 |
Author hasn't responded in months... Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this. @rustbot label: +S-inactive |
It isn't panic safe de-facto (e.g. for
Vec
) so I just document current behaviour.Panic safety was mentioned by @scottmcm when discussing PR with deriving clone_from.