Skip to content

Commit 9a9ad65

Browse files
authored
Unrolled build for rust-lang#127221
Rollup merge of rust-lang#127221 - Urgau:check-cfg-std-diag, r=pnkfelix Improve well known value check-cfg diagnostic for the standard library This PR adjust the current logic for hidding the rustc/Cargo suggestion to add a value to a well-known name to exclude the standard library and rustc crates. This is done in order to improve the contributor experience, in particular when adding a new target, which often requires adding some cfgs like `target_os` which may not be available yet in stage0. <details> The diagnostic code would look like this. ```text error: unexpected `cfg` condition value: `blable` --> library/core/src/lib.rs:369:7 | 369 | #[cfg(target_os = "blable")] | ^^^^^^^^^^^^^^^^^^^^ | = note: expected values for `target_os` are: `aix`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `openbsd`, `psp`, `redox`, `solaris`, `solid_asp3`, `teeos`, `tvos`, `uefi`, `unknown`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, and `windows` and 2 more = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("blable"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(target_os, values(\"blable\"))");` to the top of the `build.rs` = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration = note: `-D unexpected-cfgs` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unexpected_cfgs)]` ``` </details>
2 parents 5c08cc7 + 9e12d91 commit 9a9ad65

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,16 @@ pub(super) fn unexpected_cfg_value(
261261
lints::unexpected_cfg_value::CodeSuggestion::RemoveCondition { suggestion, name }
262262
};
263263

264-
// We don't want to suggest adding values to well known names
265-
// since those are defined by rustc it-self. Users can still
266-
// do it if they want, but should not encourage them.
267-
let is_cfg_a_well_know_name = sess.psess.check_config.well_known_names.contains(&name);
264+
// We don't want to encourage people to add values to a well-known names, as these are
265+
// defined by rustc/Rust itself. Users can still do this if they wish, but should not be
266+
// encouraged to do so.
267+
let can_suggest_adding_value = !sess.psess.check_config.well_known_names.contains(&name)
268+
// Except when working on rustc or the standard library itself, in which case we want to
269+
// suggest adding these cfgs to the "normal" place because of bootstraping reasons. As a
270+
// basic heuristic, we use the "cheat" unstable feature enable method and the
271+
// non-ui-testing enabled option.
272+
|| (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
273+
&& !sess.opts.unstable_opts.ui_testing);
268274

269275
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
270276

@@ -275,14 +281,14 @@ pub(super) fn unexpected_cfg_value(
275281
} else {
276282
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
277283
}
278-
} else if !is_cfg_a_well_know_name {
284+
} else if can_suggest_adding_value {
279285
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst)))
280286
} else {
281287
None
282288
};
283289
lints::unexpected_cfg_value::InvocationHelp::Cargo(help)
284290
} else {
285-
let help = if !is_cfg_a_well_know_name {
291+
let help = if can_suggest_adding_value {
286292
Some(lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)))
287293
} else {
288294
None

0 commit comments

Comments
 (0)