Skip to content

Commit b673eae

Browse files
authored
chore: fix version detection in build script (#4860)
1 parent b343767 commit b673eae

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

tokio/build.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
use autocfg::AutoCfg;
22

3+
const CONST_THREAD_LOCAL_PROBE: &str = r#"
4+
{
5+
thread_local! {
6+
static MY_PROBE: usize = const { 10 };
7+
}
8+
9+
MY_PROBE.with(|val| *val)
10+
}
11+
"#;
12+
313
fn main() {
14+
let mut enable_const_thread_local = false;
15+
416
match AutoCfg::new() {
517
Ok(ac) => {
6-
// Const-initialized thread locals were stabilized in 1.59
7-
if ac.probe_rustc_version(1, 59) {
8-
autocfg::emit("tokio_const_thread_local")
18+
// These checks prefer to call only `probe_rustc_version` if that is
19+
// enough to determine whether the feature is supported. This is
20+
// because the `probe_expression` call involves a call to rustc,
21+
// which the `probe_rustc_version` call avoids.
22+
23+
// Const-initialized thread locals were stabilized in 1.59.
24+
if ac.probe_rustc_version(1, 60) {
25+
enable_const_thread_local = true;
26+
} else if ac.probe_rustc_version(1, 59) {
27+
// This compiler claims to be 1.59, but there are some nightly
28+
// compilers that claim to be 1.59 without supporting the
29+
// feature. Explicitly probe to check if code using them
30+
// compiles.
31+
//
32+
// The oldest nightly that supports the feature is 2021-12-06.
33+
if ac.probe_expression(CONST_THREAD_LOCAL_PROBE) {
34+
enable_const_thread_local = true;
35+
}
936
}
1037
}
1138

@@ -19,4 +46,12 @@ fn main() {
1946
);
2047
}
2148
}
49+
50+
if !enable_const_thread_local {
51+
// To disable this feature on compilers that support it, you can
52+
// explicitly pass this flag with the following environment variable:
53+
//
54+
// RUSTFLAGS="--cfg tokio_no_const_thread_local"
55+
autocfg::emit("tokio_no_const_thread_local")
56+
}
2257
}

tokio/src/macros/thread_local.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ macro_rules! thread_local {
1010
($($tts:tt)+) => { loom::thread_local!{ $($tts)+ } }
1111
}
1212

13-
#[cfg(all(tokio_const_thread_local, not(all(loom, test))))]
13+
#[cfg(not(tokio_no_const_thread_local))]
14+
#[cfg(not(all(loom, test)))]
1415
macro_rules! thread_local {
1516
($($tts:tt)+) => { ::std::thread_local!{ $($tts)+ } }
1617
}
1718

18-
#[cfg(all(not(tokio_const_thread_local), not(all(loom, test))))]
19+
#[cfg(tokio_no_const_thread_local)]
20+
#[cfg(not(all(loom, test)))]
1921
macro_rules! thread_local {
2022
($(#[$attrs:meta])* $vis:vis static $name:ident: $ty:ty = const { $expr:expr } $(;)?) => {
2123
::std::thread_local! {

tokio/src/task/task_local.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ macro_rules! task_local {
4747
}
4848

4949
#[doc(hidden)]
50-
#[cfg(tokio_const_thread_local)]
50+
#[cfg(not(tokio_no_const_thread_local))]
5151
#[macro_export]
5252
macro_rules! __task_local_inner {
5353
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty) => {
@@ -62,7 +62,7 @@ macro_rules! __task_local_inner {
6262
}
6363

6464
#[doc(hidden)]
65-
#[cfg(not(tokio_const_thread_local))]
65+
#[cfg(tokio_no_const_thread_local)]
6666
#[macro_export]
6767
macro_rules! __task_local_inner {
6868
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty) => {

0 commit comments

Comments
 (0)