about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2021-03-23 11:04:29 -0700
committerAlex Crichton <alex@alexcrichton.com>2021-04-16 09:21:38 -0700
commitc6eea222a9cafeeeedfa1767232328710b350447 (patch)
tree445d5ae2cf15dca79fe4ac1611e83aedccb08ac6 /src
parentf1ca558db189690a9774713306c94550681ac590 (diff)
downloadrust-c6eea222a9cafeeeedfa1767232328710b350447.tar.gz
rust-c6eea222a9cafeeeedfa1767232328710b350447.zip
std: Add a variant of thread locals with const init
This commit adds a variant of the `thread_local!` macro as a new
`thread_local_const_init!` macro which requires that the initialization
expression is constant (e.g. could be stuck into a `const` if so
desired). This form of thread local allows for a more efficient
implementation of `LocalKey::with` both if the value has a destructor
and if it doesn't. If the value doesn't have a destructor then `with`
should desugar to exactly as-if you use `#[thread_local]` given
sufficient inlining.

The purpose of this new form of thread locals is to precisely be
equivalent to `#[thread_local]` on platforms where possible for values
which fit the bill (those without destructors). This should help close
the gap in performance between `thread_local!`, which is safe, relative
to `#[thread_local]`, which is not easy to use in a portable fashion.
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/feature-gates/thread-local-const-init.rs4
-rw-r--r--src/test/ui/feature-gates/thread-local-const-init.stderr13
2 files changed, 17 insertions, 0 deletions
diff --git a/src/test/ui/feature-gates/thread-local-const-init.rs b/src/test/ui/feature-gates/thread-local-const-init.rs
new file mode 100644
index 00000000000..6584ffa7cf9
--- /dev/null
+++ b/src/test/ui/feature-gates/thread-local-const-init.rs
@@ -0,0 +1,4 @@
+thread_local!(static X: u32 = const { 0 });
+//~^ ERROR: use of unstable library feature 'thread_local_const_init'
+
+fn main() {}
diff --git a/src/test/ui/feature-gates/thread-local-const-init.stderr b/src/test/ui/feature-gates/thread-local-const-init.stderr
new file mode 100644
index 00000000000..a35b18a9c29
--- /dev/null
+++ b/src/test/ui/feature-gates/thread-local-const-init.stderr
@@ -0,0 +1,13 @@
+error[E0658]: use of unstable library feature 'thread_local_const_init'
+  --> $DIR/thread-local-const-init.rs:1:1
+   |
+LL | thread_local!(static X: u32 = const { 0 });
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #84223 <https://github.com/rust-lang/rust/issues/84223> for more information
+   = help: add `#![feature(thread_local_const_init)]` to the crate attributes to enable
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.