Skip to content

Clarify that LocalKey::try_with may return AccessError#158760

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
kpeis695:fix/localkey-try-with-may-return
Jul 8, 2026
Merged

Clarify that LocalKey::try_with may return AccessError#158760
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
kpeis695:fix/localkey-try-with-may-return

Conversation

@kpeis695

@kpeis695 kpeis695 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

The docs for LocalKey::try_with state that the function will return an AccessError when the key has been destroyed. That's not actually guaranteed:

  • LocalKey::with uses try_with internally and its docs explicitly acknowledge that it does not always panic when the destructor has run. If try_with really did detect every destroyed thread-local, with could always panic.
  • The top-level LocalKey docs already note: "On all platforms it's possible for TLS to re-initialize other TLS slots during destruction."

Change will to may to accurately reflect the best-effort nature of the destroyed-key check.

Closes #157889

@rustbot label +A-docs

The docs stated that try_with will return an AccessError when the key
has been destroyed. This is not guaranteed: on some platforms, TLS
slots can be re-initialized during destruction, so the destroyed state
cannot always be observed. Change 'will' to 'may' to accurately reflect
the best-effort nature of the check.
@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 Jul 4, 2026
@rustbot

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @aapoalas (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
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

@rustbot rustbot added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Jul 4, 2026

@aapoalas aapoalas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thank you <3

@bors r+

View changes since this review

@rust-bors

rust-bors Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

📌 Commit b51d75e has been approved by aapoalas

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Jul 7, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 7, 2026
…-return, r=aapoalas

Clarify that `LocalKey::try_with` may return `AccessError`

The docs for `LocalKey::try_with` state that the function *will* return an `AccessError` when the key has been destroyed. That's not actually guaranteed:

- `LocalKey::with` uses `try_with` internally and its docs explicitly acknowledge that it does not always panic when the destructor has run. If `try_with` really did detect every destroyed thread-local, `with` could always panic.
- The top-level `LocalKey` docs already note: "On all platforms it's possible for TLS to re-initialize other TLS slots during destruction."

Change *will* to *may* to accurately reflect the best-effort nature of the destroyed-key check.

Closes rust-lang#157889

@rustbot label +A-docs
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 7, 2026
…-return, r=aapoalas

Clarify that `LocalKey::try_with` may return `AccessError`

The docs for `LocalKey::try_with` state that the function *will* return an `AccessError` when the key has been destroyed. That's not actually guaranteed:

- `LocalKey::with` uses `try_with` internally and its docs explicitly acknowledge that it does not always panic when the destructor has run. If `try_with` really did detect every destroyed thread-local, `with` could always panic.
- The top-level `LocalKey` docs already note: "On all platforms it's possible for TLS to re-initialize other TLS slots during destruction."

Change *will* to *may* to accurately reflect the best-effort nature of the destroyed-key check.

Closes rust-lang#157889

@rustbot label +A-docs
rust-bors Bot pushed a commit that referenced this pull request Jul 7, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #157385 (Enable Enzyme on x86_64-apple)
 - #157561 (rustdoc: do not include extra stuff in span)
 - #158179 (std: unconditionally use `preadv`/`pwritev` on AArch64 macOS)
 - #158617 (allow mGCA const arguments to fall back to anon consts)
 - #158621 (disallow `extern "custom"` on wasm and spirv targets)
 - #158690 (delegation: support mapping of all arguments with `Self` type)
 - #158696 (Rename some `body_id` to `body_def_id`)
 - #158697 (Fixes for QNX SDP 8)
 - #158760 (Clarify that `LocalKey::try_with` may return `AccessError`)
 - #157801 (Rewrite safety requirements for `Allocator` impls)
 - #158333 (Fix typetree generation for differentiated functions)
 - #158646 (powerpc64le_unknown_freebsd.rs: link with -lgcc)
 - #158701 ( diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch)
 - #158791 (Avoid unused braces lint for macro generated arguments)
 - #158802 (Use `ci-mirrors` in `armhf-gnu` for {busybox, ubuntu rootfs} artifacts)
 - #158841 (Avoid final override ICE for RPITIT associated types)
 - #158889 (tests: catch up with LLVM returning f128 on the stack)
 - #158905 (delegation: add constraints to new generic args)
rust-bors Bot pushed a commit that referenced this pull request Jul 7, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #156016 (view-types: store view types in the AST)
 - #157385 (Enable Enzyme on x86_64-apple)
 - #158179 (std: unconditionally use `preadv`/`pwritev` on AArch64 macOS)
 - #158621 (disallow `extern "custom"` on wasm and spirv targets)
 - #158690 (delegation: support mapping of all arguments with `Self` type)
 - #158696 (Rename some `body_id` to `body_def_id`)
 - #158697 (Fixes for QNX SDP 8)
 - #158760 (Clarify that `LocalKey::try_with` may return `AccessError`)
 - #157801 (Rewrite safety requirements for `Allocator` impls)
 - #158085 (rustdoc: Fix sidebar heading order)
 - #158333 (Fix typetree generation for differentiated functions)
 - #158646 (powerpc64le_unknown_freebsd.rs: link with -lgcc)
 - #158701 ( diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch)
 - #158791 (Avoid unused braces lint for macro generated arguments)
 - #158802 (Use `ci-mirrors` in `armhf-gnu` for {busybox, ubuntu rootfs} artifacts)
 - #158841 (Avoid final override ICE for RPITIT associated types)
 - #158889 (tests: catch up with LLVM returning f128 on the stack)
 - #158905 (delegation: add constraints to new generic args)
 - #158922 (tests: clean up over-constraint on LLVM feature count)

Failed merges:

 - #158617 (allow mGCA const arguments to fall back to anon consts)
@rust-bors rust-bors Bot merged commit c1d608b into rust-lang:main Jul 8, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 8, 2026
rust-timer added a commit that referenced this pull request Jul 8, 2026
Rollup merge of #158760 - kpeis695:fix/localkey-try-with-may-return, r=aapoalas

Clarify that `LocalKey::try_with` may return `AccessError`

The docs for `LocalKey::try_with` state that the function *will* return an `AccessError` when the key has been destroyed. That's not actually guaranteed:

- `LocalKey::with` uses `try_with` internally and its docs explicitly acknowledge that it does not always panic when the destructor has run. If `try_with` really did detect every destroyed thread-local, `with` could always panic.
- The top-level `LocalKey` docs already note: "On all platforms it's possible for TLS to re-initialize other TLS slots during destruction."

Change *will* to *may* to accurately reflect the best-effort nature of the destroyed-key check.

Closes #157889

@rustbot label +A-docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools 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.

LocalKey::try_with error is incorrectly documented to be guaranteed

3 participants