When RFC 3013, "Checking conditional compilation at compile time", is implemented, there will be warnings for a #[cfg]
referring to a feature name that is not declared by Cargo, just as you're asking for. However, the implementation only just got started (Sep 28, 2021).
The means of operation described in the RFC is just as you suggested, ‘cargo
would pass down the flags to rustc
’.
It may be worth noting that this will not check all conditions appearing in the source text; as described in the RFC:
This lint will not be able to detect invalid #[cfg] tests that are within modules that are not compiled, presumably because an ancestor mod is disabled.
So, it will not confirm that all #[cfg(feature)]
are valid on a single cargo check
— you will need to test with your various features or combinations of features. But those are the same combinations that you would need anyway to check for compile errors in all of the regular source code that could be enabled; once you do that, the lint will assure you that you don't have any #[cfg(feature)]
that are never enabled due to a typo.
rustc
. For instance you could pass down a set of features that you wish the compiler to enforce, instead of pushing this responsibility further to a linter, which is very much optional to use. – HerbariumCargo.toml
. The set of features defined there (and then selected for a compilation) are simply turned into--cfg
arguments to the rustc invocation: rustc won't know about any that exist but aren't enabled, nor is Cargo's use offeature
as a cfg key in any way special to rustc. – Nuncupativecargo
would pass down the flags torustc
if it would be able to take in feature flags for strict checking. – Herbariumoption
as a cfg identifier instead offeature
; might give each such feature its own cfg identifier each with a boolean value (to indicate whether or not it's enabled); or indeed might do something completely different. Requiring the compiler to offer a validator for the whims of each possible build tool seems unnecessarily complex, and anything else would be coupling it a little too tightly to one particular approach? – Nuncupativefeature
is used bycore
'scfg
. – Herbarium