-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Add cargo_cfg_target_family_multivalued FCW
#147545
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
base: main
Are you sure you want to change the base?
Conversation
compiler/rustc_lint/src/cargo_cfg.rs
Outdated
| /// | ||
| /// [CARGO_CFG_TARGET_FAMILY]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#:~:text=CARGO_CFG_TARGET_FAMILY | ||
| /// [cfg-target_family]: https://doc.rust-lang.org/reference/conditional-compilation.html#target_family | ||
| CARGO_CFG_TARGET_FAMILY_MULTIVALUED, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about the name? cargo_cfg_target_family_multivalued is kinda long, but I felt that shortening it risked loosing meaning. _multivalued isn't the best descriptor though, perhaps _direct_comparison would be better?
| struct ReplaceWithSplitAny { | ||
| #[suggestion_part(code = "!")] | ||
| negate: Option<Span>, | ||
| #[suggestion_part(code = ".split(',').any(|x| x == ")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could suggest .contains("unix") instead, that'd work as well. I felt .split(',').any(|x| x == "unix") was more principled, but I'd be fine with either.
| Warn, | ||
| "comparing `CARGO_CFG_TARGET_FAMILY` env var with a single value", | ||
| @future_incompatible = FutureIncompatibleInfo { | ||
| reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure if this is the intended use of a FCW? I would like it to trigger in dependents some day, but there wasn't precedent for using FutureReleaseSemanticsChange anywhere.
|
Just to summarize, I think the main questions for lang is:
|
cargo_cfg_target_family_multivalued lintcargo_cfg_target_family_multivalued FCW
|
We talked about this in today's @rust-lang/lang meeting. This is a novel category of lint! In the future if we have a framework for custom lints, this should use a custom lint. But in the meantime, we're willing to consider this. The name should describe the behavior it catches, something like We feel that this shouldn't be warn-by-default; instead, it should be allow-by-default, and Cargo should change it to warn when building Finally, we'd like to get confirmation from @rust-lang/cargo that they would turn this lint on once it exists. |
|
(Please renominate for lang when ready per the above comment.) |
|
Whats the line for a lint like this to be in rustc vs clippy? In seeing this, it feels like something I would see in clippy. |
|
While I understand that it may have a weird "vibe", the purpose of this lint is for powering a migration that would affect usage of Rust in any platform-sensitive way. Thus it would be odd for it to require you to have anything but the minimal profile in order to detect it. |
9a0ef74 to
43f3f07
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
I've changed the name to what you suggest. Do note that the lint also catches
I considered going that route, but there exist build script helper crates such as If we really only want this to only trigger in build scripts, there is precedent elsewhere in For that reason, I'll de-nominate t-cargo, and re-nominate t-lang (while this lints on a Cargo environment variable, this is ultimately a lang issue, Cargo "just" forwards the
Clippy can't have FCWs, and we would like this to trigger for dependents because we would like to change this in the future. If we didn't intend to add further |
This comment has been minimized.
This comment has been minimized.
43f3f07 to
e043c5a
Compare
e043c5a to
3a8c4dd
Compare
Cargo has an unstable feature for multiple build scripts which is a stepping stone to having an artifact build-dependency that users tell Cargo to run as a build script. In both cases, the build script crate name is no longer |
|
@madsmtm We talked about this a bit more in today's meeting. We felt that:
In practice, I'd expect that it isn't going to be that common to move something from |
Add
cargo_cfg_target_family_multivaluedlint which detects simple comparisons like==ormatchon theCARGO_CFG_TARGET_FAMILYenvironment variable. These are wrong, sinceCARGO_CFG_TARGET_FAMILYis multi-valued (currently only onwasm32-unknown-emscriptenandwasm32-wali-linux-musl, so admittedly pretty rare).This lint is inherently incomplete, because we can't track a value like this through arbitrary control flow. Regardless, I've found that it works pretty well in practice, from the various repos I took a look at that used
CARGO_CFG_TARGET_FAMILY, I'd guess at least 80% of them would hit this lint.This should unblock at some point adding
#[cfg(target_family = "darwin")]as a replacement for#[cfg(target_vendor = "apple")], which in turns unblocks #100343.There exist other multi-valued cfgs such as
CARGO_CFG_TARGET_HAS_ATOMICorCARGO_CFG_TARGET_FEATURE, but these were very clearly multi-valued from the beginning, and thus doesn't make sense to lint for.This is my first time adding a lint, so beware that I'm a bit unsure if I did things right. Especially unsure if I should feature-gate the lint to start with, or if we should insta-stabilize it? I'm pretty sure it needs a lang FCP though.
r? compiler
CC @thomcc @workingjubilee