or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
--check-cfg
Stabilization Report- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Summary
This adds a new top-level command line option
--check-cfg
(torustc
andrustdoc
) in order to enable checking conditional compilation at compile time.rustc
will check every#[cfg(name = "value")]
,#[cfg_attr(name = "value")]
,#[link(name = "a", cfg(name = "value"))]
attributes andcfg!(name = "value")
macro calls.--cfg
arguments are not checked[1].The syntax looks (roughly) like this:
From the unstable book documentation:
Example
Well known names and values
From the unstable book section:
As of
2024-01-09T
, the list of known names is as follows:debug_assertions
doc
doctest
miri
overflow_checks
panic
proc_macro
relocation_model
sanitize
sanitizer_cfi_generalize_pointers
sanitizer_cfi_normalize_integers
target_abi
target_arch
target_endian
target_env
target_family
target_feature
target_has_atomic
target_has_atomic_equal_alignment
target_has_atomic_load_store
target_os
target_pointer_width
target_thread_local
target_vendor
test
unix
windows
Cargo integration
Since ~1.61 nightly Cargo there has been some form of integration of
--check-cfg
in Cargo with the-Zcheck-cfg
.The latest and current one is
-Zcheck-cfg
which:A stabilization report for Cargo will be done and would propose to make the behavior of
-Zcheck-cfg
the default.Documentation and Testing
The feature is described in the unstable book under it's own section.
The feature is being extensively tested under
tests/ui/check-cfg
(27 test files). To give a some highlights:-Zcheck-cfg
widnows
instead ofwindows
)unexpected_cfgs
works in different situationsReal World Usage
-Zcheck-cfg
portable-atomic
:build.sh
Unresolved questions
From the tracking issue: Which configs should be in the well known list?
Currently the informal logic that has been followed is to include all the configs that are:
rustc
is not the only tool that sets cfgs,rustdoc
setsdoc
anddoctest
,cargo-miri
setsmiri
, docs.rs setsdocsrs
, …)rustc
cannot possibly know the what to add; this excludes Cargofeature
cfg)I propose that we continue to follow this rule.
-Zcheck-cfg
Stabilization Report- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Summary
This enables checking conditional compilation at compile time by passing a new command line option
--check-cfg
to allrustc
andrustdoc
invocations.In particular this enables checking Cargo features and custom configs with
cargo::rustc-check-cfg
(in build scripts).Since rust#117522 setting custom
--cfg
onRUSTFLAGS
no longer has an impact on any crates, only uses of them in the code triggers the warnings.Example
Enable-by-default
Per the tracking issue, there is one last but important question to address:
I propose that the behavior be enabled by default without a way to opt out, otherwise users will probably not see their mistakes in time or not know about the flag at all.
Impact
A crater run was done in rust#120701, the full summary can be found there. The sort version is:
A Call for testing was also performed and the responses were generaly positive, there were questions about the default set well know names/values and some anti pattern
#[path]
file sharing, but those are ortogonal to this stabilization.Mitigations options
However to mitigate it's impact we probably want to annonce the feature ahead of time and provide ahead of time a "migration guide" for users of custom configs.
None
We can consider that the impact is minimal enough and that positives of the feature are strong enough to not do anything more.
Edition bound
We have a edition (2024) that is approaching, we could bound the feature to being enabled on being enabled only for edition>=2024.
on Cargo side
We would only pass
--check-cfg
on newer editions.on rustc side
We (Cargo) would always pass
--check-cfg
but the lint would be allow by default for edition<2024 and warn-by-default for edition>=2024.Documentation and Testing
The feature is currently documented in the check-cfg section of the Cargo unstable features chapter. Stabilizing the feature would mainly involve documentating
cargo::rustc-check-cfg
as stable and providing users with a "migration page".The feature is being extensively tested under
tests/testsuite/check_cfg.rs
(~20 tests). To give a some highlights:features_with_namespaced_features
: test the feature with optional deps, explicit feature and path dependencieswell_known_names_values
: test that even without any Cargo features Cargo still emits the appropriate--check-cfg
argswell_known_names_values_doctest
: test the integration withrustdoc
build_script
: testcargo::rustc-check-cfg
in build scriptfeatures_fingerprinting
: simple test that makes sure that Cargo rerun rustc if the delcared features changesReal World Usage
portable-atomic
:build.sh
Unresolved questions
For a big part of the existance of the feature,
--cfg
CLI args were also checked, but given that it interacted badly CargoRUSTFLAGS
, it was removed and left as an future posibility in the documentation. ↩︎from the Call for testing ↩︎
by "false positive" I mean: missing well known names/values ↩︎