1
Fork 0

Rollup merge of #136016 - Urgau:check-cfg-allow-test-improv, r=jieyouxu

Improve check-cfg expected names diagnostic

This PR improves the check-cfg `allow-same-level` test by ~~normalizing it's output and by~~ adding more context to the test.

It also filters the well known cfgs from the `expected names are` note, as to reduce the size of the diagnostic. Users can still find the full list on the [rustc book](https://doc.rust-lang.org/nightly/rustc/check-cfg.html#well-known-names-and-values), which is reinforced for Cargo users by adding a note in the Cargo check-cfg specific section.

Fixes https://github.com/rust-lang/rust/issues/135995
r? `@jieyouxu`
This commit is contained in:
Matthias Krüger 2025-01-25 23:15:25 +01:00 committed by GitHub
commit dbe911a64d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 123 additions and 43 deletions

View file

@ -10,19 +10,35 @@ use crate::lints;
const MAX_CHECK_CFG_NAMES_OR_VALUES: usize = 35;
enum FilterWellKnownNames {
Yes,
No,
}
fn sort_and_truncate_possibilities(
sess: &Session,
mut possibilities: Vec<Symbol>,
filter_well_known_names: FilterWellKnownNames,
) -> (Vec<Symbol>, usize) {
let possibilities_len = possibilities.len();
let n_possibilities = if sess.opts.unstable_opts.check_cfg_all_expected {
possibilities.len()
} else {
match filter_well_known_names {
FilterWellKnownNames::Yes => {
possibilities.retain(|cfg_name| {
!sess.psess.check_config.well_known_names.contains(cfg_name)
});
}
FilterWellKnownNames::No => {}
};
std::cmp::min(possibilities.len(), MAX_CHECK_CFG_NAMES_OR_VALUES)
};
possibilities.sort_by(|s1, s2| s1.as_str().cmp(s2.as_str()));
let and_more = possibilities.len().saturating_sub(n_possibilities);
let and_more = possibilities_len.saturating_sub(n_possibilities);
possibilities.truncate(n_possibilities);
(possibilities, and_more)
}
@ -198,8 +214,10 @@ pub(super) fn unexpected_cfg_name(
} else {
vec![]
};
let (possibilities, and_more) =
sort_and_truncate_possibilities(sess, possibilities, FilterWellKnownNames::Yes);
let expected_names = if !possibilities.is_empty() {
let (possibilities, and_more) = sort_and_truncate_possibilities(sess, possibilities);
let possibilities: Vec<_> =
possibilities.into_iter().map(|s| Ident::new(s, name_span)).collect();
Some(lints::unexpected_cfg_name::ExpectedNames {
@ -269,8 +287,11 @@ pub(super) fn unexpected_cfg_value(
// for names as the possibilities could be very long
let code_sugg = if !possibilities.is_empty() {
let expected_values = {
let (possibilities, and_more) =
sort_and_truncate_possibilities(sess, possibilities.clone());
let (possibilities, and_more) = sort_and_truncate_possibilities(
sess,
possibilities.clone(),
FilterWellKnownNames::No,
);
lints::unexpected_cfg_value::ExpectedValues {
name,
have_none_possibility,