Skip to content

Commit 0a6cb07

Browse files
authored
Probe the compiler for LowerExp/UpperExp NonZero* support. (#1464)
This is only supported in Rust 1.84+ so add a check to continue supporting older Rust versions.
1 parent 92466d9 commit 0a6cb07

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ check-cfg = [
259259
'cfg(linux_kernel)',
260260
'cfg(linux_like)',
261261
'cfg(linux_raw)',
262+
'cfg(lower_upper_exp_for_non_zero)',
262263
'cfg(netbsdlike)',
263264
'cfg(rustc_attrs)',
264265
'cfg(solarish)',

build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ fn main() {
7979
use_feature("static_assertions");
8080
}
8181

82+
// `LowerExp`/`UpperExp` for `NonZeroI32` etc.
83+
if has_lower_upper_exp_for_non_zero() {
84+
use_feature("lower_upper_exp_for_non_zero");
85+
}
86+
8287
// WASI support can utilize wasi_ext if present.
8388
if os == "wasi" {
8489
use_feature_or_nothing("wasi_ext");
@@ -188,6 +193,12 @@ fn use_thumb_mode() -> bool {
188193
!can_compile("pub unsafe fn f() { core::arch::asm!(\"udf #16\", in(\"r7\") 0); }")
189194
}
190195

196+
fn has_lower_upper_exp_for_non_zero() -> bool {
197+
// LowerExp/UpperExp for NonZero* were added in Rust 1.84.
198+
// <https://doc.rust-lang.org/stable/std/fmt/trait.LowerExp.html#impl-LowerExp-for-NonZero%3CT%3E>
199+
can_compile("fn a(x: &core::num::NonZeroI32, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::LowerExp::fmt(x, f) }")
200+
}
201+
191202
fn use_feature_or_nothing(feature: &str) {
192203
if has_feature(feature) {
193204
use_feature(feature);

src/pid.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ impl fmt::UpperHex for Pid {
127127
self.0.fmt(f)
128128
}
129129
}
130+
#[cfg(lower_upper_exp_for_non_zero)]
130131
impl fmt::LowerExp for Pid {
131132
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132133
self.0.fmt(f)
133134
}
134135
}
136+
#[cfg(lower_upper_exp_for_non_zero)]
135137
impl fmt::UpperExp for Pid {
136138
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137139
self.0.fmt(f)

0 commit comments

Comments
 (0)