11use 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+
313fn 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}
0 commit comments