Skip to content

Commit dc65255

Browse files
authored
Use cfg_select! (RustPython#7636)
1 parent a9fd4bf commit dc65255

22 files changed

Lines changed: 138 additions & 162 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ rustpython-pylib = { workspace = true, optional = true }
3636
rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] }
3737
rustpython-vm = { workspace = true, features = ["compiler", "gc"] }
3838

39-
cfg-if = { workspace = true }
4039
log = { workspace = true }
4140
flame = { workspace = true, optional = true }
4241

@@ -136,7 +135,7 @@ exclude = ["pymath"]
136135
version = "0.5.0"
137136
authors = ["RustPython Team"]
138137
edition = "2024"
139-
rust-version = "1.94.0"
138+
rust-version = "1.95.0"
140139
repository = "https://github.com/RustPython/RustPython"
141140
license = "MIT"
142141

@@ -177,7 +176,6 @@ ascii = "1.1"
177176
bitflags = "2.11.0"
178177
bitflagset = "0.0.3"
179178
bstr = "1"
180-
cfg-if = "1.0"
181179
chrono = { version = "0.4.44", default-features = false, features = ["clock", "oldtime", "std"] }
182180
constant_time_eq = "0.4"
183181
criterion = { version = "0.8", features = ["html_reports"] }

crates/common/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ rustpython-wtf8 = { workspace = true }
2020

2121
ascii = { workspace = true }
2222
bitflags = { workspace = true }
23-
cfg-if = { workspace = true }
2423
getrandom = { workspace = true }
2524
itertools = { workspace = true }
2625
libc = { workspace = true }

crates/common/src/lock.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use lock_api::{
66
RwLockReadGuard, RwLockUpgradableReadGuard, RwLockWriteGuard,
77
};
88

9-
cfg_if::cfg_if! {
10-
if #[cfg(feature = "threading")] {
9+
cfg_select! {
10+
feature = "threading" => {
1111
pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};
12-
1312
pub use std::sync::OnceLock as OnceCell;
1413
pub use core::cell::LazyCell;
15-
} else {
14+
}
15+
_ => {
1616
mod cell_lock;
1717
pub use cell_lock::{RawCellMutex as RawMutex, RawCellRwLock as RawRwLock, SingleThreadId as RawThreadId};
1818

@@ -23,10 +23,11 @@ cfg_if::cfg_if! {
2323
// LazyLock: uses std::sync::LazyLock when std is available (even without
2424
// threading, because Rust test runner uses parallel threads).
2525
// Without std, uses a LazyCell wrapper (truly single-threaded only).
26-
cfg_if::cfg_if! {
27-
if #[cfg(any(feature = "threading", feature = "std"))] {
26+
cfg_select! {
27+
any(feature = "threading", feature = "std") => {
2828
pub use std::sync::LazyLock;
29-
} else {
29+
}
30+
_ => {
3031
pub struct LazyLock<T, F = fn() -> T>(core::cell::LazyCell<T, F>);
3132
// SAFETY: This branch is only active when both "std" and "threading"
3233
// features are absent — i.e., truly single-threaded no_std environments

crates/stdlib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ ruff_source_file = { workspace = true }
3939

4040
ahash = { workspace = true }
4141
ascii = { workspace = true }
42-
cfg-if = { workspace = true }
4342
crossbeam-utils = { workspace = true }
4443
flame = { workspace = true, optional = true }
4544
hex = { workspace = true }

crates/stdlib/src/_sqlite3.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,14 +2819,15 @@ mod _sqlite3 {
28192819
db: *mut sqlite3,
28202820
}
28212821

2822-
cfg_if::cfg_if! {
2823-
if #[cfg(feature = "threading")] {
2822+
cfg_select! {
2823+
feature = "threading" => {
28242824
unsafe impl Send for SqliteStatement {}
28252825
// unsafe impl Sync for SqliteStatement {}
28262826
unsafe impl Send for Sqlite {}
28272827
// unsafe impl Sync for Sqlite {}
28282828
unsafe impl Send for SqliteBlob {}
28292829
}
2830+
_ => {}
28302831
}
28312832

28322833
impl From<SqliteStatementRaw> for SqliteRaw {

crates/stdlib/src/openssl.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ mod cert;
77
mod ssl_error;
88

99
// Conditional compilation for OpenSSL version-specific error codes
10-
cfg_if::cfg_if! {
11-
if #[cfg(ossl310)] {
12-
// OpenSSL 3.1.0+
10+
cfg_select! {
11+
// OpenSSL 3.1.0+
12+
ossl310 => {
1313
mod ssl_data_31;
1414
use ssl_data_31 as ssl_data;
15-
} else if #[cfg(ossl300)] {
16-
// OpenSSL 3.0.0+
15+
}
16+
// OpenSSL 3.0.0+
17+
ossl300 => {
1718
mod ssl_data_300;
1819
use ssl_data_300 as ssl_data;
19-
} else {
20-
// OpenSSL 1.1.1+ (fallback)
20+
}
21+
// OpenSSL 1.1.1+ (fallback)
22+
_ => {
2123
mod ssl_data_111;
2224
use ssl_data_111 as ssl_data;
2325
}
@@ -30,13 +32,10 @@ use rustpython_common::lock::LazyLock;
3032

3133
// define our own copy of ProbeResult so we can handle the vendor case
3234
// easily, without having to have a bunch of cfgs
33-
cfg_if::cfg_if! {
34-
if #[cfg(openssl_vendored)] {
35-
static PROBE: LazyLock<ProbeResult> = LazyLock::new(openssl_probe::probe);
36-
} else {
37-
static PROBE: LazyLock<ProbeResult> = LazyLock::new(|| ProbeResult { cert_file: None, cert_dir: vec![] });
38-
}
39-
}
35+
static PROBE: LazyLock<ProbeResult> = cfg_select! {
36+
openssl_vendored => LazyLock::new(openssl_probe::probe)
37+
_ => LazyLock::new(|| ProbeResult { cert_file: None, cert_dir: vec![] })
38+
};
4039

4140
fn probe() -> &'static ProbeResult {
4241
&PROBE
@@ -1349,13 +1348,14 @@ mod _ssl {
13491348

13501349
#[pymethod]
13511350
fn set_default_verify_paths(&self, vm: &VirtualMachine) -> PyResult<()> {
1352-
cfg_if::cfg_if! {
1353-
if #[cfg(openssl_vendored)] {
1351+
cfg_select! {
1352+
openssl_vendored => {
13541353
let (cert_file, cert_dir) = get_cert_file_dir();
13551354
self.builder()
13561355
.load_verify_locations(Some(cert_file), Some(cert_dir))
13571356
.map_err(|e| convert_openssl_error(vm, e))
1358-
} else {
1357+
}
1358+
_ => {
13591359
self.builder()
13601360
.set_default_verify_paths()
13611361
.map_err(|e| convert_openssl_error(vm, e))

crates/stdlib/src/resource.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ mod resource {
1212
use core::mem;
1313
use std::io;
1414

15-
cfg_if::cfg_if! {
16-
if #[cfg(target_os = "android")] {
17-
#[expect(deprecated)]
18-
const RLIM_NLIMITS: i32 = libc::RLIM_NLIMITS;
19-
} else {
15+
#[cfg_attr(target_os = "android", expect(deprecated))]
16+
const RLIM_NLIMITS: i32 = cfg_select! {
17+
target_os = "android" => {
18+
libc::RLIM_NLIMITS
19+
}
20+
_ => {
2021
// This constant isn't abi-stable across os versions, so we just
2122
// pick a high number so we don't get false positive ValueErrors and just bubble up the
2223
// EINVAL that get/setrlimit return on an invalid resource
23-
const RLIM_NLIMITS: i32 = 256;
24+
256
2425
}
25-
}
26+
};
2627

2728
// TODO: RLIMIT_OFILE,
2829
#[pyattr]

crates/stdlib/src/socket.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,20 +1568,23 @@ mod _socket {
15681568
if socket_kind == -1 {
15691569
socket_kind = sock.r#type().map_err(|e| e.into_pyexception(vm))?.into();
15701570
}
1571-
cfg_if::cfg_if! {
1572-
if #[cfg(any(
1571+
1572+
cfg_select! {
1573+
any(
15731574
target_os = "android",
15741575
target_os = "freebsd",
15751576
target_os = "fuchsia",
15761577
target_os = "linux",
1577-
))] {
1578+
) => {
15781579
if proto == -1 {
15791580
proto = sock.protocol()?.map_or(0, Into::into);
15801581
}
1581-
} else {
1582+
}
1583+
_ => {
15821584
proto = 0;
15831585
}
15841586
}
1587+
15851588
return Ok(zelf.init_inner(family, socket_kind, proto, sock)?);
15861589
}
15871590

@@ -3259,14 +3262,9 @@ mod _socket {
32593262
}
32603263

32613264
fn sock_from_raw(fileno: RawSocket, vm: &VirtualMachine) -> PyResult<Socket> {
3262-
let invalid = {
3263-
cfg_if::cfg_if! {
3264-
if #[cfg(windows)] {
3265-
fileno == INVALID_SOCKET
3266-
} else {
3267-
fileno < 0
3268-
}
3269-
}
3265+
let invalid = cfg_select! {
3266+
windows => fileno == INVALID_SOCKET,
3267+
_ => fileno < 0
32703268
};
32713269
if invalid {
32723270
return Err(vm.new_value_error("negative file descriptor"));

crates/vm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ ascii = { workspace = true }
4747
ahash = { workspace = true }
4848
bitflags = { workspace = true }
4949
bstr = { workspace = true }
50-
cfg-if = { workspace = true }
5150
crossbeam-utils = { workspace = true }
5251
chrono = { workspace = true }
5352
constant_time_eq = { workspace = true }

0 commit comments

Comments
 (0)