Skip to content

Update check-cfg blog post with recent Cargo change #1336

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

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions posts/2024-05-06-check-cfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ fn win() {}

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

## Expecting static cfgs

*UPDATE: This section was added with the release of nightly-2024-05-19.*

Some crates might use cfgs, like `loom`, `fuzzing` 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.

It is done through the special `check-cfg` config under `[lints.rust.unexpected_cfgs]`:

*`Cargo.toml`*
```toml
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)', 'cfg(fuzzing)'] }
```

## 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` or `debug_assertions`
Expand Down Expand Up @@ -115,9 +129,11 @@ This means that doing `RUSTFLAGS="--cfg tokio_unstable" cargo check` will not re

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

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

~~There is **currently no way** to expect a custom cfg other than with `cargo::rustc-check-cfg` in a `build.rs`.~~

Crate authors that don't want to use a `build.rs` are encouraged to use Cargo features instead.
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.

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

Expand Down
1 change: 1 addition & 0 deletions src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Post {
.build()?,
extension: comrak::ExtensionOptionsBuilder::default()
.header_ids(Some(String::new()))
.strikethrough(true)
.footnotes(true)
.table(true)
.build()?,
Expand Down