Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Announce automatic checking of cfgs at compile-time #1313

Merged
merged 13 commits into from
May 6, 2024
Merged
Prev Previous commit
Next Next commit
Change the schedule to tomorrow (2024-05-05)
  • Loading branch information
Urgau committed May 4, 2024
commit 2682b15f41a6d3fed7f22cd03e97be8d0419b712
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ team: The Cargo Team <https://www.rust-lang.org/governance/teams/dev-tools#cargo

# Automatic checking of cfgs at compile-time

The Cargo and Compiler team are delighted to announce that starting with Rust 1.80 (or nightly-2024-05-08) every _reachable_ `#[cfg]` will be automatically checked that they match the expected config names and values.
The Cargo and Compiler team are delighted to announce that starting with Rust 1.80 (or nightly-2024-05-05) every _reachable_ `#[cfg]` will be automatically checked that they match the expected config names and values.

This can help with verifying that the crate is correctly handling conditional compilation for different target platforms or features. It ensures that the cfg settings are consistent between what is intended and what is used, helping to catch potential bugs or errors early in the development process.

Expand Down Expand Up @@ -48,15 +48,15 @@ fn win() {}
```

*`cargo check`*:
![cargo-check](../../../../images/2024-05-08-check-cfg/cargo-check.svg)
![cargo-check](../../../../images/2024-05-05-check-cfg/cargo-check.svg)

## Custom cfgs and build scripts

> In Cargo point-of-view: a custom cfg is one that is neither defined by `rustc` nor by a Cargo feature. Think of `tokio_unstable`, `has_foo`, ... but not `feature = "lasers"`, `unix`, ...

Some crates use custom cfgs that they either expected from the environment (`RUSTFLAGS`or other means) or is enabled by some logic in the crate `build.rs`. For those crates Cargo provides a new instruction: [`cargo::rustc-check-cfg`](https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg)[^2] (or `cargo:rustc-check-cfg` for older Cargo version).

[^2]: `cargo::rustc-check-cfg` will start working in Rust 1.80 (or nightly-2024-05-08). From Rust 1.77 to Rust 1.79 *(inclusive)* it is silently ignored. In Rust 1.76 and below a warning is emitted when used without the unstable Cargo flag `-Zcheck-cfg`.
[^2]: `cargo::rustc-check-cfg` will start working in Rust 1.80 (or nightly-2024-05-05). From Rust 1.77 to Rust 1.79 *(inclusive)* it is silently ignored. In Rust 1.76 and below a warning is emitted when used without the unstable Cargo flag `-Zcheck-cfg`.

The syntax to use is described in the [rustc book](https://doc.rust-lang.org/nightly/rustc/) section [checking configuration](https://doc.rust-lang.org/nightly/rustc/check-cfg.html), but in a nutshell the basic syntax of `--check-cfg` is:

Expand Down