1
Fork 0

Rename DiagnosticMessage as DiagMessage.

This commit is contained in:
Nicholas Nethercote 2024-02-29 11:58:51 +11:00
parent d849f5c225
commit 18715c98c6
49 changed files with 253 additions and 261 deletions

View file

@ -253,8 +253,8 @@ type FluentId = Cow<'static, str>;
/// translatable and non-translatable diagnostic messages.
///
/// Translatable messages for subdiagnostics are typically attributes attached to a larger Fluent
/// message so messages of this type must be combined with a `DiagnosticMessage` (using
/// `DiagnosticMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from
/// message so messages of this type must be combined with a `DiagMessage` (using
/// `DiagMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from
/// the `Subdiagnostic` derive refer to Fluent identifiers directly.
#[rustc_diagnostic_item = "SubdiagnosticMessage"]
pub enum SubdiagnosticMessage {
@ -265,7 +265,7 @@ pub enum SubdiagnosticMessage {
/// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
/// be instantiated multiple times with different values. These subdiagnostics' messages
/// are translated when they are added to the parent diagnostic, producing this variant of
/// `DiagnosticMessage`.
/// `DiagMessage`.
Translated(Cow<'static, str>),
/// Identifier of a Fluent message. Instances of this variant are generated by the
/// `Subdiagnostic` derive.
@ -299,8 +299,8 @@ impl From<Cow<'static, str>> for SubdiagnosticMessage {
///
/// Intended to be removed once diagnostics are entirely translatable.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
#[rustc_diagnostic_item = "DiagnosticMessage"]
pub enum DiagnosticMessage {
#[rustc_diagnostic_item = "DiagMessage"]
pub enum DiagMessage {
/// Non-translatable diagnostic message.
Str(Cow<'static, str>),
/// Translatable message which has been already translated.
@ -308,7 +308,7 @@ pub enum DiagnosticMessage {
/// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
/// be instantiated multiple times with different values. These subdiagnostics' messages
/// are translated when they are added to the parent diagnostic, producing this variant of
/// `DiagnosticMessage`.
/// `DiagMessage`.
Translated(Cow<'static, str>),
/// Identifier for a Fluent message (with optional attribute) corresponding to the diagnostic
/// message. Yet to be translated.
@ -318,85 +318,81 @@ pub enum DiagnosticMessage {
FluentIdentifier(FluentId, Option<FluentId>),
}
impl DiagnosticMessage {
impl DiagMessage {
/// Given a `SubdiagnosticMessage` which may contain a Fluent attribute, create a new
/// `DiagnosticMessage` that combines that attribute with the Fluent identifier of `self`.
/// `DiagMessage` that combines that attribute with the Fluent identifier of `self`.
///
/// - If the `SubdiagnosticMessage` is non-translatable then return the message as a
/// `DiagnosticMessage`.
/// `DiagMessage`.
/// - If `self` is non-translatable then return `self`'s message.
pub fn with_subdiagnostic_message(&self, sub: SubdiagnosticMessage) -> Self {
let attr = match sub {
SubdiagnosticMessage::Str(s) => return DiagnosticMessage::Str(s),
SubdiagnosticMessage::Translated(s) => return DiagnosticMessage::Translated(s),
SubdiagnosticMessage::Str(s) => return DiagMessage::Str(s),
SubdiagnosticMessage::Translated(s) => return DiagMessage::Translated(s),
SubdiagnosticMessage::FluentIdentifier(id) => {
return DiagnosticMessage::FluentIdentifier(id, None);
return DiagMessage::FluentIdentifier(id, None);
}
SubdiagnosticMessage::FluentAttr(attr) => attr,
};
match self {
DiagnosticMessage::Str(s) => DiagnosticMessage::Str(s.clone()),
DiagnosticMessage::Translated(s) => DiagnosticMessage::Translated(s.clone()),
DiagnosticMessage::FluentIdentifier(id, _) => {
DiagnosticMessage::FluentIdentifier(id.clone(), Some(attr))
DiagMessage::Str(s) => DiagMessage::Str(s.clone()),
DiagMessage::Translated(s) => DiagMessage::Translated(s.clone()),
DiagMessage::FluentIdentifier(id, _) => {
DiagMessage::FluentIdentifier(id.clone(), Some(attr))
}
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
DiagnosticMessage::Translated(s) | DiagnosticMessage::Str(s) => Some(s),
DiagnosticMessage::FluentIdentifier(_, _) => None,
DiagMessage::Translated(s) | DiagMessage::Str(s) => Some(s),
DiagMessage::FluentIdentifier(_, _) => None,
}
}
}
impl From<String> for DiagnosticMessage {
impl From<String> for DiagMessage {
fn from(s: String) -> Self {
DiagnosticMessage::Str(Cow::Owned(s))
DiagMessage::Str(Cow::Owned(s))
}
}
impl From<&'static str> for DiagnosticMessage {
impl From<&'static str> for DiagMessage {
fn from(s: &'static str) -> Self {
DiagnosticMessage::Str(Cow::Borrowed(s))
DiagMessage::Str(Cow::Borrowed(s))
}
}
impl From<Cow<'static, str>> for DiagnosticMessage {
impl From<Cow<'static, str>> for DiagMessage {
fn from(s: Cow<'static, str>) -> Self {
DiagnosticMessage::Str(s)
DiagMessage::Str(s)
}
}
/// A workaround for must_produce_diag ICEs when formatting types in disabled lints.
///
/// Delays formatting until `.into(): DiagnosticMessage` is used.
/// Delays formatting until `.into(): DiagMessage` is used.
pub struct DelayDm<F>(pub F);
impl<F: FnOnce() -> String> From<DelayDm<F>> for DiagnosticMessage {
impl<F: FnOnce() -> String> From<DelayDm<F>> for DiagMessage {
fn from(DelayDm(f): DelayDm<F>) -> Self {
DiagnosticMessage::from(f())
DiagMessage::from(f())
}
}
/// Translating *into* a subdiagnostic message from a diagnostic message is a little strange - but
/// the subdiagnostic functions (e.g. `span_label`) take a `SubdiagnosticMessage` and the
/// subdiagnostic derive refers to typed identifiers that are `DiagnosticMessage`s, so need to be
/// able to convert between these, as much as they'll be converted back into `DiagnosticMessage`
/// subdiagnostic derive refers to typed identifiers that are `DiagMessage`s, so need to be
/// able to convert between these, as much as they'll be converted back into `DiagMessage`
/// using `with_subdiagnostic_message` eventually. Don't use this other than for the derive.
impl Into<SubdiagnosticMessage> for DiagnosticMessage {
impl Into<SubdiagnosticMessage> for DiagMessage {
fn into(self) -> SubdiagnosticMessage {
match self {
DiagnosticMessage::Str(s) => SubdiagnosticMessage::Str(s),
DiagnosticMessage::Translated(s) => SubdiagnosticMessage::Translated(s),
DiagnosticMessage::FluentIdentifier(id, None) => {
SubdiagnosticMessage::FluentIdentifier(id)
}
DiagMessage::Str(s) => SubdiagnosticMessage::Str(s),
DiagMessage::Translated(s) => SubdiagnosticMessage::Translated(s),
DiagMessage::FluentIdentifier(id, None) => SubdiagnosticMessage::FluentIdentifier(id),
// There isn't really a sensible behaviour for this because it loses information but
// this is the most sensible of the behaviours.
DiagnosticMessage::FluentIdentifier(_, Some(attr)) => {
SubdiagnosticMessage::FluentAttr(attr)
}
DiagMessage::FluentIdentifier(_, Some(attr)) => SubdiagnosticMessage::FluentAttr(attr),
}
}
}
@ -412,7 +408,7 @@ pub struct SpanLabel {
pub is_primary: bool,
/// What label should we attach to this span (if any)?
pub label: Option<DiagnosticMessage>,
pub label: Option<DiagMessage>,
}
/// A collection of `Span`s.
@ -426,7 +422,7 @@ pub struct SpanLabel {
#[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable)]
pub struct MultiSpan {
primary_spans: Vec<Span>,
span_labels: Vec<(Span, DiagnosticMessage)>,
span_labels: Vec<(Span, DiagMessage)>,
}
impl MultiSpan {
@ -444,7 +440,7 @@ impl MultiSpan {
MultiSpan { primary_spans: vec, span_labels: vec![] }
}
pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagnosticMessage>) {
pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagMessage>) {
self.span_labels.push((span, label.into()));
}
@ -487,7 +483,7 @@ impl MultiSpan {
replacements_occurred
}
pub fn pop_span_label(&mut self) -> Option<(Span, DiagnosticMessage)> {
pub fn pop_span_label(&mut self) -> Option<(Span, DiagMessage)> {
self.span_labels.pop()
}