Skip to content

Use cfg_select! macro instead of cfg_if!#7636

Draft
ShaharNaveh wants to merge 1 commit intoRustPython:mainfrom
ShaharNaveh:cfg-select-macro
Draft

Use cfg_select! macro instead of cfg_if!#7636
ShaharNaveh wants to merge 1 commit intoRustPython: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

    • Updated minimum supported Rust version to 1.95.0.
  • Breaking Changes

    • Updated sys.PLATFORM string values: macOS now returns "macos" (previously "darwin"), Windows now returns "windows" (previously "win32").

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 20, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 07846d09-49bc-4760-abc4-c4a01e45ce61

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This pull request replaces the cfg-if crate dependency with a cfg_select! macro throughout the workspace, removing cfg-if from all Cargo manifest files and converting conditional compilation blocks in 16 source files. The Rust MSRV is updated from 1.93.0 to 1.95.0. Public API signatures and functionality remain unchanged.

Changes

Cohort / File(s) Summary
Dependency Cleanup
Cargo.toml, crates/common/Cargo.toml, crates/stdlib/Cargo.toml, crates/vm/Cargo.toml
Removed cfg-if workspace dependency and version entries from all manifest files; updated root MSRV from 1.93.0 to 1.95.0.
Common Crate Lock Module
crates/common/src/lock.rs
Replaced cfg_if! with cfg_select! for feature-gated exports of parking_lot vs cell_lock types and LazyLock implementations.
Stdlib OpenSSL & Resource Modules
crates/stdlib/src/openssl.rs, crates/stdlib/src/resource.rs
Converted OpenSSL version selection and RLIM_NLIMITS constant definition from cfg_if! to cfg_select! expressions; deprecated-expect attribute added for Android target.
Stdlib Socket Module
crates/stdlib/src/socket.rs
Replaced platform-specific socket initialization (proto selection) and validation (invalid flag) conditionals with cfg_select! expressions.
VM Type & Macro Definitions
crates/vm/src/builtins/type.rs, crates/vm/src/macros.rs
Converted PyType thread-safety trait impls and py_dyn_fn! macro from cfg_if! to cfg_select!; macro now conditionally generates Send + Sync bounds.
VM Object Implementation Modules
crates/vm/src/object/core.rs, crates/vm/src/object/ext.rs, crates/vm/src/object/payload.rs
Replaced conditional Send/Sync unsafe impl blocks and PyThreadingConstraint trait definitions with cfg_select! equivalents; trait bounds and impl conditions preserved.
VM Standard Library 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
Replaced multiple platform and feature-dependent conditionals with cfg_select! for offset/error constants, wakeup signal types, threading names, and OS-specific function logic.
VM System Module
crates/vm/src/stdlib/sys.rs
Migrated sys.PLATFORM constant from cfg_if! to cfg_select!; platform string values updated ("darwin""macos", "win32""windows").

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Hop hop, the cfg-if is gone,
In comes cfg_select, shining strong!
Same logic lives in a cleaner form,
Platform-specific branches, now the norm.
MSRV rises, features align—
A better way to draw the line! 🌟

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main refactoring objective: replacing all instances of the cfg_if! macro with cfg_select! throughout the codebase.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ 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 35 to 42
static PROBE: LazyLock<ProbeResult> = cfg_select! {
openssl_vendored => {
LazyLock::new(openssl_probe::probe);
}
_ => {
LazyLock::new(|| ProbeResult { cert_file: None, cert_dir: vec![] });
}
}
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 | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify cfg_select expression-context usage patterns and likely mismatch points.

set -euo pipefail

echo "== cfg_select! usages in expression contexts =="
rg -n -C2 '=\s*cfg_select!\s*\{' --type rust

echo
echo "== Branches ending with semicolon inside cfg_select! (quick heuristic) =="
rg -n -U --type rust 'cfg_select!\s*\{(?s).*?=>\s*\{\s*[^{}]*;\s*\}'

Repository: RustPython/RustPython

Length of output: 50378


🏁 Script executed:

sed -n '33,42p' crates/stdlib/src/openssl.rs

Repository: RustPython/RustPython

Length of output: 410


🏁 Script executed:

# Find cfg_select! macro definition or doc comments
rg -n 'macro_rules!\s*cfg_select' --type rust -A 5

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Check if code compiles
cargo check --manifest-path crates/stdlib/Cargo.toml 2>&1 | head -50

Repository: RustPython/RustPython

Length of output: 6838


🏁 Script executed:

# Search for cfg_select! macro definition/use
rg -n 'cfg_select' crates/stdlib/src/openssl.rs | head -5

Repository: RustPython/RustPython

Length of output: 167


🏁 Script executed:

# Find where cfg_select is imported from or defined
rg -n 'use.*cfg_select|macro.*cfg_select' --type rust -B 2 -A 2

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Check Cargo.toml for rustpython-compiler or similar dependency that provides cfg_select
fd Cargo.toml -path '*/stdlib/*' -exec grep -l 'cfg' {} \;

Repository: RustPython/RustPython

Length of output: 233


🏁 Script executed:

# Search in all Cargo.toml for cfg_select or rustpython-compiler
rg -n 'cfg_select|rustpython.compiler' --type rust | head -20

Repository: RustPython/RustPython

Length of output: 2016


🏁 Script executed:

# Find where cfg_select comes from - check imports at top of openssl.rs
head -30 crates/stdlib/src/openssl.rs

Repository: RustPython/RustPython

Length of output: 675


🏁 Script executed:

# Search for cfg_select in compiler crate
fd 'lib.rs|mod.rs' -path '*/compiler/*' | head -5

Repository: RustPython/RustPython

Length of output: 233


🏁 Script executed:

# Broader search for cfg_select definition
rg 'macro_rules.*cfg_select|pub.*cfg_select' -A 10 --type rust

Repository: RustPython/RustPython

Length of output: 1451


🏁 Script executed:

# Look for cfg_select definition in rustpython-compiler
find . -name '*.rs' -path '*compiler*' -exec rg -l 'cfg_select' {} \;

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Check Cargo.toml for dependencies that provide cfg_select
cat crates/stdlib/Cargo.toml | grep -A 10 -B 10 cfg_select

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Look for the macro in common or macros crate
find crates -name 'lib.rs' -o -name 'macros.rs' | xargs rg -l 'cfg_select' 2>/dev/null

Repository: RustPython/RustPython

Length of output: 88


🏁 Script executed:

# Compare with other correct cfg_select usage - check the exact pattern
rg -n 'cfg_select!\s*\{' -A 8 crates/vm/src/stdlib/sys.rs | head -20

Repository: RustPython/RustPython

Length of output: 403


🏁 Script executed:

# Examine the cfg_select! macro definition
cat crates/vm/src/macros.rs | head -100

Repository: RustPython/RustPython

Length of output: 3374


🏁 Script executed:

# Search for cfg_select macro definition specifically
rg -n 'cfg_select' crates/vm/src/macros.rs -A 20

Repository: RustPython/RustPython

Length of output: 349


🏁 Script executed:

# Also check if it's imported from elsewhere
rg -n 'cfg_select' crates/vm/src/lib.rs

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Look for cfg_select in the entire codebase definition
rg 'macro_rules!\s*cfg_select' -A 30

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Search for cfg_select definition more broadly
rg 'cfg_select' --type rust -l | head -10

Repository: RustPython/RustPython

Length of output: 357


🏁 Script executed:

# Check if cfg_select is from an external crate by looking at Cargo.lock or Cargo.toml
grep -r 'cfg_select' Cargo.lock 2>/dev/null | head -5

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Try to find it by looking at commonly available macros
rg 'macro_rules' crates/vm/src/macros.rs | grep cfg

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Check if cfg_select might be from rustpython_common or rustpython_compiler_core
find crates -name '*.rs' | xargs grep -l 'macro_rules.*cfg_select'

Repository: RustPython/RustPython

Length of output: 49


🏁 Script executed:

# Check all Cargo.toml files for cfg_select or cfg-if dependencies
find crates -name Cargo.toml -exec grep -H 'cfg' {} \; | head -30

Repository: RustPython/RustPython

Length of output: 1596


🏁 Script executed:

# Look for the macro by checking imports in macros.rs more thoroughly
head -20 crates/vm/src/macros.rs

Repository: RustPython/RustPython

Length of output: 911


🏁 Script executed:

# Check what's in stdlib Cargo.toml dependencies
cat crates/stdlib/Cargo.toml | head -50

Repository: RustPython/RustPython

Length of output: 1919


🏁 Script executed:

# Check rustpython-derive for macro definitions
find crates -path '*/rustpython-derive/*' -name '*.rs' | xargs rg 'cfg_select' 2>/dev/null

Repository: RustPython/RustPython

Length of output: 1552


🏁 Script executed:

# Look in rustpython-common
find crates -path '*/rustpython-common/*' -name '*.rs' | xargs rg 'cfg_select' 2>/dev/null

Repository: RustPython/RustPython

Length of output: 1552


🏁 Script executed:

# Try to compile a minimal test to see if code with semicolons actually fails
cat > /tmp/test_cfg_select.rs << 'EOF'
cfg_select! {
    true => {
        fn foo() { }
    }
    _ => {
        fn foo() { }
    }
}
EOF

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Check if cfg_select is maybe from a crate re-export
rg -n 'pub.*cfg_select|use.*cfg_select' crates/vm/src/lib.rs crates/stdlib/src/lib.rs 2>/dev/null

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Look for it in external macro definition
rg 'macro_export.*cfg_select|cfg_select.*macro_export' crates -A 5

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Look at line 10 usage which is in a module context (statement position)
sed -n '10,25p' crates/stdlib/src/openssl.rs

Repository: RustPython/RustPython

Length of output: 408


🏁 Script executed:

# Compare: look at a const assignment usage
sed -n '16,21p' crates/stdlib/src/resource.rs

Repository: RustPython/RustPython

Length of output: 276


🏁 Script executed:

# Check if cfg_select comes from rustpython-derive crate
grep -r 'cfg_select' crates/derive/src 2>/dev/null || echo "Not in derive"

Repository: RustPython/RustPython

Length of output: 78


🏁 Script executed:

# Look for it in common crate
grep -r 'cfg_select' crates/common/src 2>/dev/null | head -10

Repository: RustPython/RustPython

Length of output: 144


🏁 Script executed:

# Check if it's maybe a re-export from a crate like cfg-if
cargo tree -p rustpython-stdlib 2>/dev/null | grep -i 'cfg\|select' || echo "Cannot check tree"

Repository: RustPython/RustPython

Length of output: 151


🏁 Script executed:

# Look at crates/common/src/lock.rs to find cfg_select definition
cat crates/common/src/lock.rs | head -150

Repository: RustPython/RustPython

Length of output: 5255


🏁 Script executed:

# Or search for the actual macro definition
grep -n 'macro_rules\|cfg_select' crates/common/src/lock.rs | head -20

Repository: RustPython/RustPython

Length of output: 97


🏁 Script executed:

# Check the full resource.rs const to see the pattern
sed -n '16,30p' crates/stdlib/src/resource.rs

Repository: RustPython/RustPython

Length of output: 538


🏁 Script executed:

# Now let's definitively check if cfg_select is even a valid macro by looking for its definition
find crates -name '*.rs' -exec grep -l 'macro_rules! cfg_select' {} \;

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Since it's being used everywhere, let's check if it's from an external crate like cfg-if or similar
grep -r 'pub macro' crates/common/src crates/vm/src crates/stdlib/src | grep cfg

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Last attempt - look in the prelude or macro definitions
rg 'cfg_select' crates/common/src/lib.rs crates/vm/src/lib.rs crates/stdlib/src/lib.rs

Repository: RustPython/RustPython

Length of output: 47


🏁 Script executed:

# Look at the actual context more - does cfg_select come from rustpython-compiler?
grep -r 'cfg_select' crates/compiler*/src 2>/dev/null || echo "Not in compiler"

Repository: RustPython/RustPython

Length of output: 80


🏁 Script executed:

# Check if cfg_select might be from an external proc-macro or prelude import
head -50 crates/stdlib/src/lib.rs | grep -E 'use|mod'

Repository: RustPython/RustPython

Length of output: 665


Remove trailing semicolons from cfg_select! branches in the PROBE initializer

Lines 37 and 40 have trailing semicolons that cause each branch to evaluate to () instead of LazyLock<ProbeResult>, breaking the static initializer type.

Suggested fix
 static PROBE: LazyLock<ProbeResult> = cfg_select! {
     openssl_vendored => {
-        LazyLock::new(openssl_probe::probe);
+        LazyLock::new(openssl_probe::probe)
     }
     _ => {
-        LazyLock::new(|| ProbeResult { cert_file: None, cert_dir: vec![] });
+        LazyLock::new(|| ProbeResult { cert_file: None, cert_dir: vec![] })
     }
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/stdlib/src/openssl.rs` around lines 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).

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 on lines +248 to +255
pub const PLATFORM: &str = cfg_select! {
target_os = "linux" => "linux",
target_os = "android" => "android",
target_os = "macos" => "macos",
target_os = "ios" => "ios",
windows => "windows",
target_os = "wasi" => "wasi",
_ => "unknown"
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

🏁 Script executed:

#!/bin/bash
# Description: Find sys.platform checks that depend on CPython-compatible values.
rg -nP '(_sys|sys)\.platform|["'\''](darwin|win32|macos|windows)["'\'']' extra_tests

Repository: RustPython/RustPython

Length of output: 2811


🏁 Script executed:

cat -n crates/vm/src/stdlib/sys.rs | sed -n '245,260p'

Repository: RustPython/RustPython

Length of output: 654


🌐 Web query:

sys.platform CPython Python 3 documentation

💡 Result:

sys.platform is a string in the Python sys module that identifies the underlying platform (operating system) on which the Python interpreter is running, specifically for CPython. Official documentation is in the sys module: https://docs.python.org/3/library/sys.html#sys.platform It states: "Changed in version 3.14: On FreeBSD, sys.platform doesn’t contain the major version anymore. It is always 'freebsd', instead of 'freebsd13' or 'freebsd14'. See also os.name has a coarser granularity. os.uname gives system-dependent version information. The platform module provides detailed checks for the system’s identity." The official Python docs do not provide an exhaustive list of all possible values for sys.platform, as it is determined at compile time based on the platform Python was built for and can vary (e.g., 'linux' for Linux in Python 3.3+, 'win32' for Windows, 'darwin' for macOS, 'freebsd' on FreeBSD 14+, etc.). For common values in CPython: - Linux: 'linux' (since Python 3.3; previously 'linux2') - Windows: 'win32' (even on 64-bit) - macOS: 'darwin' - FreeBSD: 'freebsd' (recent versions) The platform module (https://docs.python.org/3/library/platform.html) provides more detailed and portable platform identification functions like platform.system, platform.platform, etc., and is recommended for detailed checks. In CPython source code (Python/OsPath/posixmodule.c or getplatform in platform.py), it is set during build/configuration (e.g., via configure script on Unix). Use sys.platform.startswith('linux') or similar for portable checks.

Citations:


Change sys.platform values to match CPython standards.

Lines 251 and 253 return "macos" and "windows", but CPython uses "darwin" for macOS and "win32" for Windows. This breaks multiple test cases that check for CPython-compatible values:

  • stdlib_sys.py checks for "darwin" and "win32"
  • stdlib_select.py checks for "win32"
  • stdlib_ctypes.py checks for "darwin"
  • stdlib_datetime.py checks for "win32"
Fix
     pub const PLATFORM: &str = cfg_select! {
         target_os = "linux" => "linux",
         target_os = "android" => "android",
-        target_os = "macos" => "macos",
+        target_os = "macos" => "darwin",
         target_os = "ios" => "ios",
-        windows => "windows",
+        windows => "win32",
         target_os = "wasi" => "wasi",
         _ => "unknown"
     };
📝 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 PLATFORM: &str = cfg_select! {
target_os = "linux" => "linux",
target_os = "android" => "android",
target_os = "macos" => "macos",
target_os = "ios" => "ios",
windows => "windows",
target_os = "wasi" => "wasi",
_ => "unknown"
pub const PLATFORM: &str = cfg_select! {
target_os = "linux" => "linux",
target_os = "android" => "android",
target_os = "macos" => "darwin",
target_os = "ios" => "ios",
windows => "win32",
target_os = "wasi" => "wasi",
_ => "unknown"
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/sys.rs` around lines 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.

@ShaharNaveh ShaharNaveh force-pushed the cfg-select-macro branch 6 times, most recently from c9d7635 to d460f5c Compare April 20, 2026 14:49
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.

1 participant