Skip to content

Commit

Permalink
chore: fix version detection in build script (#4860)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Jul 25, 2022
1 parent b343767 commit b673eae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
41 changes: 38 additions & 3 deletions tokio/build.rs
@@ -1,11 +1,38 @@
use autocfg::AutoCfg;

const CONST_THREAD_LOCAL_PROBE: &str = r#"
{
thread_local! {
static MY_PROBE: usize = const { 10 };
}
MY_PROBE.with(|val| *val)
}
"#;

fn main() {
let mut enable_const_thread_local = false;

match AutoCfg::new() {
Ok(ac) => {
// Const-initialized thread locals were stabilized in 1.59
if ac.probe_rustc_version(1, 59) {
autocfg::emit("tokio_const_thread_local")
// These checks prefer to call only `probe_rustc_version` if that is
// enough to determine whether the feature is supported. This is
// because the `probe_expression` call involves a call to rustc,
// which the `probe_rustc_version` call avoids.

// Const-initialized thread locals were stabilized in 1.59.
if ac.probe_rustc_version(1, 60) {
enable_const_thread_local = true;
} else if ac.probe_rustc_version(1, 59) {
// This compiler claims to be 1.59, but there are some nightly
// compilers that claim to be 1.59 without supporting the
// feature. Explicitly probe to check if code using them
// compiles.
//
// The oldest nightly that supports the feature is 2021-12-06.
if ac.probe_expression(CONST_THREAD_LOCAL_PROBE) {
enable_const_thread_local = true;
}
}
}

Expand All @@ -19,4 +46,12 @@ fn main() {
);
}
}

if !enable_const_thread_local {
// To disable this feature on compilers that support it, you can
// explicitly pass this flag with the following environment variable:
//
// RUSTFLAGS="--cfg tokio_no_const_thread_local"
autocfg::emit("tokio_no_const_thread_local")
}
}
6 changes: 4 additions & 2 deletions tokio/src/macros/thread_local.rs
Expand Up @@ -10,12 +10,14 @@ macro_rules! thread_local {
($($tts:tt)+) => { loom::thread_local!{ $($tts)+ } }
}

#[cfg(all(tokio_const_thread_local, not(all(loom, test))))]
#[cfg(not(tokio_no_const_thread_local))]
#[cfg(not(all(loom, test)))]
macro_rules! thread_local {
($($tts:tt)+) => { ::std::thread_local!{ $($tts)+ } }
}

#[cfg(all(not(tokio_const_thread_local), not(all(loom, test))))]
#[cfg(tokio_no_const_thread_local)]
#[cfg(not(all(loom, test)))]
macro_rules! thread_local {
($(#[$attrs:meta])* $vis:vis static $name:ident: $ty:ty = const { $expr:expr } $(;)?) => {
::std::thread_local! {
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/task/task_local.rs
Expand Up @@ -47,7 +47,7 @@ macro_rules! task_local {
}

#[doc(hidden)]
#[cfg(tokio_const_thread_local)]
#[cfg(not(tokio_no_const_thread_local))]
#[macro_export]
macro_rules! __task_local_inner {
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty) => {
Expand All @@ -62,7 +62,7 @@ macro_rules! __task_local_inner {
}

#[doc(hidden)]
#[cfg(not(tokio_const_thread_local))]
#[cfg(tokio_no_const_thread_local)]
#[macro_export]
macro_rules! __task_local_inner {
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty) => {
Expand Down

0 comments on commit b673eae

Please sign in to comment.