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

Remove cargo::rustc-check-cfg to only have one place to configure the unknown_cfgs lint #13945

Open
tbu- opened this issue May 21, 2024 · 8 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.

Comments

@tbu-
Copy link
Contributor

tbu- commented May 21, 2024

There are currently two places where the lint can be configured, in build.rs using cargo::rustc-check-cfg and in the Cargo.toml in the lints section. Generally, having only one place is better than having two, as users don't have to look into two places to find the configuration.

Since this feature hasn't been shipped to a stable compiler yet, we could currently still remove one of them without backward compatibility hazards.

The possible cfgs of a crate across all build configurations are known statically, since they're at most all that appear in the source code. This means that all possible configurations should be expressible in Cargo.toml.

@tbu- tbu- added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels May 21, 2024
@tbu-
Copy link
Contributor Author

tbu- commented May 21, 2024

The declarative approach also works for cfgs used in build.rs directly: rust-lang/rust#125368.

@epage
Copy link
Contributor

epage commented May 22, 2024

Currently, we have it documented to encourage

  • build.rs if you specify the associated cfg in build.rs
  • Cargo.toml for other cases (like fuzzing)

When we document the build.rs case, we talk about the importance of keeping cargo::rustc-check-cfg close to cargo::rustc-cfg. I think this is an important property to help ensure they are updated hand-in-hand.

@mcclure
Copy link

mcclure commented May 22, 2024

@epage , please do notice my note at the end in #125368 — the fact that the build-rs-printf method does not suppress warnings creates a problem because currently it's possible for the warning to recommend a mitigation which has no effect.

Assuming that isn't fixable, if you do keep in the build-rs-printf method, I think you should be careful to document build-rs-printf will not helpful if cfgs are used in build.rs itself, and ideally you should modify the warning hint to not recommend the build-rs-printf fix if it detects (because this is detectable) that the warning has been triggered within build.rs. Alternately, removing the build-rs-printf method would mean you don't need that complexity in messaging/warning plumbing.

@Urgau
Copy link
Member

Urgau commented May 22, 2024

I agree with @epage, keeping cargo::rustc-check-cfg is important, there are users how would be negatively affected by it's removal, like users of the cfg_aliases, which would need to manually declare the expected cfgs outside of where they define their aliases, which goes completely against what we are trying to achieve.

Assuming that isn't fixable

It is fixable. I just didn't yet have time to do it.

@Urgau
Copy link
Member

Urgau commented May 22, 2024

The declarative approach also works for cfgs used in build.rs directly: rust-lang/rust#125368.

While it works, it's also not 100% the same thing, the [lints.rust.unexpected_cfgs.check-cfg] lint config applies to very unit, including build.rs, but if the config is declared by the build.rs, that probably means it isn't expected for the build.rs it-self. So putting it at the same place as the cargo::rustc-cfg instruction guarantees the same applicability.

@tbu-
Copy link
Contributor Author

tbu- commented May 22, 2024

When we document the build.rs case, we talk about the importance of keeping cargo::rustc-check-cfg close to cargo::rustc-cfg. I think this is an important property to help ensure they are updated hand-in-hand.

I see, I think this argument makes sense. It also means that tools outputting cargo::rustc-cfg can also directly output cargo::rustc-check-cfg.

I still feel that a) having a non-declarative version of the config makes it harder for tools, because they need to run untrusted code to get a list of cfgs, and b) users need to be taught about two things that do roughly the same thing, are two significant downsides to this approach.

If we started with [lints.rust.unexpected_cfgs.check-cfg] today, would the build.rs way we be added? Perhaps yes.

@epage
Copy link
Contributor

epage commented May 22, 2024

@mcclure

@epage , please do notice my note at the end in rust-lang/rust#125368 — the fact that the build-rs-printf method does not suppress warnings creates a problem because currently it's possible for the warning to recommend a mitigation which has no effect.

build.rs only sets compilation flags for all other build targets and not for itself.

With the split I gave of static vs dynamic cfgs, could you give an example of a cfg that the build.rs both reads and sets where this is a problem?

@mcclure
Copy link

mcclure commented May 22, 2024

build.rs only sets compilation flags for all other build targets and not for itself.

With the split I gave of static vs dynamic cfgs, could you give an example of a cfg that the build.rs both reads and sets where this is a problem?

Hi, I'm not sure I understand what you mean by "the split I gave of static vs dynamic cfgs", but the case I give in #125368 is the case of a .rs file which for whatever reason is used both in the build.rs and also in a non-build.rs target. It's true the custom cfg can be set for only non-build.rs targets, but the rustc-check-cfg warning appears also for cfg(not()) statements, and code that runs during build.rs might indeed want to happen conditionally on cfg(not(symbolname)) where symbolname is a cfg build.rs sets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

4 participants