mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
errors: simplify referring to fluent attributes
To render the message of a Fluent attribute, the identifier of the Fluent message must be known. `DiagnosticMessage::FluentIdentifier` contains both the message's identifier and optionally the identifier of an attribute. Generated constants for each attribute would therefore need to be named uniquely (amongst all error messages) or be able to refer to only the attribute identifier which will be combined with a message identifier later. In this commit, the latter strategy is implemented as part of the `Diagnostic` type's functions for adding subdiagnostics of various kinds. Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
855fc022fe
commit
f669b78ffc
@ -234,6 +234,48 @@ pub fn fallback_fluent_bundle(
|
|||||||
/// Identifier for the Fluent message/attribute corresponding to a diagnostic message.
|
/// Identifier for the Fluent message/attribute corresponding to a diagnostic message.
|
||||||
type FluentId = Cow<'static, str>;
|
type FluentId = Cow<'static, str>;
|
||||||
|
|
||||||
|
/// Abstraction over a message in a subdiagnostic (i.e. label, note, help, etc) to support both
|
||||||
|
/// 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
|
||||||
|
/// the `SessionSubdiagnostic` derive refer to Fluent identifiers directly.
|
||||||
|
pub enum SubdiagnosticMessage {
|
||||||
|
/// Non-translatable diagnostic message.
|
||||||
|
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
|
||||||
|
Str(String),
|
||||||
|
/// Identifier of a Fluent message. Instances of this variant are generated by the
|
||||||
|
/// `SessionSubdiagnostic` derive.
|
||||||
|
FluentIdentifier(FluentId),
|
||||||
|
/// Attribute of a Fluent message. Needs to be combined with a Fluent identifier to produce an
|
||||||
|
/// actual translated message. Instances of this variant are generated by the `fluent_messages`
|
||||||
|
/// macro.
|
||||||
|
///
|
||||||
|
/// <https://projectfluent.org/fluent/guide/attributes.html>
|
||||||
|
FluentAttr(FluentId),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SubdiagnosticMessage {
|
||||||
|
/// Create a `SubdiagnosticMessage` for the provided Fluent attribute.
|
||||||
|
pub fn attr(id: impl Into<FluentId>) -> Self {
|
||||||
|
SubdiagnosticMessage::FluentAttr(id.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a `SubdiagnosticMessage` for the provided Fluent identifier.
|
||||||
|
pub fn message(id: impl Into<FluentId>) -> Self {
|
||||||
|
SubdiagnosticMessage::FluentIdentifier(id.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `From` impl that enables existing diagnostic calls to functions which now take
|
||||||
|
/// `impl Into<SubdiagnosticMessage>` to continue to work as before.
|
||||||
|
impl<S: Into<String>> From<S> for SubdiagnosticMessage {
|
||||||
|
fn from(s: S) -> Self {
|
||||||
|
SubdiagnosticMessage::Str(s.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Abstraction over a message in a diagnostic to support both translatable and non-translatable
|
/// Abstraction over a message in a diagnostic to support both translatable and non-translatable
|
||||||
/// diagnostic messages.
|
/// diagnostic messages.
|
||||||
///
|
///
|
||||||
@ -252,6 +294,29 @@ pub enum DiagnosticMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DiagnosticMessage {
|
impl DiagnosticMessage {
|
||||||
|
/// Given a `SubdiagnosticMessage` which may contain a Fluent attribute, create a new
|
||||||
|
/// `DiagnosticMessage` that combines that attribute with the Fluent identifier of `self`.
|
||||||
|
///
|
||||||
|
/// - If the `SubdiagnosticMessage` is non-translatable then return the message as a
|
||||||
|
/// `DiagnosticMessage`.
|
||||||
|
/// - 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.clone()),
|
||||||
|
SubdiagnosticMessage::FluentIdentifier(id) => {
|
||||||
|
return DiagnosticMessage::FluentIdentifier(id, None);
|
||||||
|
}
|
||||||
|
SubdiagnosticMessage::FluentAttr(attr) => attr,
|
||||||
|
};
|
||||||
|
|
||||||
|
match self {
|
||||||
|
DiagnosticMessage::Str(s) => DiagnosticMessage::Str(s.clone()),
|
||||||
|
DiagnosticMessage::FluentIdentifier(id, _) => {
|
||||||
|
DiagnosticMessage::FluentIdentifier(id.clone(), Some(attr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the `String` contained within the `DiagnosticMessage::Str` variant, assuming that
|
/// Returns the `String` contained within the `DiagnosticMessage::Str` variant, assuming that
|
||||||
/// this diagnostic message is of the legacy, non-translatable variety. Panics if this
|
/// this diagnostic message is of the legacy, non-translatable variety. Panics if this
|
||||||
/// assumption does not hold.
|
/// assumption does not hold.
|
||||||
@ -266,14 +331,9 @@ impl DiagnosticMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `DiagnosticMessage` for the provided Fluent identifier.
|
/// Create a `DiagnosticMessage` for the provided Fluent identifier.
|
||||||
pub fn fluent(id: impl Into<FluentId>) -> Self {
|
pub fn new(id: impl Into<FluentId>) -> Self {
|
||||||
DiagnosticMessage::FluentIdentifier(id.into(), None)
|
DiagnosticMessage::FluentIdentifier(id.into(), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `DiagnosticMessage` for the provided Fluent identifier and attribute.
|
|
||||||
pub fn fluent_attr(id: impl Into<FluentId>, attr: impl Into<FluentId>) -> Self {
|
|
||||||
DiagnosticMessage::FluentIdentifier(id.into(), Some(attr.into()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `From` impl that enables existing diagnostic calls to functions which now take
|
/// `From` impl that enables existing diagnostic calls to functions which now take
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::snippet::Style;
|
use crate::snippet::Style;
|
||||||
use crate::{
|
use crate::{
|
||||||
CodeSuggestion, DiagnosticMessage, Level, MultiSpan, Substitution, SubstitutionPart,
|
CodeSuggestion, DiagnosticMessage, Level, MultiSpan, SubdiagnosticMessage, Substitution,
|
||||||
SuggestionStyle,
|
SubstitutionPart, SuggestionStyle,
|
||||||
};
|
};
|
||||||
use rustc_data_structures::stable_map::FxHashMap;
|
use rustc_data_structures::stable_map::FxHashMap;
|
||||||
use rustc_error_messages::FluentValue;
|
use rustc_error_messages::FluentValue;
|
||||||
@ -283,8 +283,8 @@ impl Diagnostic {
|
|||||||
///
|
///
|
||||||
/// This span is *not* considered a ["primary span"][`MultiSpan`]; only
|
/// This span is *not* considered a ["primary span"][`MultiSpan`]; only
|
||||||
/// the `Span` supplied when creating the diagnostic is primary.
|
/// the `Span` supplied when creating the diagnostic is primary.
|
||||||
pub fn span_label(&mut self, span: Span, label: impl Into<DiagnosticMessage>) -> &mut Self {
|
pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
||||||
self.span.push_span_label(span, label.into());
|
self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,12 +401,12 @@ impl Diagnostic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add a note attached to this diagnostic.
|
/// Add a note attached to this diagnostic.
|
||||||
pub fn note(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
|
pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
||||||
self.sub(Level::Note, msg, MultiSpan::new(), None);
|
self.sub(Level::Note, msg, MultiSpan::new(), None);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn highlighted_note<M: Into<DiagnosticMessage>>(
|
pub fn highlighted_note<M: Into<SubdiagnosticMessage>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: Vec<(M, Style)>,
|
msg: Vec<(M, Style)>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -416,7 +416,7 @@ impl Diagnostic {
|
|||||||
|
|
||||||
/// Prints the span with a note above it.
|
/// Prints the span with a note above it.
|
||||||
/// This is like [`Diagnostic::note()`], but it gets its own span.
|
/// This is like [`Diagnostic::note()`], but it gets its own span.
|
||||||
pub fn note_once(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
|
pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
||||||
self.sub(Level::OnceNote, msg, MultiSpan::new(), None);
|
self.sub(Level::OnceNote, msg, MultiSpan::new(), None);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -426,7 +426,7 @@ impl Diagnostic {
|
|||||||
pub fn span_note<S: Into<MultiSpan>>(
|
pub fn span_note<S: Into<MultiSpan>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: S,
|
sp: S,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
self.sub(Level::Note, msg, sp.into(), None);
|
self.sub(Level::Note, msg, sp.into(), None);
|
||||||
self
|
self
|
||||||
@ -437,14 +437,14 @@ impl Diagnostic {
|
|||||||
pub fn span_note_once<S: Into<MultiSpan>>(
|
pub fn span_note_once<S: Into<MultiSpan>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: S,
|
sp: S,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
self.sub(Level::OnceNote, msg, sp.into(), None);
|
self.sub(Level::OnceNote, msg, sp.into(), None);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a warning attached to this diagnostic.
|
/// Add a warning attached to this diagnostic.
|
||||||
pub fn warn(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
|
pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
||||||
self.sub(Level::Warning, msg, MultiSpan::new(), None);
|
self.sub(Level::Warning, msg, MultiSpan::new(), None);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -454,14 +454,14 @@ impl Diagnostic {
|
|||||||
pub fn span_warn<S: Into<MultiSpan>>(
|
pub fn span_warn<S: Into<MultiSpan>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: S,
|
sp: S,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
self.sub(Level::Warning, msg, sp.into(), None);
|
self.sub(Level::Warning, msg, sp.into(), None);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a help message attached to this diagnostic.
|
/// Add a help message attached to this diagnostic.
|
||||||
pub fn help(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
|
pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
||||||
self.sub(Level::Help, msg, MultiSpan::new(), None);
|
self.sub(Level::Help, msg, MultiSpan::new(), None);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -477,7 +477,7 @@ impl Diagnostic {
|
|||||||
pub fn span_help<S: Into<MultiSpan>>(
|
pub fn span_help<S: Into<MultiSpan>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: S,
|
sp: S,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
self.sub(Level::Help, msg, sp.into(), None);
|
self.sub(Level::Help, msg, sp.into(), None);
|
||||||
self
|
self
|
||||||
@ -514,7 +514,7 @@ impl Diagnostic {
|
|||||||
/// In other words, multiple changes need to be applied as part of this suggestion.
|
/// In other words, multiple changes need to be applied as part of this suggestion.
|
||||||
pub fn multipart_suggestion(
|
pub fn multipart_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -530,7 +530,7 @@ impl Diagnostic {
|
|||||||
/// In other words, multiple changes need to be applied as part of this suggestion.
|
/// In other words, multiple changes need to be applied as part of this suggestion.
|
||||||
pub fn multipart_suggestion_verbose(
|
pub fn multipart_suggestion_verbose(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -544,7 +544,7 @@ impl Diagnostic {
|
|||||||
/// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
|
/// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
|
||||||
pub fn multipart_suggestion_with_style(
|
pub fn multipart_suggestion_with_style(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
style: SuggestionStyle,
|
style: SuggestionStyle,
|
||||||
@ -557,7 +557,7 @@ impl Diagnostic {
|
|||||||
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
||||||
.collect(),
|
.collect(),
|
||||||
}],
|
}],
|
||||||
msg: msg.into(),
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
||||||
style,
|
style,
|
||||||
applicability,
|
applicability,
|
||||||
});
|
});
|
||||||
@ -572,7 +572,7 @@ impl Diagnostic {
|
|||||||
/// improve understandability.
|
/// improve understandability.
|
||||||
pub fn tool_only_multipart_suggestion(
|
pub fn tool_only_multipart_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -584,7 +584,7 @@ impl Diagnostic {
|
|||||||
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
||||||
.collect(),
|
.collect(),
|
||||||
}],
|
}],
|
||||||
msg: msg.into(),
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
||||||
style: SuggestionStyle::CompletelyHidden,
|
style: SuggestionStyle::CompletelyHidden,
|
||||||
applicability,
|
applicability,
|
||||||
});
|
});
|
||||||
@ -611,7 +611,7 @@ impl Diagnostic {
|
|||||||
pub fn span_suggestion(
|
pub fn span_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -629,7 +629,7 @@ impl Diagnostic {
|
|||||||
pub fn span_suggestion_with_style(
|
pub fn span_suggestion_with_style(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
style: SuggestionStyle,
|
style: SuggestionStyle,
|
||||||
@ -638,7 +638,7 @@ impl Diagnostic {
|
|||||||
substitutions: vec![Substitution {
|
substitutions: vec![Substitution {
|
||||||
parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
|
parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
|
||||||
}],
|
}],
|
||||||
msg: msg.into(),
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
||||||
style,
|
style,
|
||||||
applicability,
|
applicability,
|
||||||
});
|
});
|
||||||
@ -649,7 +649,7 @@ impl Diagnostic {
|
|||||||
pub fn span_suggestion_verbose(
|
pub fn span_suggestion_verbose(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -668,7 +668,7 @@ impl Diagnostic {
|
|||||||
pub fn span_suggestions(
|
pub fn span_suggestions(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestions: impl Iterator<Item = String>,
|
suggestions: impl Iterator<Item = String>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -680,7 +680,7 @@ impl Diagnostic {
|
|||||||
.collect();
|
.collect();
|
||||||
self.push_suggestion(CodeSuggestion {
|
self.push_suggestion(CodeSuggestion {
|
||||||
substitutions,
|
substitutions,
|
||||||
msg: msg.into(),
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
||||||
style: SuggestionStyle::ShowCode,
|
style: SuggestionStyle::ShowCode,
|
||||||
applicability,
|
applicability,
|
||||||
});
|
});
|
||||||
@ -691,7 +691,7 @@ impl Diagnostic {
|
|||||||
/// See also [`Diagnostic::span_suggestion()`].
|
/// See also [`Diagnostic::span_suggestion()`].
|
||||||
pub fn multipart_suggestions(
|
pub fn multipart_suggestions(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestions: impl Iterator<Item = Vec<(Span, String)>>,
|
suggestions: impl Iterator<Item = Vec<(Span, String)>>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -704,7 +704,7 @@ impl Diagnostic {
|
|||||||
.collect(),
|
.collect(),
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
msg: msg.into(),
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
||||||
style: SuggestionStyle::ShowCode,
|
style: SuggestionStyle::ShowCode,
|
||||||
applicability,
|
applicability,
|
||||||
});
|
});
|
||||||
@ -717,7 +717,7 @@ impl Diagnostic {
|
|||||||
pub fn span_suggestion_short(
|
pub fn span_suggestion_short(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -740,7 +740,7 @@ impl Diagnostic {
|
|||||||
pub fn span_suggestion_hidden(
|
pub fn span_suggestion_hidden(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -761,7 +761,7 @@ impl Diagnostic {
|
|||||||
pub fn tool_only_span_suggestion(
|
pub fn tool_only_span_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
@ -831,6 +831,18 @@ impl Diagnostic {
|
|||||||
&self.message
|
&self.message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
|
||||||
|
/// combining it with the primary message of the diagnostic (if translatable, otherwise it just
|
||||||
|
/// passes the user's string along).
|
||||||
|
fn subdiagnostic_message_to_diagnostic_message(
|
||||||
|
&self,
|
||||||
|
attr: impl Into<SubdiagnosticMessage>,
|
||||||
|
) -> DiagnosticMessage {
|
||||||
|
let msg =
|
||||||
|
self.message.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
|
||||||
|
msg.with_subdiagnostic_message(attr.into())
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience function for internal use, clients should use one of the
|
/// Convenience function for internal use, clients should use one of the
|
||||||
/// public methods above.
|
/// public methods above.
|
||||||
///
|
///
|
||||||
@ -838,13 +850,16 @@ impl Diagnostic {
|
|||||||
pub fn sub(
|
pub fn sub(
|
||||||
&mut self,
|
&mut self,
|
||||||
level: Level,
|
level: Level,
|
||||||
message: impl Into<DiagnosticMessage>,
|
message: impl Into<SubdiagnosticMessage>,
|
||||||
span: MultiSpan,
|
span: MultiSpan,
|
||||||
render_span: Option<MultiSpan>,
|
render_span: Option<MultiSpan>,
|
||||||
) {
|
) {
|
||||||
let sub = SubDiagnostic {
|
let sub = SubDiagnostic {
|
||||||
level,
|
level,
|
||||||
message: vec![(message.into(), Style::NoStyle)],
|
message: vec![(
|
||||||
|
self.subdiagnostic_message_to_diagnostic_message(message),
|
||||||
|
Style::NoStyle,
|
||||||
|
)],
|
||||||
span,
|
span,
|
||||||
render_span,
|
render_span,
|
||||||
};
|
};
|
||||||
@ -853,14 +868,17 @@ impl Diagnostic {
|
|||||||
|
|
||||||
/// Convenience function for internal use, clients should use one of the
|
/// Convenience function for internal use, clients should use one of the
|
||||||
/// public methods above.
|
/// public methods above.
|
||||||
fn sub_with_highlights<M: Into<DiagnosticMessage>>(
|
fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
level: Level,
|
level: Level,
|
||||||
mut message: Vec<(M, Style)>,
|
mut message: Vec<(M, Style)>,
|
||||||
span: MultiSpan,
|
span: MultiSpan,
|
||||||
render_span: Option<MultiSpan>,
|
render_span: Option<MultiSpan>,
|
||||||
) {
|
) {
|
||||||
let message = message.drain(..).map(|m| (m.0.into(), m.1)).collect();
|
let message = message
|
||||||
|
.drain(..)
|
||||||
|
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1))
|
||||||
|
.collect();
|
||||||
let sub = SubDiagnostic { level, message, span, render_span };
|
let sub = SubDiagnostic { level, message, span, render_span };
|
||||||
self.children.push(sub);
|
self.children.push(sub);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
use crate::diagnostic::IntoDiagnosticArg;
|
use crate::diagnostic::IntoDiagnosticArg;
|
||||||
use crate::{Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed};
|
use crate::{
|
||||||
|
Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed,
|
||||||
|
SubdiagnosticMessage,
|
||||||
|
};
|
||||||
use crate::{Handler, Level, MultiSpan, StashKey};
|
use crate::{Handler, Level, MultiSpan, StashKey};
|
||||||
use rustc_lint_defs::Applicability;
|
use rustc_lint_defs::Applicability;
|
||||||
|
|
||||||
@ -395,7 +398,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
|||||||
/// the diagnostic was constructed. However, the label span is *not* considered a
|
/// the diagnostic was constructed. However, the label span is *not* considered a
|
||||||
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
|
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
|
||||||
/// primary.
|
/// primary.
|
||||||
pub fn span_label(&mut self, span: Span, label: impl Into<DiagnosticMessage>) -> &mut Self);
|
pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||||
|
|
||||||
forward!(
|
forward!(
|
||||||
/// Labels all the given spans with the provided label.
|
/// Labels all the given spans with the provided label.
|
||||||
@ -430,25 +433,29 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
|||||||
found: DiagnosticStyledString,
|
found: DiagnosticStyledString,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
|
|
||||||
forward!(pub fn note(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
|
forward!(pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||||
forward!(pub fn note_once(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
|
forward!(pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||||
forward!(pub fn span_note(
|
forward!(pub fn span_note(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: impl Into<MultiSpan>,
|
sp: impl Into<MultiSpan>,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn span_note_once(
|
forward!(pub fn span_note_once(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: impl Into<MultiSpan>,
|
sp: impl Into<MultiSpan>,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn warn(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
|
forward!(pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||||
forward!(pub fn span_warn(&mut self, sp: impl Into<MultiSpan>, msg: &str) -> &mut Self);
|
forward!(pub fn span_warn(
|
||||||
forward!(pub fn help(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
|
&mut self,
|
||||||
|
sp: impl Into<MultiSpan>,
|
||||||
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
|
) -> &mut Self);
|
||||||
|
forward!(pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||||
forward!(pub fn span_help(
|
forward!(pub fn span_help(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: impl Into<MultiSpan>,
|
sp: impl Into<MultiSpan>,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self);
|
forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self);
|
||||||
forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
|
forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
|
||||||
@ -457,67 +464,67 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
|||||||
|
|
||||||
forward!(pub fn multipart_suggestion(
|
forward!(pub fn multipart_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn multipart_suggestion_verbose(
|
forward!(pub fn multipart_suggestion_verbose(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn tool_only_multipart_suggestion(
|
forward!(pub fn tool_only_multipart_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: Vec<(Span, String)>,
|
suggestion: Vec<(Span, String)>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn span_suggestion(
|
forward!(pub fn span_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn span_suggestions(
|
forward!(pub fn span_suggestions(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestions: impl Iterator<Item = String>,
|
suggestions: impl Iterator<Item = String>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn multipart_suggestions(
|
forward!(pub fn multipart_suggestions(
|
||||||
&mut self,
|
&mut self,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestions: impl Iterator<Item = Vec<(Span, String)>>,
|
suggestions: impl Iterator<Item = Vec<(Span, String)>>,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn span_suggestion_short(
|
forward!(pub fn span_suggestion_short(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn span_suggestion_verbose(
|
forward!(pub fn span_suggestion_verbose(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn span_suggestion_hidden(
|
forward!(pub fn span_suggestion_hidden(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
forward!(pub fn tool_only_span_suggestion(
|
forward!(pub fn tool_only_span_suggestion(
|
||||||
&mut self,
|
&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
msg: impl Into<DiagnosticMessage>,
|
msg: impl Into<SubdiagnosticMessage>,
|
||||||
suggestion: impl ToString,
|
suggestion: impl ToString,
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
) -> &mut Self);
|
) -> &mut Self);
|
||||||
|
@ -32,7 +32,8 @@ use rustc_data_structures::sync::{self, Lock, Lrc};
|
|||||||
use rustc_data_structures::AtomicRef;
|
use rustc_data_structures::AtomicRef;
|
||||||
pub use rustc_error_messages::{
|
pub use rustc_error_messages::{
|
||||||
fallback_fluent_bundle, fluent, fluent_bundle, DiagnosticMessage, FluentBundle,
|
fallback_fluent_bundle, fluent, fluent_bundle, DiagnosticMessage, FluentBundle,
|
||||||
LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel, DEFAULT_LOCALE_RESOURCES,
|
LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel, SubdiagnosticMessage,
|
||||||
|
DEFAULT_LOCALE_RESOURCES,
|
||||||
};
|
};
|
||||||
pub use rustc_lint_defs::{pluralize, Applicability};
|
pub use rustc_lint_defs::{pluralize, Applicability};
|
||||||
use rustc_span::source_map::SourceMap;
|
use rustc_span::source_map::SourceMap;
|
||||||
|
@ -126,14 +126,14 @@ impl<'a> SessionDiagnosticDerive<'a> {
|
|||||||
(Some((SessionDiagnosticKind::Error, _)), Some((slug, _))) => {
|
(Some((SessionDiagnosticKind::Error, _)), Some((slug, _))) => {
|
||||||
quote! {
|
quote! {
|
||||||
let mut #diag = #sess.struct_err(
|
let mut #diag = #sess.struct_err(
|
||||||
rustc_errors::DiagnosticMessage::fluent(#slug),
|
rustc_errors::DiagnosticMessage::new(#slug),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Some((SessionDiagnosticKind::Warn, _)), Some((slug, _))) => {
|
(Some((SessionDiagnosticKind::Warn, _)), Some((slug, _))) => {
|
||||||
quote! {
|
quote! {
|
||||||
let mut #diag = #sess.struct_warn(
|
let mut #diag = #sess.struct_warn(
|
||||||
rustc_errors::DiagnosticMessage::fluent(#slug),
|
rustc_errors::DiagnosticMessage::new(#slug),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -254,21 +254,6 @@ impl SessionDiagnosticDeriveBuilder {
|
|||||||
|
|
||||||
if matches!(name, "help" | "note") && matches!(meta, Meta::Path(_) | Meta::NameValue(_)) {
|
if matches!(name, "help" | "note") && matches!(meta, Meta::Path(_) | Meta::NameValue(_)) {
|
||||||
let diag = &self.diag;
|
let diag = &self.diag;
|
||||||
let slug = match &self.slug {
|
|
||||||
Some((slug, _)) => slug.as_str(),
|
|
||||||
None => throw_span_err!(
|
|
||||||
span,
|
|
||||||
&format!(
|
|
||||||
"`#[{}{}]` must come after `#[error(..)]` or `#[warn(..)]`",
|
|
||||||
name,
|
|
||||||
match meta {
|
|
||||||
Meta::Path(_) => "",
|
|
||||||
Meta::NameValue(_) => " = ...",
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
),
|
|
||||||
};
|
|
||||||
let id = match meta {
|
let id = match meta {
|
||||||
Meta::Path(..) => quote! { #name },
|
Meta::Path(..) => quote! { #name },
|
||||||
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
|
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
|
||||||
@ -279,7 +264,7 @@ impl SessionDiagnosticDeriveBuilder {
|
|||||||
let fn_name = proc_macro2::Ident::new(name, attr.span());
|
let fn_name = proc_macro2::Ident::new(name, attr.span());
|
||||||
|
|
||||||
return Ok(quote! {
|
return Ok(quote! {
|
||||||
#diag.#fn_name(rustc_errors::DiagnosticMessage::fluent_attr(#slug, #id));
|
#diag.#fn_name(rustc_errors::SubdiagnosticMessage::attr(#id));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -525,13 +510,8 @@ impl SessionDiagnosticDeriveBuilder {
|
|||||||
|
|
||||||
let method = format_ident!("span_{}", name);
|
let method = format_ident!("span_{}", name);
|
||||||
|
|
||||||
let slug = self
|
|
||||||
.slug
|
|
||||||
.as_ref()
|
|
||||||
.map(|(slug, _)| slug.as_str())
|
|
||||||
.unwrap_or_else(|| "missing-slug");
|
|
||||||
let msg = msg.as_deref().unwrap_or("suggestion");
|
let msg = msg.as_deref().unwrap_or("suggestion");
|
||||||
let msg = quote! { rustc_errors::DiagnosticMessage::fluent_attr(#slug, #msg) };
|
let msg = quote! { rustc_errors::SubdiagnosticMessage::attr(#msg) };
|
||||||
let code = code.unwrap_or_else(|| quote! { String::new() });
|
let code = code.unwrap_or_else(|| quote! { String::new() });
|
||||||
|
|
||||||
Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); })
|
Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); })
|
||||||
@ -549,14 +529,11 @@ impl SessionDiagnosticDeriveBuilder {
|
|||||||
fluent_attr_identifier: &str,
|
fluent_attr_identifier: &str,
|
||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
let diag = &self.diag;
|
let diag = &self.diag;
|
||||||
|
|
||||||
let slug =
|
|
||||||
self.slug.as_ref().map(|(slug, _)| slug.as_str()).unwrap_or_else(|| "missing-slug");
|
|
||||||
let fn_name = format_ident!("span_{}", kind);
|
let fn_name = format_ident!("span_{}", kind);
|
||||||
quote! {
|
quote! {
|
||||||
#diag.#fn_name(
|
#diag.#fn_name(
|
||||||
#field_binding,
|
#field_binding,
|
||||||
rustc_errors::DiagnosticMessage::fluent_attr(#slug, #fluent_attr_identifier)
|
rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -565,9 +542,8 @@ impl SessionDiagnosticDeriveBuilder {
|
|||||||
/// and `fluent_attr_identifier`.
|
/// and `fluent_attr_identifier`.
|
||||||
fn add_subdiagnostic(&self, kind: &Ident, fluent_attr_identifier: &str) -> TokenStream {
|
fn add_subdiagnostic(&self, kind: &Ident, fluent_attr_identifier: &str) -> TokenStream {
|
||||||
let diag = &self.diag;
|
let diag = &self.diag;
|
||||||
let slug = self.slug.as_ref().map(|(slug, _)| slug.as_str()).unwrap_or("missing-slug");
|
|
||||||
quote! {
|
quote! {
|
||||||
#diag.#kind(rustc_errors::DiagnosticMessage::fluent_attr(#slug, #fluent_attr_identifier));
|
#diag.#kind(rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ use proc_macro::{Diagnostic, Level, Span};
|
|||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::{HashMap, HashSet},
|
||||||
fs::File,
|
fs::File,
|
||||||
io::Read,
|
io::Read,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
@ -100,6 +100,10 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
|
|||||||
let ident_span = res.ident.span().unwrap();
|
let ident_span = res.ident.span().unwrap();
|
||||||
let path_span = res.resource.span().unwrap();
|
let path_span = res.resource.span().unwrap();
|
||||||
|
|
||||||
|
// Set of Fluent attribute names already output, to avoid duplicate type errors - any given
|
||||||
|
// constant created for a given attribute is the same.
|
||||||
|
let mut previous_attrs = HashSet::new();
|
||||||
|
|
||||||
let relative_ftl_path = res.resource.value();
|
let relative_ftl_path = res.resource.value();
|
||||||
let absolute_ftl_path =
|
let absolute_ftl_path =
|
||||||
invocation_relative_path_to_absolute(ident_span, &relative_ftl_path);
|
invocation_relative_path_to_absolute(ident_span, &relative_ftl_path);
|
||||||
@ -199,13 +203,15 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
|
|||||||
});
|
});
|
||||||
|
|
||||||
for Attribute { id: Identifier { name: attr_name }, .. } in attributes {
|
for Attribute { id: Identifier { name: attr_name }, .. } in attributes {
|
||||||
let attr_snake_name = attr_name.replace("-", "_");
|
let snake_name = Ident::new(&attr_name.replace("-", "_"), span);
|
||||||
let snake_name = Ident::new(&format!("{snake_name}_{attr_snake_name}"), span);
|
if !previous_attrs.insert(snake_name.clone()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
constants.extend(quote! {
|
constants.extend(quote! {
|
||||||
pub const #snake_name: crate::DiagnosticMessage =
|
pub const #snake_name: crate::SubdiagnosticMessage =
|
||||||
crate::DiagnosticMessage::FluentIdentifier(
|
crate::SubdiagnosticMessage::FluentAttr(
|
||||||
std::borrow::Cow::Borrowed(#name),
|
std::borrow::Cow::Borrowed(#attr_name)
|
||||||
Some(std::borrow::Cow::Borrowed(#attr_name))
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -397,7 +397,7 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
|
|||||||
|
|
||||||
let diag = &self.diag;
|
let diag = &self.diag;
|
||||||
let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
|
let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
|
||||||
let message = quote! { rustc_errors::DiagnosticMessage::fluent(#slug) };
|
let message = quote! { rustc_errors::SubdiagnosticMessage::message(#slug) };
|
||||||
let call = if matches!(kind, SubdiagnosticKind::Suggestion(..)) {
|
let call = if matches!(kind, SubdiagnosticKind::Suggestion(..)) {
|
||||||
if let Some(span) = span_field {
|
if let Some(span) = span_field {
|
||||||
quote! { #diag.#name(#span, #message, #code, #applicability); }
|
quote! { #diag.#name(#span, #message, #code, #applicability); }
|
||||||
|
@ -17,10 +17,10 @@ use rustc_ast::{
|
|||||||
};
|
};
|
||||||
use rustc_ast_pretty::pprust;
|
use rustc_ast_pretty::pprust;
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, ErrorGuaranteed};
|
|
||||||
use rustc_errors::{
|
use rustc_errors::{
|
||||||
Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult,
|
fluent, Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult,
|
||||||
};
|
};
|
||||||
|
use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, ErrorGuaranteed};
|
||||||
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
|
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
|
||||||
use rustc_span::source_map::Spanned;
|
use rustc_span::source_map::Spanned;
|
||||||
use rustc_span::symbol::{kw, Ident};
|
use rustc_span::symbol::{kw, Ident};
|
||||||
@ -658,13 +658,10 @@ impl<'a> Parser<'a> {
|
|||||||
err.delay_as_bug();
|
err.delay_as_bug();
|
||||||
self.struct_span_err(
|
self.struct_span_err(
|
||||||
expr.span,
|
expr.span,
|
||||||
DiagnosticMessage::fluent("parser-struct-literal-body-without-path"),
|
fluent::parser::struct_literal_body_without_path,
|
||||||
)
|
)
|
||||||
.multipart_suggestion(
|
.multipart_suggestion(
|
||||||
DiagnosticMessage::fluent_attr(
|
fluent::parser::suggestion,
|
||||||
"parser-struct-literal-body-without-path",
|
|
||||||
"suggestion",
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
(expr.span.shrink_to_lo(), "{ SomeStruct ".to_string()),
|
(expr.span.shrink_to_lo(), "{ SomeStruct ".to_string()),
|
||||||
(expr.span.shrink_to_hi(), " }".to_string()),
|
(expr.span.shrink_to_hi(), " }".to_string()),
|
||||||
|
@ -277,7 +277,7 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
|
|||||||
.join(", "),
|
.join(", "),
|
||||||
);
|
);
|
||||||
|
|
||||||
err.span_label(self.def_span, rustc_errors::fluent::typeck::missing_type_params_label);
|
err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
|
||||||
|
|
||||||
let mut suggested = false;
|
let mut suggested = false;
|
||||||
if let (Ok(snippet), true) = (
|
if let (Ok(snippet), true) = (
|
||||||
@ -295,7 +295,7 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
|
|||||||
// least we can clue them to the correct syntax `Iterator<Type>`.
|
// least we can clue them to the correct syntax `Iterator<Type>`.
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
self.span,
|
self.span,
|
||||||
rustc_errors::fluent::typeck::missing_type_params_suggestion,
|
rustc_errors::fluent::typeck::suggestion,
|
||||||
format!("{}<{}>", snippet, self.missing_type_params.join(", ")),
|
format!("{}<{}>", snippet, self.missing_type_params.join(", ")),
|
||||||
Applicability::HasPlaceholders,
|
Applicability::HasPlaceholders,
|
||||||
);
|
);
|
||||||
@ -303,13 +303,10 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !suggested {
|
if !suggested {
|
||||||
err.span_label(
|
err.span_label(self.span, rustc_errors::fluent::typeck::no_suggestion_label);
|
||||||
self.span,
|
|
||||||
rustc_errors::fluent::typeck::missing_type_params_no_suggestion_label,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err.note(rustc_errors::fluent::typeck::missing_type_params_note);
|
err.note(rustc_errors::fluent::typeck::note);
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,12 @@ pub enum DiagnosticMessage {
|
|||||||
FluentIdentifier(std::borrow::Cow<'static, str>, Option<std::borrow::Cow<'static, str>>),
|
FluentIdentifier(std::borrow::Cow<'static, str>, Option<std::borrow::Cow<'static, str>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Copy of the relevant `SubdiagnosticMessage` variant constructed by `fluent_messages` as it
|
||||||
|
/// expects `crate::SubdiagnosticMessage` to exist.
|
||||||
|
pub enum SubdiagnosticMessage {
|
||||||
|
FluentAttr(std::borrow::Cow<'static, str>),
|
||||||
|
}
|
||||||
|
|
||||||
mod missing_absolute {
|
mod missing_absolute {
|
||||||
use super::fluent_messages;
|
use super::fluent_messages;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
error: could not open Fluent resource
|
error: could not open Fluent resource
|
||||||
--> $DIR/test.rs:19:29
|
--> $DIR/test.rs:25:29
|
||||||
|
|
|
|
||||||
LL | missing_absolute => "/definitely_does_not_exist.ftl",
|
LL | missing_absolute => "/definitely_does_not_exist.ftl",
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -7,7 +7,7 @@ LL | missing_absolute => "/definitely_does_not_exist.ftl",
|
|||||||
= note: os-specific message
|
= note: os-specific message
|
||||||
|
|
||||||
error: could not open Fluent resource
|
error: could not open Fluent resource
|
||||||
--> $DIR/test.rs:28:29
|
--> $DIR/test.rs:34:29
|
||||||
|
|
|
|
||||||
LL | missing_relative => "../definitely_does_not_exist.ftl",
|
LL | missing_relative => "../definitely_does_not_exist.ftl",
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -15,7 +15,7 @@ LL | missing_relative => "../definitely_does_not_exist.ftl",
|
|||||||
= note: os-specific message
|
= note: os-specific message
|
||||||
|
|
||||||
error: could not parse Fluent resource
|
error: could not parse Fluent resource
|
||||||
--> $DIR/test.rs:37:28
|
--> $DIR/test.rs:43:28
|
||||||
|
|
|
|
||||||
LL | missing_message => "./missing-message.ftl",
|
LL | missing_message => "./missing-message.ftl",
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -30,13 +30,13 @@ error: expected a message field for "missing-message"
|
|||||||
|
|
|
|
||||||
|
|
||||||
error: overrides existing message: `key`
|
error: overrides existing message: `key`
|
||||||
--> $DIR/test.rs:47:9
|
--> $DIR/test.rs:53:9
|
||||||
|
|
|
|
||||||
LL | b => "./duplicate-b.ftl",
|
LL | b => "./duplicate-b.ftl",
|
||||||
| ^
|
| ^
|
||||||
|
|
|
|
||||||
help: previously defined in this resource
|
help: previously defined in this resource
|
||||||
--> $DIR/test.rs:46:9
|
--> $DIR/test.rs:52:9
|
||||||
|
|
|
|
||||||
LL | a => "./duplicate-a.ftl",
|
LL | a => "./duplicate-a.ftl",
|
||||||
| ^
|
| ^
|
||||||
|
@ -404,7 +404,6 @@ struct ErrorWithHelpCustom {
|
|||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
#[help]
|
#[help]
|
||||||
//~^ ERROR `#[help]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
#[error(code = "E0123", slug = "foo")]
|
#[error(code = "E0123", slug = "foo")]
|
||||||
struct ErrorWithHelpWrongOrder {
|
struct ErrorWithHelpWrongOrder {
|
||||||
val: String,
|
val: String,
|
||||||
@ -412,7 +411,6 @@ struct ErrorWithHelpWrongOrder {
|
|||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
#[help = "bar"]
|
#[help = "bar"]
|
||||||
//~^ ERROR `#[help = ...]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
#[error(code = "E0123", slug = "foo")]
|
#[error(code = "E0123", slug = "foo")]
|
||||||
struct ErrorWithHelpCustomWrongOrder {
|
struct ErrorWithHelpCustomWrongOrder {
|
||||||
val: String,
|
val: String,
|
||||||
@ -420,7 +418,6 @@ struct ErrorWithHelpCustomWrongOrder {
|
|||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
#[note]
|
#[note]
|
||||||
//~^ ERROR `#[note]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
#[error(code = "E0123", slug = "foo")]
|
#[error(code = "E0123", slug = "foo")]
|
||||||
struct ErrorWithNoteWrongOrder {
|
struct ErrorWithNoteWrongOrder {
|
||||||
val: String,
|
val: String,
|
||||||
@ -428,7 +425,6 @@ struct ErrorWithNoteWrongOrder {
|
|||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
#[note = "bar"]
|
#[note = "bar"]
|
||||||
//~^ ERROR `#[note = ...]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
#[error(code = "E0123", slug = "foo")]
|
#[error(code = "E0123", slug = "foo")]
|
||||||
struct ErrorWithNoteCustomWrongOrder {
|
struct ErrorWithNoteCustomWrongOrder {
|
||||||
val: String,
|
val: String,
|
||||||
|
@ -301,38 +301,14 @@ LL | #[label("bar")]
|
|||||||
|
|
|
|
||||||
= help: only `suggestion{,_short,_hidden,_verbose}` are valid field attributes
|
= help: only `suggestion{,_short,_hidden,_verbose}` are valid field attributes
|
||||||
|
|
||||||
error: `#[help]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
--> $DIR/diagnostic-derive.rs:406:1
|
|
||||||
|
|
|
||||||
LL | #[help]
|
|
||||||
| ^^^^^^^
|
|
||||||
|
|
||||||
error: `#[help = ...]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
--> $DIR/diagnostic-derive.rs:414:1
|
|
||||||
|
|
|
||||||
LL | #[help = "bar"]
|
|
||||||
| ^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: `#[note]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
--> $DIR/diagnostic-derive.rs:422:1
|
|
||||||
|
|
|
||||||
LL | #[note]
|
|
||||||
| ^^^^^^^
|
|
||||||
|
|
||||||
error: `#[note = ...]` must come after `#[error(..)]` or `#[warn(..)]`
|
|
||||||
--> $DIR/diagnostic-derive.rs:430:1
|
|
||||||
|
|
|
||||||
LL | #[note = "bar"]
|
|
||||||
| ^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: applicability cannot be set in both the field and attribute
|
error: applicability cannot be set in both the field and attribute
|
||||||
--> $DIR/diagnostic-derive.rs:440:49
|
--> $DIR/diagnostic-derive.rs:436:49
|
||||||
|
|
|
|
||||||
LL | #[suggestion(message = "bar", code = "...", applicability = "maybe-incorrect")]
|
LL | #[suggestion(message = "bar", code = "...", applicability = "maybe-incorrect")]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: invalid applicability
|
error: invalid applicability
|
||||||
--> $DIR/diagnostic-derive.rs:448:49
|
--> $DIR/diagnostic-derive.rs:444:49
|
||||||
|
|
|
|
||||||
LL | #[suggestion(message = "bar", code = "...", applicability = "batman")]
|
LL | #[suggestion(message = "bar", code = "...", applicability = "batman")]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -363,12 +339,12 @@ LL | #[derive(SessionDiagnostic)]
|
|||||||
rustc_middle::ty::Ty<'tcx>
|
rustc_middle::ty::Ty<'tcx>
|
||||||
usize
|
usize
|
||||||
note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
|
note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
|
||||||
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:531:19
|
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:538:19
|
||||||
|
|
|
|
||||||
LL | arg: impl IntoDiagnosticArg,
|
LL | arg: impl IntoDiagnosticArg,
|
||||||
| ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg`
|
| ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg`
|
||||||
= note: this error originates in the derive macro `SessionDiagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the derive macro `SessionDiagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: aborting due to 43 previous errors
|
error: aborting due to 39 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0277`.
|
For more information about this error, try `rustc --explain E0277`.
|
||||||
|
Loading…
Reference in New Issue
Block a user