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

Make datagen work without CARGO_PKG_RUST_VERSION environment variable #4292

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- CLDR 44 compatibility fixes (https://github.com/unicode-org/icu4x/pull/4134, https://github.com/unicode-org/icu4x/pull/4156, https://github.com/unicode-org/icu4x/pull/4158)
- Fix `supported_locales` for collator keys (https://github.com/unicode-org/icu4x/pull/4169)
- CLI: Fix behavior of `--segmenter-lstm-root` such that it does not override `icuexportdata-root` (https://github.com/unicode-org/icu4x/pull/4277)
- Make datagen work without `CARGO_PKG_RUST_VERSION` environment variable (https://github.com/unicode-org/icu4x/pull/4292)
- Utilities
- `databake`
- Add implementations for `BTreeSet`, `BTreeMap` (https://github.com/unicode-org/icu4x/pull/4268, https://github.com/unicode-org/icu4x/pull/4274)
Expand Down
33 changes: 25 additions & 8 deletions provider/datagen/src/baked_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ macro_rules! move_out {
// TokenStream isn't Send/Sync
type SyncTokenStream = String;

const MSRV: &str = std::env!("CARGO_PKG_RUST_VERSION");
// Produces an MSRV clippy annotation if the `CARGO_PKG_RUST_VERSION` is set.
fn maybe_msrv() -> TokenStream {
std::option_env!("CARGO_PKG_RUST_VERSION")
.map(|msrv| {
quote! {
#[clippy::msrv = #msrv]
}
})
.unwrap_or_default()
}

/// Options for configuring the output of [`BakedExporter`].
#[non_exhaustive]
Expand Down Expand Up @@ -288,6 +297,8 @@ impl BakedExporter {

let prefixed_macro_ident = format!("__impl_{ident}").parse::<TokenStream>().unwrap();

let maybe_msrv = maybe_msrv();

self.write_to_file(
PathBuf::from(format!("macros/{}.rs.data", ident)),
quote! {
Expand All @@ -298,7 +309,7 @@ impl BakedExporter {
#[macro_export]
macro_rules! #prefixed_macro_ident {
($provider:ty) => {
#[clippy::msrv = #MSRV]
#maybe_msrv
const _: () = <$provider>::MUST_USE_MAKE_PROVIDER_MACRO;
#body
}
Expand Down Expand Up @@ -355,15 +366,17 @@ impl DataExporter for BakedExporter {

let bake = payload.tokenize(&self.dependencies);

let maybe_msrv = maybe_msrv();

self.write_impl_macro(quote! {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl $provider {
// Exposing singleton structs as consts allows us to get rid of fallibility
#[doc(hidden)]
pub const #singleton_ident: &'static <#marker as icu_provider::DataMarker>::Yokeable = &#bake;
}

#[clippy::msrv = #MSRV]
#maybe_msrv
impl icu_provider::DataProvider<#marker> for $provider {
fn load(
&self,
Expand Down Expand Up @@ -450,6 +463,8 @@ impl BakedExporter {
.remove(&key)
.unwrap_or_default();

let maybe_msrv = maybe_msrv();

let body = if values.is_empty() {
quote!(Err(icu_provider::DataErrorKind::MissingLocale.with_req(<#marker as icu_provider::KeyedDataMarker>::KEY, req)))
} else {
Expand Down Expand Up @@ -559,7 +574,7 @@ impl BakedExporter {

self.write_impl_macro(
quote! {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl icu_provider::DataProvider<#marker> for $provider {
fn load(
&self,
Expand Down Expand Up @@ -624,6 +639,8 @@ impl BakedExporter {
)
}));

let maybe_msrv = maybe_msrv();

// macros.rs is the interface for built-in data. It exposes one macro per data key.
self.write_to_file(
PathBuf::from("macros.rs"),
Expand All @@ -643,7 +660,7 @@ impl BakedExporter {
#[macro_export]
macro_rules! __make_provider {
($name:ty) => {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl $name {
#[doc(hidden)]
#[allow(dead_code)]
Expand Down Expand Up @@ -686,7 +703,7 @@ impl BakedExporter {
#[allow(unused_macros)]
macro_rules! impl_any_provider {
($provider:ty) => {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl icu_provider::AnyProvider for $provider {
fn load_any(&self, key: icu_provider::DataKey, req: icu_provider::DataRequest) -> Result<icu_provider::AnyResponse, icu_provider::DataError> {
match key.hashed() {
Expand All @@ -703,7 +720,7 @@ impl BakedExporter {
}

// For backwards compatibility
#[clippy::msrv = #MSRV]
#maybe_msrv
pub struct BakedDataProvider;
impl_data_provider!(BakedDataProvider);
},
Expand Down