Skip to content

Use cfg_select! macro instead of cfg_if!#7636

Merged
youknowone merged 2 commits into
RustPython:mainfrom
ShaharNaveh:cfg-select-macro
Apr 21, 2026
Merged

Use cfg_select! macro instead of cfg_if!#7636
youknowone merged 2 commits into
RustPython:mainfrom
ShaharNaveh:cfg-select-macro

Conversation

@ShaharNaveh
Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh commented Apr 20, 2026

Since 1.95 cfg_select! macro is stabilized

https://doc.rust-lang.org/stable/std/macro.cfg_select.html

Summary by CodeRabbit

  • New Requirements

    • Minimum supported Rust version raised to 1.95.0.
  • Chores

    • Removed an internal build-time dependency and modernized compile-time condition handling across the codebase.
  • Clarification

    • No changes to public APIs, runtime behavior, or platform-identifying constants; no user-facing functionality was altered.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 14c68ac5-93be-4151-b777-2ddf8a8913e9

📥 Commits

Reviewing files that changed from the base of the PR and between f446b28 and b4e8da6.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (1)
  • crates/stdlib/Cargo.toml
💤 Files with no reviewable changes (1)
  • crates/stdlib/Cargo.toml

📝 Walkthrough

Walkthrough

Removed cfg-if workspace dependency and replaced multiple cfg_if! conditional blocks with cfg_select! across crates; bumped workspace rust-version from 1.93.0 to 1.95.0. No public API signatures or runtime behavior were changed.

Changes

Cohort / File(s) Summary
Workspace manifests
Cargo.toml, crates/common/Cargo.toml, crates/stdlib/Cargo.toml, crates/vm/Cargo.toml
Removed cfg-if dependency entries from manifests; updated root/workspace rust-version from 1.93.01.95.0.
Common lock logic
crates/common/src/lock.rs
Replaced cfg_if! with cfg_select! when selecting RawMutex/RawRwLock/RawThreadId and LazyLock implementation; no API changes.
Stdlib: OpenSSL & resource
crates/stdlib/src/openssl.rs, crates/stdlib/src/resource.rs
Switched OpenSSL module selection, PROBE/set_default_verify_paths, and RLIM_NLIMITS constant selection from cfg_if! to cfg_select! (Android #[expect(deprecated)] preserved).
Stdlib: socket
crates/stdlib/src/socket.rs
Converted platform conditionals for proto selection and fileno invalid checks to cfg_select!; behavior unchanged.
VM manifests & concurrency impls
crates/vm/Cargo.toml, crates/vm/src/builtins/type.rs
Removed cfg-if from vm manifest; replaced cfg_if! with cfg_select! to gate unsafe impl Send/Sync for PyType.
VM macros
crates/vm/src/macros.rs
Unified py_dyn_fn! macro and used cfg_select! inside expansion to choose trait-object bounds (adds Send + Sync only when feature = "threading").
VM object types
crates/vm/src/object/core.rs, crates/vm/src/object/ext.rs, crates/vm/src/object/payload.rs
Rewrote multiple cfg_if!-guarded unsafe impl Send/Sync blocks and PyThreadingConstraint definition to use cfg_select!; semantics preserved.
VM stdlib modules
crates/vm/src/stdlib/_io.rs, crates/vm/src/stdlib/_signal.rs, crates/vm/src/stdlib/_thread.rs, crates/vm/src/stdlib/os.rs, crates/vm/src/stdlib/posix.rs, crates/vm/src/stdlib/sys.rs
Replaced many platform/feature cfg_if! branches with cfg_select! for constants, type selections, and platform strings (e.g., sys::PLATFORM now via cfg_select!; "macos""darwin", "windows""win32").
Stdlib sqlite3/threading impls
crates/stdlib/src/_sqlite3.rs
Replaced cfg_if! with cfg_select! around unsafe impl Send trait implementations; added explicit fallback arm.
Shell helper
src/shell/helper.rs
Replaced cfg_if! controlling rustyline trait impls with cfg_select!, adding an explicit fallback arm for wasm32.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped through cfg-if's tangled lair,
swapped branches neat with cfg_select flair.
Same shapes, same bounds, one tiny version leap—
I nibble old macros, then cuddle up to sleep. 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: replacing cfg_if! conditional compilation with cfg_select! macro throughout the codebase.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ShaharNaveh ShaharNaveh marked this pull request as draft April 20, 2026 13:16
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
crates/vm/src/stdlib/posix.rs (1)

1805-1820: ⚠️ Potential issue | 🔴 Critical

Remove the stray if from the cfg_select! arm.

Line 1807 uses if any(...) =>, but Rust 1.95 cfg_select! arms use the cfg predicate directly without the if keyword (any(...) =>). This is a compile-blocking syntax error. All other cfg_select! usages in the codebase follow the correct syntax without if.

🐛 Proposed fix
                 // Note: POSIX_SPAWN_SETSID may not be available on all platforms
                 cfg_select! {
-                    if any(
+                    any(
                         target_os = "linux",
                         target_os = "haiku",
                         target_os = "solaris",
                         target_os = "illumos",
                         target_os = "hurd",
                     ) => {
                         flags.insert(nix::spawn::PosixSpawnFlags::from_bits_retain(libc::POSIX_SPAWN_SETSID));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/posix.rs` around lines 1805 - 1820, The cfg_select! arm
in the flags handling block incorrectly uses the `if` keyword ("if any(...) =>")
which is invalid syntax; edit the cfg_select! invocation (the block that inserts
nix::spawn::PosixSpawnFlags::from_bits_retain(libc::POSIX_SPAWN_SETSID) and
returns a new_not_implemented_error) to remove the stray `if` so the arm uses
the predicate directly (e.g., `any(...) =>`), leaving the flags.insert(...) and
the `_ => { return Err(vm.new_not_implemented_error(...)); }` arm unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/stdlib/src/openssl.rs`:
- Around line 35-42: The PROBE static initializer's cfg_select! branches end
with trailing semicolons making each branch evaluate to () instead of returning
a LazyLock<ProbeResult>; remove the semicolons after
LazyLock::new(openssl_probe::probe) and LazyLock::new(|| ProbeResult {
cert_file: None, cert_dir: vec![] }) so each branch yields a
LazyLock<ProbeResult> (affecting the PROBE static, the cfg_select! macro,
LazyLock::new invocations, and the ProbeResult construction).

In `@crates/vm/src/stdlib/_thread.rs`:
- Around line 34-39: The cfg_select! arms for PYTHREAD_NAME currently list the
broader unix => Some("pthread") arm before the more specific any(target_os =
"solaris", target_os = "illumos") arm, so Solaris/illumos targets never reach
their intended match; fix by reordering the cfg_select! arms in the
PYTHREAD_NAME constant so the any(target_os = "solaris", target_os = "illumos")
arm appears before the unix arm, preserving the other arms (windows and default)
as-is.

In `@crates/vm/src/stdlib/sys.rs`:
- Around line 248-255: The PLATFORM constant in sys.rs currently maps target_os
"macos" and Windows to non-CPython values; update the cfg_select! mapping in the
pub const PLATFORM definition so that target_os = "macos" => "darwin" and
windows => "win32" (leave the other arms unchanged) to match CPython's
sys.platform values; modify the PLATFORM const in crates/vm/src/stdlib/sys.rs
accordingly and run the build/tests to verify.

---

Outside diff comments:
In `@crates/vm/src/stdlib/posix.rs`:
- Around line 1805-1820: The cfg_select! arm in the flags handling block
incorrectly uses the `if` keyword ("if any(...) =>") which is invalid syntax;
edit the cfg_select! invocation (the block that inserts
nix::spawn::PosixSpawnFlags::from_bits_retain(libc::POSIX_SPAWN_SETSID) and
returns a new_not_implemented_error) to remove the stray `if` so the arm uses
the predicate directly (e.g., `any(...) =>`), leaving the flags.insert(...) and
the `_ => { return Err(vm.new_not_implemented_error(...)); }` arm unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 3c4d5dab-4eac-460c-b598-031fda07a667

📥 Commits

Reviewing files that changed from the base of the PR and between af41d11 and 67f5ebc.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (19)
  • Cargo.toml
  • crates/common/Cargo.toml
  • crates/common/src/lock.rs
  • crates/stdlib/Cargo.toml
  • crates/stdlib/src/openssl.rs
  • crates/stdlib/src/resource.rs
  • crates/stdlib/src/socket.rs
  • crates/vm/Cargo.toml
  • crates/vm/src/builtins/type.rs
  • crates/vm/src/macros.rs
  • crates/vm/src/object/core.rs
  • crates/vm/src/object/ext.rs
  • crates/vm/src/object/payload.rs
  • crates/vm/src/stdlib/_io.rs
  • crates/vm/src/stdlib/_signal.rs
  • crates/vm/src/stdlib/_thread.rs
  • crates/vm/src/stdlib/os.rs
  • crates/vm/src/stdlib/posix.rs
  • crates/vm/src/stdlib/sys.rs
💤 Files with no reviewable changes (3)
  • crates/common/Cargo.toml
  • crates/vm/Cargo.toml
  • crates/stdlib/Cargo.toml

Comment thread crates/stdlib/src/openssl.rs Outdated
Comment on lines +34 to 39
pub const PYTHREAD_NAME: Option<&str> = cfg_select! {
windows => Some("nt"),
unix => Some("pthread"),
any(target_os = "solaris", target_os = "illumos") => Some("solaris"),
_ => None,
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

Rust cfg_select first matching arm target_os solaris illumos cfg unix target_family

💡 Result:

In Rust, both Solaris (e.g., x86_64-pc-solaris) and illumos (e.g., x86_64-unknown-illumos, aarch64-unknown-illumos) targets have target_os = "solaris" for LLVM but are recognized distinctly in Rust's cfg system. Illumos uses base::illumos::opts, analogous to base::solaris::opts which explicitly sets target_family = ["unix"]. Custom illumos targets also specify "target-family": ["unix"]. Both match cfg(target_os = "solaris"), cfg(target_family = "unix"), and cfg(unix). The cfg_select! macro (stable since Rust 1.95) selects the first matching arm based on cfg predicates evaluated left-to-right. For a cfg_select prioritizing illumos over generic solaris/unix: cfg_select! { target_os = "solaris", target_os = "illumos" => { /* illumos-specific / }, target_os = "solaris" => { / Solaris-specific (excludes illumos) / }, target_family = "unix" => { / other unix / }, _ => { / fallback */ } } However, since illumos sets target_os = "illumos" in Rust (distinct from Solaris's "solaris"), target_os = "illumos" matches only illumos, while target_os = "solaris" matches only Solaris. If using target_os = "solaris" first, it matches neither, falling through. To match illumos first: target_os = "illumos" arm before target_os = "solaris". Both are under target_family = "unix".

Citations:


🏁 Script executed:

cat -n crates/vm/src/stdlib/_thread.rs | sed -n '30,45p'

Repository: RustPython/RustPython

Length of output: 751


Move the Solaris/illumos arm before the unix arm.

cfg_select! evaluates arms left-to-right and stops at the first match. Since both Solaris and illumos targets have target_family = "unix", they currently match the unix => Some("pthread") arm before reaching the more specific any(target_os = "solaris", target_os = "illumos") arm. Reorder to check the narrower condition first.

🔧 Proposed fix
 pub const PYTHREAD_NAME: Option<&str> = cfg_select! {
     windows => Some("nt"),
+    any(target_os = "solaris", target_os = "illumos") => Some("solaris"),
     unix => Some("pthread"),
-    any(target_os = "solaris", target_os = "illumos") => Some("solaris"),
     _ => None,
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub const PYTHREAD_NAME: Option<&str> = cfg_select! {
windows => Some("nt"),
unix => Some("pthread"),
any(target_os = "solaris", target_os = "illumos") => Some("solaris"),
_ => None,
};
pub const PYTHREAD_NAME: Option<&str> = cfg_select! {
windows => Some("nt"),
any(target_os = "solaris", target_os = "illumos") => Some("solaris"),
unix => Some("pthread"),
_ => None,
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/_thread.rs` around lines 34 - 39, The cfg_select! arms
for PYTHREAD_NAME currently list the broader unix => Some("pthread") arm before
the more specific any(target_os = "solaris", target_os = "illumos") arm, so
Solaris/illumos targets never reach their intended match; fix by reordering the
cfg_select! arms in the PYTHREAD_NAME constant so the any(target_os = "solaris",
target_os = "illumos") arm appears before the unix arm, preserving the other
arms (windows and default) as-is.

Comment thread crates/vm/src/stdlib/sys.rs
@ShaharNaveh ShaharNaveh force-pushed the cfg-select-macro branch 9 times, most recently from 8169811 to 7dce1bb Compare April 20, 2026 17:03
@ShaharNaveh ShaharNaveh marked this pull request as ready for review April 20, 2026 19:23
@ShaharNaveh ShaharNaveh requested a review from youknowone April 21, 2026 07:21
@youknowone youknowone enabled auto-merge (squash) April 21, 2026 15:03
Copy link
Copy Markdown
Member

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@youknowone youknowone merged commit dc65255 into RustPython:main Apr 21, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants