-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
New nightly feature "rustc-check-cfg" doesn't work as advertised by warning hint (cannot dismiss warning) #125368
Comments
Your analysis here is correct, I think you just missed this tiny note which is that cargo suppresses identical warnings 🙂 Before uncommenting the
After:
@Urgau what's the right way to deal with |
Thank you, I should have updated to NEWEST nightly before filing. I see that with 2024-05-20 nightly rustc the [lint] IS recommended in a hint, so this hint would have solved my problem if I had upgraded this morning before filing. The lint works as expected and my problem is resolved (lint removes warnings where println does not). I have a recommendation. If you are merging build.rs and executable warnings, then in principle you know that my "exe" cfg warnings are coming (in part) from build.rs. Currently, you display two recommendations*, the build.rs print and the [lint]. In the case of a cfg warning originating from a build.rs, the build.rs print! will not help and you know that ahead of time. Therefore for these warnings, I suggest you omit the build.rs print recommendation and show only the [lint] recommendation. If that is impractical, then you may close this issue. * with
|
Rollup merge of rust-lang#125412 - Urgau:check-cfg-less-build-rs, r=wesleywiser Don't suggest adding the unexpected cfgs to the build-script it-self This PR adds a check to avoid suggesting to add the unexpected cfgs inside the build-script when building the build-script it-self, as it won't have any effect, since build-scripts applies to their descended target. Fixes rust-lang#125368
EDIT: Prefixing this bug with a better explanation now that I understand better: You can register a custom cfg with
rustc-check-cfg
in two ways, with a printf in build.rs or a[lint]
in cargo.toml. However if your custom cfg is referenced in build.rs itself, the printf method does not work (you will get the warning anyway). Worse, the warning triggered by your cfg in build.rs will recommend suppressing itself by adding the printf, even though (currently?) that printf will be ineffective.There is a very new (last 2 weeks) Nightly feature which protects against typos in cfg()s by warning if an unexpected cfg is found, comparing against a list of known Rust cfgs. That's useful, but what if your project legitimately uses a cfg that the Rust project did not forsee? According to a new compiler hint added along with this feature, you can register a custom cfg in build.rs with, for example:
In my configuration, this "registration" println does not do anything.
Repro
Clone this project (branch
z_bug_exe_warning
), then rungit submodule update --recursive --init
:https://github.com/mcclure/pocket-riscv-rs-bug/tree/z_bug_exe_warning
Build with
make
orcargo +nightly build --release
(the former invokes the latter). You will see four instances of this warning:(Aside: In my tests, build warnings in this project are always printed three times, once with ANSI coloring and twice without, so you'll actually see 12 of this warning, but that's outside the scope of this bug and I think it's my fault).
Context: This project builds a video game for an unusual (and low-horsepower) embedded system (hence the submodule). I don't want large support libraries like PNG decoders on the device, so I preprocess large assets such as images in build.rs and serialize them using rkyv. This means I have a file,
rkyv_types.rs
, which contains the serialized types, and this file is included in both build.rs and the final program. So that this file can work properly in both environments, I put this in mybuild.rs
:So this means I can use
cfg(exe)
to verify I'm in the built program andcfg(not(exe))
to verify I'm in build.rs. Thisexe
is the cfgrustc-check-cfg
warns about. Fair enough, let's notifyrustc-check-cfg
of this custom cfg so we don't get the warning. At the top of build.rs main() is this line; uncomment it:(This is of course the action recommended by the hint in the warning.) Now re-run the build with
make
. You will see… the exact same four errors.The rustc-check-cfg printout does nothing.
Analysis
#[warn(unexpected_cfgs)]
. The warning seems good because it catches typos. I just don't want to be warned aboutexe
because it's not a typo.rustc-check-cfg
, because that feature only works when Rust MSRV is above 1.77, and requiring Rust 1.80 in order to banish a warning that didn't exist before Rust 1.80 seems silly. However, I did test with double colon and it didn't behave any different.rustc-check-cfg
suppressed the warnings about the surplusexe
config in the program I might still wind up facing warnings aboutexe
in the build.rs, since the println has not run at the time the build.rs is built. However I don't think this is the problem above, because in that case I would have expected the number of warnings to halve on adding the suppression line, not stay the same.exe
andnot(exe)
.Meta
The text was updated successfully, but these errors were encountered: