Skip to content
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

#[cfg(features = "something")] compiles, creating a potential foot-gun #108737

Closed
sam0x17 opened this issue Mar 4, 2023 · 3 comments
Closed

#[cfg(features = "something")] compiles, creating a potential foot-gun #108737

sam0x17 opened this issue Mar 4, 2023 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@sam0x17
Copy link

sam0x17 commented Mar 4, 2023

I tried this code:

#[cfg(features = "my-feature")]
struct MyStruct {}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=256c4dfff8b9a14d10ea75cb4e4aa2f5

I expected to see this happen:

A compiler error, or at least a suppressable warning specific to this situation. The valid syntax is #[cfg(feature = ...], not #[cfg(features = ...], and as far as I know, there is no valid context where it would be #[cfg(features = [something]].

Instead, this happened:

Right now this compiles without any errors or warnings. This caused a huge foot-gun for me in a project where I accidentally had #[cfg(features = ...] in several places and the compiler didn't fail or warn me in any way.

Meta

rustc --version --verbose:

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: aarch64-apple-darwin
release: 1.67.1
LLVM version: 15.0.6

Note: this behavior is consistent in stable, beta, and nightly on the Rust Playground as of 03/04/2023.

@sam0x17 sam0x17 added the C-bug Category: This is a bug. label Mar 4, 2023
@Nilstrieb
Copy link
Member

Nilstrieb commented Mar 4, 2023

This can't be a compiler error, because it's totally legal to pass a cfg with that name, like --cfg features="my-feature".

We already have an unstable option --check-cfg "names()", that will complain about unknown cfgs.

While this lint is useful, it does need some configuration to pass other known cfgs to avoid false positives. Maybe it would make sense to enable a subset of it by default, where it only checks whether an unknown cfg is similiar to a known one. This way something "exotic" like cfg(loom) wouldn't warn by default, but cfg(debug_asertions) or cfg(features = "whatever") would (because they are almost surely wrong).

@Nilstrieb Nilstrieb added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. labels Mar 4, 2023
@Nilstrieb
Copy link
Member

RFC 3013 describes this in more detail

@ehuss
Copy link
Contributor

ehuss commented Mar 4, 2023

This has been implemented and is available on the nightly channel. See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg and rust-lang/cargo#10554 and #82450 for more information and tracking of the stabilization.

@ehuss ehuss closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants