Skip to content

Allow elided ('static) lifetimes in thread_local! - #159564

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
zachs18:fix-159358
Sep 26, 2026
Merged

rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
zachs18:fix-159358

Conversation

@zachs18

@zachs18 zachs18 commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

with or without a const initializer, on all platforms.

Lifetime elision on functions includes named lifetimes and 'static in input lifetime positions, so if we give the macro-generated __rust_std_internal_init_fn function an argument mentioning 'static, then elided lifetimes in the return type default to 'static. This uses PhantomData<&'static ()> so that it should probably compile down to nothing (at least in release mode).

Before this change, elided 'static lifetimes were allowed only with const initializers on the "no-threads" and "native" thread_local! implementations, not on the "os" implementation, and not with non-const initializers.

After this change, they are allowed in all thread_local! implementations, with or without a const initializer (A and B both compile on targets with all three thread_local! implementations.)

// Const initializer
std::thread_local!(static A: &str = const { "" });
// Non-const initializer
std::thread_local!(static B: &str = "");
thread_local! implementation const initializer (A) non-const initializer (B)
no-threads (e.g. x86_64-unknown-uefi) ✅️ ❌️ -> ✅️
target_thread_local (e.g. x86_64-unknown-linux-gnu) ✅️ ❌️ -> ✅️
os (e.g. x86_64-pc-windows-gnu) ❌️ -> ✅️ ❌️ -> ✅️

An alternative implementation that would only fix the inconsistency between targets, but not add support for elision with non-const initializers, would be to do this same thing, but only on the os implementation, and only if the initializer is const (i.e. split const initializers to a different macro arm; currently const and non-const initializers generate the same code under the os thread_local! implementation).

Fixes #159538

Fixes #159640 (assuming the non-const-initializer part of this PR is not removed)

with or without a const initializer, on all platforms.
@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 19, 2026
@rustbot

rustbot commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
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 6 candidates

@zachs18

zachs18 commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Question for reviewer: (how) should I add a test for this? I tried adding a test to tests/ui/thread-local with revisions for different targets, but ui tests don't seem to build the stdlib for anything but the current target by default, so it failed with "could not find crate core" on the other two non-x86_64-unknown-linux-gnu targets unless I manually ran with --target x86_64-unknown-uefi or --target x86_64-pc-windows-gnu (in which case that target succeeded and the other non-native target still failed).

attempted UI test
// Test each of the three `thread_local!` implementations,
// that it defaults elided lifetimes in the type to `'static`.
// Regression test for 159358.
//@ check-pass

// no-threads
//@ revisions: x86_64-uefi
//@[x86_64-uefi] compile-flags: --target x86_64-unknown-uefi
//@[x86_64-uefi] needs-llvm-components: x86

// target_thread_local
//@ revisions: x86_64-linux
//@[x86_64-linux] compile-flags: --target x86_64-unknown-linux-gnu
//@[x86_64-linux] needs-llvm-components: x86

// os
//@ revisions: x86_64-windows-gnu
//@[x86_64-windows-gnu] compile-flags: --target x86_64-pc-windows-gnu
//@[x86_64-windows-gnu] needs-llvm-components: x86

// Const initializer, non-Drop
std::thread_local!(static A: &str = const { "" });
// Non-const initializer, non-Drop
std::thread_local!(static B: &str = "");

fn main() {}

(Dropness isn't relevant here)

@zachs18 zachs18 changed the title Allow elided ('static) lifetimes in #[thread_local] Allow elided ('static) lifetimes in thread_local! Jul 19, 2026
@zachs18

zachs18 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot label T-libs-api

I think this needs T-libs-api approval, at least for the non-const-initializer part?

@rustbot rustbot added the T-libs-api [DEPRECATED; DO NOT USE] label Jul 19, 2026
@rust-log-analyzer

This comment has been minimized.

It was testing diagnostics emitted for missing lifetime specifiers in `thread_local!`,
but now elided lifetimes in `thread_local!` are `'static`, so there's nothing to test.
@zachs18

zachs18 commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor Author

For tests: Hmm, if there's at least one tier-1 target that uses each implementation of thread_local!, then just adding the test with no target revisions should work (i.e. the different impl's will get tested in CI), right? Well, there's no Tier 1 "no-threads" target, but x86_64-unknown-linux-gnu ("native") and x86_64-pc-windows-gnu ("os") are both Tier 1, so those will both be tested.

@clarfonthey clarfonthey removed the T-libs-api [DEPRECATED; DO NOT USE] label Aug 12, 2026
@JohnTitor

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned oli-obk and unassigned JohnTitor Aug 15, 2026
@oli-obk

oli-obk commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

r? libs

@rustbot rustbot assigned clarfonthey and unassigned oli-obk Aug 18, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

I'll nominate for today's libs meeting.

@rustbot label:I-libs-nominated

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Aug 18, 2026
@Amanieu

Amanieu commented Aug 18, 2026

Copy link
Copy Markdown
Member

@rfcbot merge libs,libs-api

@rust-rfcbot

rust-rfcbot commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

@Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

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.

@rust-rfcbot rust-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 Aug 18, 2026
@nia-e nia-e removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Aug 24, 2026
@rust-rfcbot rust-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 Sep 15, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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

@clarfonthey clarfonthey added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 16, 2026
Comment on lines +84 to 88
// We intentionally have an argument-position `'static` lifetime so that elided lifetimes in `$t`
// become `'static` like they do for `const`s and `static`s, including in the other two
// `thread_local!` implementations.
#[allow(mismatched_lifetime_syntaxes)]
#[inline]

@RalfJung RalfJung Sep 25, 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.

This comment could use a bit more detail, it's quite hard to understand what is actually going on. I'd suggest discussing the concrete example of $t being &str and why the argument matters for that.

View changes since the review

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.

Fair, it took me a bit as a reviewer to understand too. If @zachs18 wants to try and improve the docs before merge I'd be happy to review, otherwise I don't mind improving the docs in a separate PR.

@RalfJung RalfJung Sep 25, 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.

We still error if the lifetime can't be 'static, right? Is there a test for that?

View changes since the review

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.

Considering the load-bearing:

static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t, ()>

uh, yes, if that accepted anything not-'static I would be quite concerned. But I would be happy to add more tests post-merge if it makes you comfortable. Since beta branches today we'll have an entire release cycle to verify there are no issues.

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.

How's that load-bearing? Seems worth a comment then.

@rust-rfcbot rust-rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Sep 25, 2026
@rust-rfcbot rust-rfcbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. to-announce Announce this issue on triage meeting labels Sep 25, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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.

@clarfonthey

Copy link
Copy Markdown
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 25, 2026
Allow elided ('static) lifetimes in `thread_local!`
@rust-bors

rust-bors Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 5986ef5 (5986ef50c8f1a4e25072bd37f245d83f0e1e04f3)
Base parent: 5ceaf66 (5ceaf6608eb354c2f5bbb3b8d974caa367dac81c)

@clarfonthey

Copy link
Copy Markdown
Contributor

@bors r+ rollup

Hooray! This is finally merged. I will follow up with a PR adding some more docs/tests for @RalfJung since we have a whole release cycle to clarify that.

@rust-bors

rust-bors Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit e423836 has been approved by clarfonthey

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 Sep 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 26, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #150824 (Document platform-specific behavior of `current_exe`, including that Linux can add `" (deleted)"`)
 - #159564 (Allow elided ('static) lifetimes in `thread_local!`)
 - #163026 (hir_typeck: simplify `upvar::determine_capture_info` impl)
 - #163256 (Document `rustc_abi::VariantLayout`)
 - #163366 (Stabilize SyncView)
 - #163376 (Option, Result: not all arguments passed to map_or are eagerly evaluated)
rust-bors Bot pushed a commit that referenced this pull request Sep 26, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #150824 (Document platform-specific behavior of `current_exe`, including that Linux can add `" (deleted)"`)
 - #159564 (Allow elided ('static) lifetimes in `thread_local!`)
 - #163026 (hir_typeck: simplify `upvar::determine_capture_info` impl)
 - #163256 (Document `rustc_abi::VariantLayout`)
 - #163366 (Stabilize SyncView)
 - #163376 (Option, Result: not all arguments passed to map_or are eagerly evaluated)
@rust-bors
rust-bors Bot merged commit d8fb820 into rust-lang:main Sep 26, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Sep 26, 2026
Rollup merge of #159564 - zachs18:fix-159358, r=clarfonthey

Allow elided ('static) lifetimes in `thread_local!`

with or without a const initializer, on all platforms.

Lifetime elision on functions includes named lifetimes and `'static` in input lifetime positions, so if we give the macro-generated `__rust_std_internal_init_fn` function an argument mentioning `'static`, then elided lifetimes in the return type default to `'static`. This uses `PhantomData<&'static ()>` so that it should probably compile down to nothing (at least in release mode).

Before this change, elided `'static` lifetimes were allowed only with `const` initializers on the "no-threads" and "native" `thread_local!` implementations, not on the "os" implementation, and not with non-`const` initializers.

After this change, they are allowed in all `thread_local!` implementations, with or without a `const` initializer (`A` and `B` both compile on targets with all three `thread_local!` implementations.)

```rs
// Const initializer
std::thread_local!(static A: &str = const { "" });
// Non-const initializer
std::thread_local!(static B: &str = "");
```

|  `thread_local!` implementation | `const` initializer (`A`) | non-`const` initializer (`B`) |
| ------------- | ------------- | ---- |
| no-threads (e.g. `x86_64-unknown-uefi`)   | ✅️ | ❌️ -> ✅️ |
| `target_thread_local` (e.g. `x86_64-unknown-linux-gnu`) | ✅️ | ❌️ -> ✅️ |
| os (e.g. `x86_64-pc-windows-gnu`) | ❌️ -> ✅️ | ❌️ -> ✅️ |

An alternative implementation that would only fix the inconsistency between targets, but not add support for elision with non-`const` initializers, would be to do this same thing, but only on the `os` implementation, and only if the initializer is `const` (i.e. split const initializers to a different macro arm; currently const and non-const initializers generate the same code under the `os` `thread_local!` implementation).

Fixes #159538

Fixes #159640 (assuming the non-`const`-initializer part of this PR is not removed)
@rustbot rustbot added this to the 1.101.0 milestone Sep 26, 2026
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-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. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent lifetime elision for thread_local! Decl within thread_local! reports missing lifetime specifiers only on x86_64-pc-windows-gnu

10 participants