Skip to content

Change ConflictingField error to contain the previously loaded field #6480

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

Merged
merged 2 commits into from
Apr 21, 2025
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
9 changes: 7 additions & 2 deletions components/datetime/src/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ pub enum PatternLoadError {
/// Fields conflict if they require the same type of data, for example the
/// `EEE` and `EEEE` fields (short vs long weekday) conflict, or the `M`
/// and `L` (format vs standalone month) conflict.
#[displaydoc("A field {0:?} conflicts with a previous field.")]
ConflictingField(ErrorField),
#[displaydoc("A field {field:?} conflicts with a previously loaded field {previous_field:?}.")]
ConflictingField {
/// The field that was not able to be loaded.
field: ErrorField,
/// The field that prevented the new field from being loaded.
previous_field: ErrorField,
},
/// The field symbol is not supported in that length.
///
/// Some fields, such as `O` are not defined for all lengths (e.g. `OO`).
Expand Down
12 changes: 6 additions & 6 deletions components/datetime/src/pattern/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ impl<C: CldrCalendar, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, F
/// // But loading a new length fails:
/// assert!(matches!(
/// names.include_year_names(YearNameLength::Abbreviated),
/// Err(PatternLoadError::ConflictingField(_))
/// Err(PatternLoadError::ConflictingField { .. })
/// ));
/// ```
#[cfg(feature = "compiled_data")]
Expand Down Expand Up @@ -1309,11 +1309,11 @@ impl<C: CldrCalendar, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, F
/// // But loading a new symbol or length fails:
/// assert!(matches!(
/// names.include_month_names(MonthNameLength::StandaloneWide),
/// Err(PatternLoadError::ConflictingField(_))
/// Err(PatternLoadError::ConflictingField { .. })
/// ));
/// assert!(matches!(
/// names.include_month_names(MonthNameLength::Abbreviated),
/// Err(PatternLoadError::ConflictingField(_))
/// Err(PatternLoadError::ConflictingField { .. })
/// ));
/// ```
#[cfg(feature = "compiled_data")]
Expand Down Expand Up @@ -1382,7 +1382,7 @@ impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
/// // But loading a new length fails:
/// assert!(matches!(
/// names.include_day_period_names(DayPeriodNameLength::Abbreviated),
/// Err(PatternLoadError::ConflictingField(_))
/// Err(PatternLoadError::ConflictingField { .. })
/// ));
/// ```
#[cfg(feature = "compiled_data")]
Expand Down Expand Up @@ -1445,11 +1445,11 @@ impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
/// // But loading a new symbol or length fails:
/// assert!(matches!(
/// names.include_weekday_names(WeekdayNameLength::StandaloneWide),
/// Err(PatternLoadError::ConflictingField(_))
/// Err(PatternLoadError::ConflictingField { .. })
/// ));
/// assert!(matches!(
/// names.include_weekday_names(WeekdayNameLength::Abbreviated),
/// Err(PatternLoadError::ConflictingField(_))
/// Err(PatternLoadError::ConflictingField { .. })
/// ));
/// ```
#[cfg(feature = "compiled_data")]
Expand Down
69 changes: 61 additions & 8 deletions components/datetime/src/scaffold/names_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ pub trait DateTimeNamesMarker: UnstableSealed {
type MetazoneLookup: NamesContainer<tz::MzPeriodV1, ()>;
}

/// A trait for `Variables` that can be converted to [`ErrorField`]
pub trait MaybeAsErrorField: UnstableSealed {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I think this should be Sealed not UnstableSealed: I don't see a reason for users to be able to overwrite this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #6475, I think there are no more traits in datetime that don't use UnstableSealed. Why is this trait more special than all the other scaffolding traits?

That said, this type is only useful when applied to the handful of types that are used as Variable, which are fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trait also has an invariant that resolves to a debug assert, which could be an argument that it is in a different class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this trait more special than all the other scaffolding traits?

Justification was provided for the other scaffolding traits.

As a reminder:

@robertbastian was of the opinion that GetField was the only one that should be exposed. We didn't discuss that disagreement in depth then, I think.

To me, consistency is an argument when user confusion is on the cards (unlikely, users aren't supposed to be looking at these), or when it's a sign for some underlying issue. The latter is possible, but I'm not really saying we have the right ontology here, just that I don't wish to expand the set for 2.0 without justification.

A valid justification here would state why a custom FooLength class would be made by a user. I can't immediately see that, I don't think it doesn't exist, but I don't wish to spend time thinking about it.

fn maybe_as_error_field(&self) -> Option<ErrorField>;
}

impl MaybeAsErrorField for () {
fn maybe_as_error_field(&self) -> Option<ErrorField> {
None
}
}

impl UnstableSealed for YearNameLength {}
impl MaybeAsErrorField for YearNameLength {
fn maybe_as_error_field(&self) -> Option<ErrorField> {
Some(self.to_approximate_error_field())
}
}

impl UnstableSealed for MonthNameLength {}
impl MaybeAsErrorField for MonthNameLength {
fn maybe_as_error_field(&self) -> Option<ErrorField> {
Some(self.to_approximate_error_field())
}
}

impl UnstableSealed for WeekdayNameLength {}
impl MaybeAsErrorField for WeekdayNameLength {
fn maybe_as_error_field(&self) -> Option<ErrorField> {
Some(self.to_approximate_error_field())
}
}

impl UnstableSealed for DayPeriodNameLength {}
impl MaybeAsErrorField for DayPeriodNameLength {
fn maybe_as_error_field(&self) -> Option<ErrorField> {
Some(self.to_approximate_error_field())
}
}

/// Trait that associates a container for a payload parameterized by the given variables.
///
/// <div class="stab unstable">
Expand All @@ -69,7 +108,7 @@ macro_rules! impl_holder_trait {
impl UnstableSealed for $marker {}
impl<Variables> NamesContainer<$marker, Variables> for $marker
where
Variables: PartialEq + Copy + fmt::Debug,
Variables: PartialEq + Copy + MaybeAsErrorField + fmt::Debug,
{
type Container = DataPayloadWithVariables<$marker, Variables>;
}
Expand Down Expand Up @@ -97,10 +136,10 @@ impl_holder_trait!(tz::MzPeriodV1);
#[derive(Debug, displaydoc::Display)]
#[non_exhaustive]
pub enum MaybePayloadError {
/// TODO
/// The container's field set doesn't support the field
FormatterTooSpecific,
/// TODO
ConflictingField,
/// The field is already loaded with a different length
ConflictingField(ErrorField),
}

impl core::error::Error for MaybePayloadError {}
Expand All @@ -109,7 +148,10 @@ impl MaybePayloadError {
pub(crate) fn into_load_error(self, error_field: ErrorField) -> PatternLoadError {
match self {
Self::FormatterTooSpecific => PatternLoadError::FormatterTooSpecific(error_field),
Self::ConflictingField => PatternLoadError::ConflictingField(error_field),
Self::ConflictingField(loaded_field) => PatternLoadError::ConflictingField {
field: error_field,
previous_field: loaded_field,
},
}
}
}
Expand Down Expand Up @@ -200,7 +242,7 @@ where
impl<M: DynamicDataMarker, Variables> MaybePayload<M, Variables>
for DataPayloadWithVariables<M, Variables>
where
Variables: PartialEq + Copy,
Variables: PartialEq + Copy + MaybeAsErrorField,
{
#[inline]
fn new_empty() -> Self {
Expand All @@ -224,8 +266,19 @@ where
// TODO(#6063): probably not correct
return Ok(Ok(Default::default()));
}
OptionalNames::SingleLength { .. } => {
return Err(MaybePayloadError::ConflictingField);
OptionalNames::SingleLength { variables, .. } => {
let loaded_field = match variables.maybe_as_error_field() {
Some(x) => x,
None => {
debug_assert!(false, "all non-unit variables implement this trait");
use crate::provider::fields::*;
ErrorField(Field {
symbol: FieldSymbol::Era,
length: FieldLength::Six,
})
}
};
return Err(MaybePayloadError::ConflictingField(loaded_field));
}
OptionalNames::None => (),
};
Expand Down
6 changes: 4 additions & 2 deletions ffi/capi/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl From<icu_datetime::DateTimeFormatterLoadError> for DateTimeFormatterLoadErr
fn from(e: icu_datetime::DateTimeFormatterLoadError) -> Self {
match e {
icu_datetime::DateTimeFormatterLoadError::Names(
icu_datetime::pattern::PatternLoadError::ConflictingField(_),
icu_datetime::pattern::PatternLoadError::ConflictingField { .. },
) => Self::ConflictingField,
icu_datetime::DateTimeFormatterLoadError::Names(
icu_datetime::pattern::PatternLoadError::UnsupportedLength(_),
Expand Down Expand Up @@ -246,7 +246,9 @@ impl From<icu_provider::DataError> for DateTimeFormatterLoadError {
impl From<icu_datetime::pattern::PatternLoadError> for ffi::DateTimeFormatterLoadError {
fn from(value: icu_datetime::pattern::PatternLoadError) -> Self {
match value {
icu_datetime::pattern::PatternLoadError::ConflictingField(_) => Self::ConflictingField,
icu_datetime::pattern::PatternLoadError::ConflictingField { .. } => {
Self::ConflictingField
}
icu_datetime::pattern::PatternLoadError::UnsupportedLength(_) => {
Self::UnsupportedLength
}
Expand Down