Skip to content

Commit

Permalink
Datagen: Special-case cfg(test) in registry macro to fix misleading w…
Browse files Browse the repository at this point in the history
…arnings (#4297)

Fixes #4296
  • Loading branch information
sffc authored Nov 15, 2023
1 parent 6e93719 commit b138047
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions provider/datagen/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,41 @@

use icu_provider::prelude::*;

macro_rules! make_check_all_keys {
($(#[cfg($feature:meta)] $($marker:path = $path:literal,)+)+) => {
fn check_all_keys() {
#[cfg(not(all($($feature,)+)))]
log::warn!("The icu_datagen crates has not been built with all components, so `all_keys` only returns a subset of keys");
}
};
}

/// This macro contains special handling for `#[cfg(test)]` because it is not
/// a warning if that cfg is not enabled.
///
/// The first cfg should be `#[cfg(test)]`, followed by all test markers,
/// followed by `#[cfg(feature = "...")]` and all remaining markers.
///
/// If there are no test markers, `#[cfg(test)]` should be replaced by
/// `no_cfg_test,` followed by the first `#[cfg(feature = "...")]`.
macro_rules! registry {
(#[cfg(test)] $($marker_test:path = $path_test:literal,)+ $(#[cfg($feature:meta)] $($marker:path = $path:literal,)+)+) => {
make_check_all_keys!($(#[cfg($feature)] $($marker = $path,)+)+);
// Call the macro again with #[cfg(any(test))] to get the normal code path
registry!(#[cfg(any(test))] $($marker_test = $path_test,)+ $(#[cfg($feature)] $($marker = $path,)+)+);
};
(no_cfg_test, $(#[cfg($feature:meta)] $($marker:path = $path:literal,)+)+) => {
make_check_all_keys!($(#[cfg($feature)] $($marker = $path,)+)+);
registry!($(#[cfg($feature)] $($marker = $path,)+)+);
};
($(#[cfg($feature:meta)] $($marker:path = $path:literal,)+)+) => {
/// List of all keys that are available.
///
/// Note that since v1.3, `all_keys` also contains experimental keys for which the
/// corresponding Cargo features has been enabled.
// Excludes the hello world key, as that generally should not be generated.
pub fn all_keys() -> Vec<DataKey> {
#[cfg(not(all($($feature,)+)))]
log::warn!("The icu_datagen crates has not been built with all components, so `all_keys` only returns a subset of keys");
check_all_keys();
vec![
$(
$(
Expand All @@ -24,18 +49,6 @@ macro_rules! registry {
]
}

/// Same as `all_keys`.
///
/// Note that since v1.3, `all_keys` also contains experimental keys for which the
/// corresponding Cargo features has been enabled.
///
/// ✨ *Enabled with the `legacy_api` Cargo feature.*
#[deprecated(since = "1.3.0", note = "use `all_keys` with the required cargo features")]
#[cfg(feature = "legacy_api")]
pub fn all_keys_with_experimental() -> Vec<DataKey> {
all_keys()
}

/// Parses a human-readable key identifier into a [`DataKey`].
// Supports the hello world key
/// # Example
Expand Down Expand Up @@ -140,7 +153,11 @@ macro_rules! registry {
}
}

// If `#[cfg(test)]` becomes empty, replace it with `no_cfg_test,`
registry!(
#[cfg(test)]
icu_singlenumberformatter::provider::CurrencyEssentialsV1Marker = "currency/essentials@1",
icu_unitsconversion::provider::UnitsConstantsV1Marker = "units/constants@1",
#[cfg(any(all(), feature = "icu_calendar"))]
icu_calendar::provider::JapaneseErasV1Marker = "calendar/japanese@1",
icu_calendar::provider::JapaneseExtendedErasV1Marker = "calendar/japanext@1",
Expand Down Expand Up @@ -470,10 +487,6 @@ registry!(
"relativetime/short/year@1",
icu_relativetime::provider::NarrowYearRelativeTimeFormatDataV1Marker =
"relativetime/narrow/year@1",
#[cfg(test)] // keep as test until baked data is needed
icu_singlenumberformatter::provider::CurrencyEssentialsV1Marker = "currency/essentials@1",
#[cfg(test)] // keep as test until baked data is needed
icu_unitsconversion::provider::UnitsConstantsV1Marker = "units/constants@1",
#[cfg(any(all(), feature = "icu_segmenter"))]
icu_segmenter::provider::DictionaryForWordLineExtendedV1Marker =
"segmenter/dictionary/wl_ext@1",
Expand All @@ -491,6 +504,21 @@ registry!(
icu_transliterate::provider::TransliteratorRulesV1Marker = "transliterator/rules@1",
);

/// Same as `all_keys`.
///
/// Note that since v1.3, `all_keys` also contains experimental keys for which the
/// corresponding Cargo features has been enabled.
///
/// ✨ *Enabled with the `legacy_api` Cargo feature.*
#[deprecated(
since = "1.3.0",
note = "use `all_keys` with the required cargo features"
)]
#[cfg(feature = "legacy_api")]
pub fn all_keys_with_experimental() -> Vec<DataKey> {
all_keys()
}

#[test]
fn no_key_collisions() {
let mut map = std::collections::BTreeMap::new();
Expand Down

0 comments on commit b138047

Please sign in to comment.