Skip to content

Commit 1cf18a7

Browse files
committed
Add Cargo specific doc regarding his interaction with --check-cfg
1 parent 37c4a77 commit 1cf18a7

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/doc/rustc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
- [Instrumentation-based Code Coverage](instrument-coverage.md)
8686
- [Linker-plugin-based LTO](linker-plugin-lto.md)
8787
- [Checking conditional configurations](check-cfg.md)
88+
- [Cargo specifics](check-cfg/cargo-specifics.md)
8889
- [Exploit Mitigations](exploit-mitigations.md)
8990
- [Symbol Mangling](symbol-mangling/index.md)
9091
- [v0 Symbol Format](symbol-mangling/v0.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Cargo specifics - Checking conditional configurations
2+
3+
<!--
4+
This page is the canonical place for describing the interaction between Cargo
5+
and --check-cfg, it is placed in the rustc book because --check-cfg and the
6+
unexpected_cfgs are owned by rustc, not Cargo. T-Cargo also considers
7+
`[lints.rust.unexpected_cfgs.check-cfg]` to be an implementation detail and
8+
is therefor not documented in Cargo.
9+
-->
10+
11+
*See the Cargo announcement [blog post][check-cfg-blog-post] for more a global view of the feature.*
12+
13+
> This document is intented to summarize the principal ways Cargo interacts with
14+
> the `unexpected_cfgs` lint and `--check-cfg` flag. It is not intended to provide
15+
> individual details, for that refer to the [`--check-cfg` documentation](../check-cfg.md) and
16+
> to the [Cargo book](../../../cargo/index.html).
17+
18+
Cargo always enables [checking conditional configurations](../check-cfg.md) with Rust 1.80 and upper.
19+
20+
This is to help with verifying that the crate is correctly handling conditional compilation for
21+
different target platforms or features. It ensures that the cfg settings are consistent between
22+
what is intended and what is used, helping to catch potential bugs or errors early in the
23+
development process.
24+
25+
This document express different ways users can declare expected cfgs, and in particular for
26+
custom configs[^1] with Cargo as the build system.
27+
28+
[^1]: In Cargo point-of-view: a custom cfg is one that is neither defined by `rustc` nor by a Cargo
29+
feature. Think of `tokio_unstable`, `has_foo`, ... but not `feature = "lasers"`, `unix` or
30+
`debug_assertions`
31+
32+
[check-cfg-blog-post]: https://blog.rust-lang.org/2024/05/06/check-cfg.html
33+
34+
## Cargo feature
35+
36+
*See the [`[features]` section in the Cargo book][cargo-features] for more details.*
37+
38+
With the `[features]` table Cargo provide a mechanism to express conditional compilation and
39+
optional dependencies. Cargo *automatically* declares every feature as expected.
40+
41+
`Cargo.toml`:
42+
```toml
43+
[features]
44+
serde = ["dep:serde"]
45+
my_feature = []
46+
```
47+
48+
[cargo-features]: ../../../cargo/reference/features.html
49+
50+
## `check-cfg` in `[lints.rust]` table
51+
52+
*See the [`[lints]` section in the Cargo book][cargo-lints-table] for more details.*
53+
54+
When using a custom config not set via a build-script, Cargo provides the custom lint config
55+
`check-cfg` under `[lints.rust.unexpected_cfgs]`.
56+
57+
It can be used to set custom static [`--check-cfg`](../check-cfg.md) args, it is mainly useful when
58+
the list of expected cfgs is known is advance.
59+
60+
`Cargo.toml`:
61+
```toml
62+
[lints.rust]
63+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] }
64+
65+
# or
66+
67+
[lints.rust.unexpected_cfgs]
68+
level = "warn"
69+
check-cfg = ['cfg(has_foo)']
70+
```
71+
72+
[cargo-lints-table]: ../../../cargo/reference/manifest.html#the-lints-section
73+
74+
## `cargo::rustc-check-cfg` for `build.rs`/build-script
75+
76+
*See the [`cargo::rustc-check-cfg` section in the Cargo book][cargo-rustc-check-cfg] for more details.*
77+
78+
When setting a custom config with [`cargo::rustc-cfg`][cargo-rustc-cfg], Cargo provides the
79+
corrolary instruction: [`cargo::rustc-check-cfg`][cargo-rustc-check-cfg] to expect custom configs.
80+
81+
`build.rs`:
82+
```rust
83+
fn main() {
84+
println!("cargo::rustc-check-cfg=cfg(has_foo)");
85+
// ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80
86+
if has_foo() {
87+
println!("cargo::rustc-cfg=has_foo");
88+
}
89+
}
90+
```
91+
92+
> Each `cargo::rustc-cfg` should have an accompanying **unconditional** `cargo::rustc-check-cfg`
93+
> directive to avoid warnings like this: `unexpected cfg condition name: has_foo`.
94+
95+
[cargo-rustc-cfg]: ../../../cargo/reference/build-scripts.html#rustc-cfg
96+
[cargo-rustc-check-cfg]: ../../../cargo/reference/build-scripts.html#rustc-check-cfg

0 commit comments

Comments
 (0)