-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Allow any const
expression blocks in thread_local!
#116392
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
@rustbot label +T-libs-api +A-thread-locals +A-macros -T-libs |
thread_local!
const
expression blocks in thread_local!
r? @m-ou-se for T-libs-api FCP since this is a user-facing stable API change. |
/// pub static FOO: Cell<u32> = const { | ||
/// let value = 1; | ||
/// Cell::new(value) | ||
/// }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to have a test that checks this works, but I don't think we should make the documentation example more verbose/complicated than necessary.
@rfcbot merge Summary: // Allowed today:
thread_local! {
pub static A: Cell<u32> = const { Cell::new(value) };
}
// Disallowed today, but allowed after this PR:
thread_local! {
pub static B: Cell<u32> = const {
let value = 1;
Cell::new(value)
};
} |
Team member @m-ou-se 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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
LGTM. I hope this won't have an unexpected interaction with #![allow(incomplete_features)]
#![feature(const_closures)]
#![feature(type_alias_impl_trait)]
type Closure = impl Fn();
thread_local! {
pub static X: Closure = const || {};
} @rfcbot reviewed |
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. |
@rustbot author |
@nvzqz this is waiting on #116392 (review) to be addressed. |
Allow any `const` expression blocks in `thread_local!` This PR contains a rebase of the macro change from rust-lang#116392, together with adding a test under library/std/tests. Testing this feature by making the documentation's example code needlessly more complicated was not appropriate as pointed out in rust-lang#116392 (review). Without the macro change, this new test would fail to build as follows: ```console error: no rules expected the token `let` --> library/std/tests/thread.rs:26:13 | 26 | let value = 1; | ^^^ no rules expected this token in macro call | note: while trying to match meta-variable `$init:expr` --> library/std/src/thread/local.rs:189:69 | 189 | ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => ( | ^^^^^^^^^^ ``` Closes rust-lang#116392.
Allow any `const` expression blocks in `thread_local!` This PR contains a rebase of the macro change from rust-lang#116392, together with adding a test under library/std/tests. Testing this feature by making the documentation's example code needlessly more complicated was not appropriate as pointed out in rust-lang#116392 (review). Without the macro change, this new test would fail to build as follows: ```console error: no rules expected the token `let` --> library/std/tests/thread.rs:26:13 | 26 | let value = 1; | ^^^ no rules expected this token in macro call | note: while trying to match meta-variable `$init:expr` --> library/std/src/thread/local.rs:189:69 | 189 | ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => ( | ^^^^^^^^^^ ``` Closes rust-lang#116392.
Rollup merge of rust-lang#120181 - dtolnay:tlconst, r=thomcc Allow any `const` expression blocks in `thread_local!` This PR contains a rebase of the macro change from rust-lang#116392, together with adding a test under library/std/tests. Testing this feature by making the documentation's example code needlessly more complicated was not appropriate as pointed out in rust-lang#116392 (review). Without the macro change, this new test would fail to build as follows: ```console error: no rules expected the token `let` --> library/std/tests/thread.rs:26:13 | 26 | let value = 1; | ^^^ no rules expected this token in macro call | note: while trying to match meta-variable `$init:expr` --> library/std/src/thread/local.rs:189:69 | 189 | ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => ( | ^^^^^^^^^^ ``` Closes rust-lang#116392.
Previously only a single expression could be allowed within the
const
block. This change allows for statements to precede the final expression, similarly to real inlineconst
expressions.This works by taking
$init
as ablock
inthread_local!
and passing it tothread_local_inner!
, where it is accepted as anexpr
.