Skip to content

Clarify &mut-methods' docs on sync::OnceLock #140715

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

Merged
merged 1 commit into from
Jun 3, 2025

Conversation

lukaslueg
Copy link
Contributor

@lukaslueg lukaslueg commented May 6, 2025

Three small changes to the docs of sync::OnceLock:

  • The docs for OnceLock::take() used to say "Safety is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt OnceLock, as there is (currently) no way to interact with a OnceLock in an unsafe way; there are no unsafe methods on OnceLock, so there is "safety" guarantee required anywhere. What we simply meant to say is "Synchronization is guaranteed...".
  • I've add that phrase to the other methods of OnceLock which take a &mut self, to highlight the fact that having a &mut OnceLock guarantees that synchronization with other threads is not required. This is the same as with Mutex::get_mut(), Cell::get_mut(), and others.
  • In that spirit, the half-sentence "or being initialized" was removed from get_mut(), as there is no way that the OnceLock is being initialized while we are holding &mut to it. Probably a copy&paste from .get()

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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 May 6, 2025
@mejrs
Copy link
Contributor

mejrs commented May 6, 2025

I like how Mutex and RefCell phrase this: "since this method borrows the $thing mutably, ..". What about something similar like "since this method borrows the OnceLock mutably, no synchronization is necessary"?

@lukaslueg lukaslueg force-pushed the oncecellsyncdocs branch from 031b2b8 to 767083a Compare May 8, 2025 18:04
Comment on lines 162 to 165
/// Returns `None` if the cell is uninitialized.
/// This method never blocks. Since it borrows the `OnceLock` mutably,
/// it is statically guaranteed that no active borrows to the `OnceLock`
/// or the underlying data exist, including from other threads.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the fix here, sorry for the delay (I thought it was still waiting on a response to the previous comment). The change looks pretty good to me, a few possible tweaks:

  • "Since it borrows" -> "Since this call borrows", to match mutex docs and disambiguate "it"
  • Drop "or the underlying data" bit; since the OnceLock owns data, that part is implied
  • If the "Returns None..." sentence is meant to be its own paragraph, it needs an extra empty line between it and the start of the next. Or else just reflow to 100 chars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nitpicking, yet I tend to disagree: "Since it borrows" refers to "This method". "This method never blocks. Since this call borrows..." is objectively worse imho. "Or the underlying data" is specifically there to drive home what would be implied otherwise; oftentimes &self-taking methods on some T return &Inner, while &mut self return nothing; but OnceLock (and others) make use of the fact that the very existence of &mut Inner implies that there is no usable &[mut] OnceLock; there can be no borrows of whats in the OnceLock if we can call these methods at all on the OnceLock. I'd like to avoid that very implication.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough for "since it borrows", I missed that the previous sentence refers to "this method".

For the second bit, I suggested this change to align with the other similar types. There are plenty of times where ownership of the Mutex/RwLock/RefCell is discussed in their docs, but the term "underlying data" only shows up when the inner value is being accessed in some way. The motivation doesn't seem strong enough to update this everywhere that borrows on the containers are mentioned. (If Container<T> owns T and you have &mut Container<T>, nobody else can be borrowing the container so nobody else can access T).

@rustbot

This comment has been minimized.

@tgross35
Copy link
Contributor

Thanks!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 28, 2025

📌 Commit 3792ee4 has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 28, 2025
@lukaslueg
Copy link
Contributor Author

Thanks!

@bors r+ rollup

Was in the process of rebasing in case you are wondering.

@tgross35
Copy link
Contributor

Was in the process of rebasing in case you are wondering.

As in, on top of master? This shouldn't be needed (in fact it's better to --keep-base unless there are suspected conflicts, because GH's "compare" button becomes totally useless otherwise). But if you have other changes feel free to push, Bors will kick it out of the queue.

@lukaslueg
Copy link
Contributor Author

yes, unto recent master, due to #140715 (comment)

@tgross35
Copy link
Contributor

Oh, yeah that's kind of an interesting message. It doesn't really matter for small PRs like this as long as there are no conflicts (the "compare" thing I mentioned above isn't either TBH), but 🤷.

So feel free to rebase or not, should be fine either way :)

@lukaslueg
Copy link
Contributor Author

I'm done here

@lukaslueg
Copy link
Contributor Author

@tgross35, you'll need to r+ again, due to the forced push?!

@tgross35
Copy link
Contributor

tgross35 commented Jun 1, 2025

Sorry about that!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 1, 2025

📌 Commit 200d742 has been approved by tgross35

It is now in the queue for this repository.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 2, 2025
…ss35

Clarify &mut-methods' docs on sync::OnceLock

Three small changes to the docs of `sync::OnceLock`:

* The docs for `OnceLock::take()` used to [say](https://doc.rust-lang.org/std/sync/struct.OnceLock.html#method.take) "**Safety** is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt `OnceLock`, as there is (currently) no way to interact with a `OnceLock` in an unsafe way; there are no unsafe methods on `OnceLock`, so there is "safety" guarantee required anywhere. What we simply meant to say is "**Synchronization** is guaranteed...".
* I've add that phrase to the other methods of `OnceLock` which take a `&mut self`, to highlight the fact that having a `&mut OnceLock` guarantees that synchronization with other threads is not required. This is the same as with [`Mutex::get_mut()`](https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut), [`Cell::get_mut()`](https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get_mut), and others.
* In that spirit, the half-sentence "or being initialized" was removed from `get_mut()`, as there is no way that the `OnceLock` is being initialized while we are holding `&mut` to it. Probably a copy&paste from `.get()`
bors added a commit that referenced this pull request Jun 2, 2025
Rollup of 6 pull requests

Successful merges:

 - #140715 (Clarify &mut-methods' docs on sync::OnceLock)
 - #141309 (x86 (32/64): go back to passing SIMD vectors by-ptr)
 - #141677 (Async drop - type instead of async drop fn, fixes #140484)
 - #141733 (C-variadic functions must be unsafe)
 - #141858 (Fix typo in `StructuralPartialEq` docs)
 - #141874 (add f16_epsilon and f128_epsilon diagnostic items)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 3, 2025
…ss35

Clarify &mut-methods' docs on sync::OnceLock

Three small changes to the docs of `sync::OnceLock`:

* The docs for `OnceLock::take()` used to [say](https://doc.rust-lang.org/std/sync/struct.OnceLock.html#method.take) "**Safety** is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt `OnceLock`, as there is (currently) no way to interact with a `OnceLock` in an unsafe way; there are no unsafe methods on `OnceLock`, so there is "safety" guarantee required anywhere. What we simply meant to say is "**Synchronization** is guaranteed...".
* I've add that phrase to the other methods of `OnceLock` which take a `&mut self`, to highlight the fact that having a `&mut OnceLock` guarantees that synchronization with other threads is not required. This is the same as with [`Mutex::get_mut()`](https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut), [`Cell::get_mut()`](https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get_mut), and others.
* In that spirit, the half-sentence "or being initialized" was removed from `get_mut()`, as there is no way that the `OnceLock` is being initialized while we are holding `&mut` to it. Probably a copy&paste from `.get()`
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 3, 2025
…ss35

Clarify &mut-methods' docs on sync::OnceLock

Three small changes to the docs of `sync::OnceLock`:

* The docs for `OnceLock::take()` used to [say](https://doc.rust-lang.org/std/sync/struct.OnceLock.html#method.take) "**Safety** is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt `OnceLock`, as there is (currently) no way to interact with a `OnceLock` in an unsafe way; there are no unsafe methods on `OnceLock`, so there is "safety" guarantee required anywhere. What we simply meant to say is "**Synchronization** is guaranteed...".
* I've add that phrase to the other methods of `OnceLock` which take a `&mut self`, to highlight the fact that having a `&mut OnceLock` guarantees that synchronization with other threads is not required. This is the same as with [`Mutex::get_mut()`](https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut), [`Cell::get_mut()`](https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get_mut), and others.
* In that spirit, the half-sentence "or being initialized" was removed from `get_mut()`, as there is no way that the `OnceLock` is being initialized while we are holding `&mut` to it. Probably a copy&paste from `.get()`
bors added a commit that referenced this pull request Jun 3, 2025
Rollup of 8 pull requests

Successful merges:

 - #140715 (Clarify &mut-methods' docs on sync::OnceLock)
 - #141677 (Async drop - type instead of async drop fn, fixes #140484)
 - #141741 (Overhaul `UsePath`)
 - #141873 (Fixed a typo in `ManuallyDrop`'s doc)
 - #141876 (Don't declare variables in `ExprKind::Let` in invalid positions)
 - #141886 (Add missing 2015 edition directives)
 - #141889 (Add missing `dyn` keywords to tests that do not test for them)
 - #141891 (Fix borrowck mentioning a name from an external macro we (deliberately) don't save)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 55f7571 into rust-lang:master Jun 3, 2025
7 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 3, 2025
@lukaslueg lukaslueg deleted the oncecellsyncdocs branch June 3, 2025 09:53
rust-timer added a commit that referenced this pull request Jun 3, 2025
Rollup merge of #140715 - lukaslueg:oncecellsyncdocs, r=tgross35

Clarify &mut-methods' docs on sync::OnceLock

Three small changes to the docs of `sync::OnceLock`:

* The docs for `OnceLock::take()` used to [say](https://doc.rust-lang.org/std/sync/struct.OnceLock.html#method.take) "**Safety** is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt `OnceLock`, as there is (currently) no way to interact with a `OnceLock` in an unsafe way; there are no unsafe methods on `OnceLock`, so there is "safety" guarantee required anywhere. What we simply meant to say is "**Synchronization** is guaranteed...".
* I've add that phrase to the other methods of `OnceLock` which take a `&mut self`, to highlight the fact that having a `&mut OnceLock` guarantees that synchronization with other threads is not required. This is the same as with [`Mutex::get_mut()`](https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut), [`Cell::get_mut()`](https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get_mut), and others.
* In that spirit, the half-sentence "or being initialized" was removed from `get_mut()`, as there is no way that the `OnceLock` is being initialized while we are holding `&mut` to it. Probably a copy&paste from `.get()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants