Skip to content

Commit fac44ad

Browse files
authored
Merge pull request #1336 from Urgau/check-cfg-update
Update check-cfg blog post with recent Cargo change
2 parents 07f8125 + 202d6bd commit fac44ad

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

posts/2024-05-06-check-cfg.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,25 @@ fn win() {}
5252

5353
![cargo-check](../../../../images/2024-05-06-check-cfg/cargo-check.svg)
5454

55-
## Custom cfgs and build scripts
55+
## Expecting custom cfgs
56+
57+
*UPDATE: This section was added with the release of nightly-2024-05-19.*
5658

5759
> 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` or `debug_assertions`
5860
59-
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).
61+
Some crates might use custom cfgs, like `loom`, `fuzzing` or `tokio_unstable` that they expected from the environment (`RUSTFLAGS` or other means) and which are always statically known at compile time. For those cases, Cargo provides via the `[lints]` table a way to statically declare those cfgs as expected.
62+
63+
Defining those custom cfgs as expected is done through the special `check-cfg` config under `[lints.rust.unexpected_cfgs]`:
64+
65+
*`Cargo.toml`*
66+
```toml
67+
[lints.rust]
68+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)', 'cfg(fuzzing)'] }
69+
```
70+
71+
## Custom cfgs in build scripts
72+
73+
On the other hand some crates use custom cfgs that are 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).
6074

6175
[^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`.
6276

@@ -115,9 +129,11 @@ This means that doing `RUSTFLAGS="--cfg tokio_unstable" cargo check` will not re
115129

116130
### How to expect custom cfgs without a `build.rs`?
117131

118-
There is **currently no way** to expect a custom cfg other than with `cargo::rustc-check-cfg` in a `build.rs`.
132+
*UPDATE: Cargo with nightly-2024-05-19 now provides the `[lints.rust.unexpected_cfgs.check-cfg]` config to address the statically known custom cfgs.*
133+
134+
~~There is **currently no way** to expect a custom cfg other than with `cargo::rustc-check-cfg` in a `build.rs`.~~
119135

120-
Crate authors that don't want to use a `build.rs` are encouraged to use Cargo features instead.
136+
Crate authors that don't want to use a `build.rs` and cannot use `[lints.rust.unexpected_cfgs.check-cfg]`, are encouraged to use Cargo features instead.
121137

122138
### How does it interact with other build systems?
123139

src/posts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl Post {
7373
.build()?,
7474
extension: comrak::ExtensionOptionsBuilder::default()
7575
.header_ids(Some(String::new()))
76+
.strikethrough(true)
7677
.footnotes(true)
7778
.table(true)
7879
.build()?,

0 commit comments

Comments
 (0)