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

Document lack of panic safety guarantees of Clone::clone_from #98461

Conversation

AngelicosPhosphoros
Copy link
Contributor

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.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 24, 2022
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @kennytm

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 24, 2022
@AngelicosPhosphoros
Copy link
Contributor Author

r? @m-ou-se
We had already discussed this so maybe you would review?

@rust-highfive rust-highfive assigned m-ou-se and unassigned kennytm Jul 3, 2022
@m-ou-se m-ou-se added I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 3, 2022
@joshtriplett joshtriplett removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jul 20, 2022
@joshtriplett
Copy link
Member

Since this is a change to the documented guarantees (or lack thereof):

@rfcbot merge

Also, @Amanieu wanted to propose a different wording to avoid the term "safe" (which in this context is unrelated to the usual Rust safe/unsafe distinction).

@rfcbot concern reword

@rfcbot
Copy link

rfcbot commented Jul 20, 2022

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.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jul 20, 2022
@Amanieu
Copy link
Member

Amanieu commented Jul 20, 2022

The term "panic safe" generally refers to ensure unsafe code remains sound in the face of panics. Its use here is not completely accurate: clone_from must be panic-safe since it is a safe function. However what it can do is leave the contents of self as indeterminate if a panic occurs.

Comment on lines 142 to 179
/// ```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);
/// ```
Copy link
Member

@yaahc yaahc Jul 26, 2022

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?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b0df895179ea9769302d6b866baec535

Copy link
Contributor Author

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.

@m-ou-se m-ou-se added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Jul 27, 2022
@AngelicosPhosphoros AngelicosPhosphoros force-pushed the document_panic_safety_of_clone_from branch from c51267f to 67251ab Compare August 26, 2022 18:00
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]>
@AngelicosPhosphoros AngelicosPhosphoros force-pushed the document_panic_safety_of_clone_from branch from 67251ab to d665f28 Compare August 26, 2022 18:08
@AngelicosPhosphoros
Copy link
Contributor Author

@m-ou-se Reworded comment, updated example as @yaahc requested (also added her as co-author because I copied her example).

@rustbot label: +S-waiting-on-review -S-waiting-on-author

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 26, 2022
@JohnCSimon JohnCSimon removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 8, 2022
@JohnCSimon JohnCSimon added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 8, 2022
Comment on lines +162 to +165
/// let dest = Mutex::new(vec![]);
///
/// // clone from src into dest
/// std::panic::catch_unwind(|| {
Copy link
Member

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(..)).

Comment on lines +176 to +178
/// // dest is left in an incomplete state with only half of the clone having
/// // completed successfully
/// assert_eq!(vec![PanicAtTheClone(false)], *guard);
Copy link
Member

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
Copy link
Member

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?

@m-ou-se
Copy link
Member

m-ou-se commented Feb 28, 2023

ping @joshtriplett for your rfcbot concern.

@KittyBorgX KittyBorgX added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 29, 2023
@Dylan-DPC
Copy link
Member

@AngelicosPhosphoros any updates on this? thanks

@joshtriplett
Copy link
Member

@rfcbot resolve reword

Sorry to have missed the ping here.

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Aug 19, 2023
@rfcbot
Copy link

rfcbot commented Aug 19, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

Copy link
Contributor

@joshlf joshlf left a 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.
Copy link
Contributor

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.
Copy link
Contributor

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: "This example"

Comment on lines +127 to +128
/// `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
Copy link
Contributor

@kpreid kpreid Aug 25, 2023

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.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Aug 29, 2023
@rfcbot
Copy link

rfcbot commented Aug 29, 2023

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.

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Sep 1, 2023
@JohnCSimon JohnCSimon added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2023
@apiraino
Copy link
Contributor

@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 🙂

@JohnCSimon
Copy link
Member

Author hasn't responded in months...

@AngelicosPhosphoros

Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Apr 14, 2024
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet