Skip to content

thread::LocalKey: document meaning of 'inner' function argument - #163398

Open
RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:thread-local-key-inner
Open

RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:thread-local-key-inner

Conversation

@RalfJung

Copy link
Copy Markdown
Member

#92123 gave this function an argument, but without documenting what the argument means or does. I hope I reverse engineered this correctly.

Cc @m-ou-se @joboet

@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 Sep 27, 2026
@rustbot

rustbot commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

rustbot has assigned @clarfonthey.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 7 candidates

Comment thread library/std/src/thread/local.rs Outdated
// thread: if it is not yet initialized, and if the argument is `Some(&mut Some(val))`, the
// inner `val` should be `take`en out and used as the initial value instead of the default. This
// is purely an optimization for the case where the value will be immediately overwritten; it is
// okay for `inner` to always ignore `init`.

@joboet joboet Sep 27, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's not always true, this only applies in the case of const-initialisers. In other cases, init must not be ignored as LocalKey::set guarantees that it will succeed even if the initialiser panics.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah! The contract is quite subtle then. I updated the comment.

FWIW, if the destructor of the default value panics, that could still cause LocalKey::set to panic.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, true. This seems almost impossible to prevent, and such behaviour isn't that surprising – but we should probably document it...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well we could prevent the unwind part of that panic with rust-lang/rfcs#3288... but some sort of failure (like abort) cannot really be prevented, yeah. It's also normal in Rust that seting a value drops the old value which can fail.

@RalfJung
RalfJung force-pushed the thread-local-key-inner branch from 7280921 to c9c2f66 Compare September 27, 2026 09:40
@RalfJung
RalfJung force-pushed the thread-local-key-inner branch from c9c2f66 to e62fd11 Compare September 27, 2026 09:43
@clarfonthey

Copy link
Copy Markdown
Contributor

r? joboet since you seem to understand this better than I do. I'd just be guessing like Ralf is.

@rustbot rustbot assigned joboet and unassigned clarfonthey Sep 29, 2026
@rustbot

rustbot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

joboet is currently at their maximum review capacity.
They may take a while to respond.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants