1 use crate::snippet::Style;
3 CodeSuggestion, DiagnosticMessage, Level, MultiSpan, SubdiagnosticMessage, Substitution,
4 SubstitutionPart, SuggestionStyle,
6 use rustc_data_structures::stable_map::FxHashMap;
7 use rustc_error_messages::FluentValue;
8 use rustc_lint_defs::{Applicability, LintExpectationId};
9 use rustc_span::edition::LATEST_STABLE_EDITION;
10 use rustc_span::symbol::{Ident, Symbol};
11 use rustc_span::{Span, DUMMY_SP};
14 use std::hash::{Hash, Hasher};
16 /// Error type for `Diagnostic`'s `suggestions` field, indicating that
17 /// `.disable_suggestions()` was called on the `Diagnostic`.
18 #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
19 pub struct SuggestionsDisabled;
21 /// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of
22 /// `DiagnosticArg` are converted to `FluentArgs` (consuming the collection) at the start of
23 /// diagnostic emission.
24 pub type DiagnosticArg<'source> = (Cow<'source, str>, DiagnosticArgValue<'source>);
26 /// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted
27 /// to a `FluentValue` by the emitter to be used in diagnostic translation.
28 #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
29 pub enum DiagnosticArgValue<'source> {
30 Str(Cow<'source, str>),
34 /// Converts a value of a type into a `DiagnosticArg` (typically a field of a `SessionDiagnostic`
35 /// struct). Implemented as a custom trait rather than `From` so that it is implemented on the type
36 /// being converted rather than on `DiagnosticArgValue`, which enables types from other `rustc_*`
37 /// crates to implement this.
38 pub trait IntoDiagnosticArg {
39 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
42 impl IntoDiagnosticArg for String {
43 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
44 DiagnosticArgValue::Str(Cow::Owned(self))
48 impl IntoDiagnosticArg for Symbol {
49 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
50 self.to_ident_string().into_diagnostic_arg()
54 impl IntoDiagnosticArg for Ident {
55 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
56 self.to_string().into_diagnostic_arg()
60 impl<'a> IntoDiagnosticArg for &'a str {
61 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
62 self.to_string().into_diagnostic_arg()
66 impl IntoDiagnosticArg for usize {
67 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
68 DiagnosticArgValue::Number(self)
72 impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
73 fn into(self) -> FluentValue<'source> {
75 DiagnosticArgValue::Str(s) => From::from(s),
76 DiagnosticArgValue::Number(n) => From::from(n),
81 /// Trait implemented by error types. This should not be implemented manually. Instead, use
82 /// `#[derive(SessionSubdiagnostic)]` -- see [rustc_macros::SessionSubdiagnostic].
83 #[rustc_diagnostic_item = "AddSubdiagnostic"]
84 pub trait AddSubdiagnostic {
85 /// Add a subdiagnostic to an existing diagnostic.
86 fn add_to_diagnostic(self, diag: &mut Diagnostic);
90 #[derive(Clone, Debug, Encodable, Decodable)]
91 pub struct Diagnostic {
92 // NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
93 // outside of what methods in this crate themselves allow.
94 pub(crate) level: Level,
96 pub message: Vec<(DiagnosticMessage, Style)>,
97 pub code: Option<DiagnosticId>,
99 pub children: Vec<SubDiagnostic>,
100 pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
101 args: Vec<DiagnosticArg<'static>>,
103 /// This is not used for highlighting or rendering any error message. Rather, it can be used
104 /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
105 /// `span` if there is one. Otherwise, it is `DUMMY_SP`.
108 /// If diagnostic is from Lint, custom hash function ignores notes
109 /// otherwise hash is based on the all the fields
113 #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
114 pub enum DiagnosticId {
116 Lint { name: String, has_future_breakage: bool, is_force_warn: bool },
119 /// A "sub"-diagnostic attached to a parent diagnostic.
120 /// For example, a note attached to an error.
121 #[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
122 pub struct SubDiagnostic {
124 pub message: Vec<(DiagnosticMessage, Style)>,
126 pub render_span: Option<MultiSpan>,
129 #[derive(Debug, PartialEq, Eq)]
130 pub struct DiagnosticStyledString(pub Vec<StringPart>);
132 impl DiagnosticStyledString {
133 pub fn new() -> DiagnosticStyledString {
134 DiagnosticStyledString(vec![])
136 pub fn push_normal<S: Into<String>>(&mut self, t: S) {
137 self.0.push(StringPart::Normal(t.into()));
139 pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
140 self.0.push(StringPart::Highlighted(t.into()));
142 pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
144 self.push_highlighted(t);
149 pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
150 DiagnosticStyledString(vec![StringPart::Normal(t.into())])
153 pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
154 DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
157 pub fn content(&self) -> String {
158 self.0.iter().map(|x| x.content()).collect::<String>()
162 #[derive(Debug, PartialEq, Eq)]
163 pub enum StringPart {
169 pub fn content(&self) -> &str {
171 &StringPart::Normal(ref s) | &StringPart::Highlighted(ref s) => s,
177 pub fn new<M: Into<DiagnosticMessage>>(level: Level, message: M) -> Self {
178 Diagnostic::new_with_code(level, None, message)
181 pub fn new_with_code<M: Into<DiagnosticMessage>>(
183 code: Option<DiagnosticId>,
188 message: vec![(message.into(), Style::NoStyle)],
190 span: MultiSpan::new(),
192 suggestions: Ok(vec![]),
200 pub fn level(&self) -> Level {
204 pub fn is_error(&self) -> bool {
209 | Level::Error { .. }
210 | Level::FailureNote => true,
217 | Level::Expect(_) => false,
221 pub fn update_unstable_expectation_id(
223 unstable_to_stable: &FxHashMap<LintExpectationId, LintExpectationId>,
225 if let Level::Expect(expectation_id) = &mut self.level {
226 if expectation_id.is_stable() {
230 // The unstable to stable map only maps the unstable `AttrId` to a stable `HirId` with an attribute index.
231 // The lint index inside the attribute is manually transferred here.
232 let lint_index = expectation_id.get_lint_index();
233 expectation_id.set_lint_index(None);
234 let mut stable_id = *unstable_to_stable
235 .get(&expectation_id)
236 .expect("each unstable `LintExpectationId` must have a matching stable id");
238 stable_id.set_lint_index(lint_index);
239 *expectation_id = stable_id;
243 pub fn has_future_breakage(&self) -> bool {
245 Some(DiagnosticId::Lint { has_future_breakage, .. }) => has_future_breakage,
250 pub fn is_force_warn(&self) -> bool {
252 Some(DiagnosticId::Lint { is_force_warn, .. }) => is_force_warn,
257 /// Delay emission of this diagnostic as a bug.
259 /// This can be useful in contexts where an error indicates a bug but
260 /// typically this only happens when other compilation errors have already
261 /// happened. In those cases this can be used to defer emission of this
262 /// diagnostic as a bug in the compiler only if no other errors have been
265 /// In the meantime, though, callsites are required to deal with the "bug"
266 /// locally in whichever way makes the most sense.
268 pub fn downgrade_to_delayed_bug(&mut self) -> &mut Self {
271 "downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
274 self.level = Level::DelayedBug;
279 /// Adds a span/label to be included in the resulting snippet.
281 /// This is pushed onto the [`MultiSpan`] that was created when the diagnostic
282 /// was first built. That means it will be shown together with the original
283 /// span/label, *not* a span added by one of the `span_{note,warn,help,suggestions}` methods.
285 /// This span is *not* considered a ["primary span"][`MultiSpan`]; only
286 /// the `Span` supplied when creating the diagnostic is primary.
287 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
288 pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self {
289 self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label));
293 /// Labels all the given spans with the provided label.
294 /// See [`Self::span_label()`] for more information.
297 spans: impl IntoIterator<Item = Span>,
298 label: impl AsRef<str>,
300 let label = label.as_ref();
302 self.span_label(span, label);
307 pub fn replace_span_with(&mut self, after: Span) -> &mut Self {
308 let before = self.span.clone();
309 self.set_span(after);
310 for span_label in before.span_labels() {
311 if let Some(label) = span_label.label {
312 self.span.push_span_label(after, label);
318 pub fn note_expected_found(
320 expected_label: &dyn fmt::Display,
321 expected: DiagnosticStyledString,
322 found_label: &dyn fmt::Display,
323 found: DiagnosticStyledString,
325 self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"")
328 pub fn note_unsuccessful_coercion(
330 expected: DiagnosticStyledString,
331 found: DiagnosticStyledString,
333 let mut msg: Vec<_> =
334 vec![("required when trying to coerce from type `".to_string(), Style::NoStyle)];
335 msg.extend(expected.0.iter().map(|x| match *x {
336 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
337 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
339 msg.push(("` to type '".to_string(), Style::NoStyle));
340 msg.extend(found.0.iter().map(|x| match *x {
341 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
342 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
344 msg.push(("`".to_string(), Style::NoStyle));
346 // For now, just attach these as notes
347 self.highlighted_note(msg);
351 pub fn note_expected_found_extra(
353 expected_label: &dyn fmt::Display,
354 expected: DiagnosticStyledString,
355 found_label: &dyn fmt::Display,
356 found: DiagnosticStyledString,
357 expected_extra: &dyn fmt::Display,
358 found_extra: &dyn fmt::Display,
360 let expected_label = expected_label.to_string();
361 let expected_label = if expected_label.is_empty() {
362 "expected".to_string()
364 format!("expected {}", expected_label)
366 let found_label = found_label.to_string();
367 let found_label = if found_label.is_empty() {
370 format!("found {}", found_label)
372 let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
373 (expected_label.len() - found_label.len(), 0)
375 (0, found_label.len() - expected_label.len())
377 let mut msg: Vec<_> =
378 vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
379 msg.extend(expected.0.iter().map(|x| match *x {
380 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
381 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
383 msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
384 msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
385 msg.extend(found.0.iter().map(|x| match *x {
386 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
387 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
389 msg.push((format!("`{}", found_extra), Style::NoStyle));
391 // For now, just attach these as notes.
392 self.highlighted_note(msg);
396 pub fn note_trait_signature(&mut self, name: String, signature: String) -> &mut Self {
397 self.highlighted_note(vec![
398 (format!("`{}` from trait: `", name), Style::NoStyle),
399 (signature, Style::Highlight),
400 ("`".to_string(), Style::NoStyle),
405 /// Add a note attached to this diagnostic.
406 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
407 pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
408 self.sub(Level::Note, msg, MultiSpan::new(), None);
412 pub fn highlighted_note<M: Into<SubdiagnosticMessage>>(
414 msg: Vec<(M, Style)>,
416 self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
420 /// Prints the span with a note above it.
421 /// This is like [`Diagnostic::note()`], but it gets its own span.
422 pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
423 self.sub(Level::OnceNote, msg, MultiSpan::new(), None);
427 /// Prints the span with a note above it.
428 /// This is like [`Diagnostic::note()`], but it gets its own span.
429 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
430 pub fn span_note<S: Into<MultiSpan>>(
433 msg: impl Into<SubdiagnosticMessage>,
435 self.sub(Level::Note, msg, sp.into(), None);
439 /// Prints the span with a note above it.
440 /// This is like [`Diagnostic::note()`], but it gets its own span.
441 pub fn span_note_once<S: Into<MultiSpan>>(
444 msg: impl Into<SubdiagnosticMessage>,
446 self.sub(Level::OnceNote, msg, sp.into(), None);
450 /// Add a warning attached to this diagnostic.
451 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
452 pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
453 self.sub(Level::Warning, msg, MultiSpan::new(), None);
457 /// Prints the span with a warning above it.
458 /// This is like [`Diagnostic::warn()`], but it gets its own span.
459 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
460 pub fn span_warn<S: Into<MultiSpan>>(
463 msg: impl Into<SubdiagnosticMessage>,
465 self.sub(Level::Warning, msg, sp.into(), None);
469 /// Add a help message attached to this diagnostic.
470 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
471 pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
472 self.sub(Level::Help, msg, MultiSpan::new(), None);
476 /// Add a help message attached to this diagnostic with a customizable highlighted message.
477 pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
478 self.sub_with_highlights(Level::Help, msg, MultiSpan::new(), None);
482 /// Prints the span with some help above it.
483 /// This is like [`Diagnostic::help()`], but it gets its own span.
484 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
485 pub fn span_help<S: Into<MultiSpan>>(
488 msg: impl Into<SubdiagnosticMessage>,
490 self.sub(Level::Help, msg, sp.into(), None);
494 /// Help the user upgrade to the latest edition.
495 /// This is factored out to make sure it does the right thing with `Cargo.toml`.
496 pub fn help_use_latest_edition(&mut self) -> &mut Self {
497 if std::env::var_os("CARGO").is_some() {
498 self.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
500 self.help(&format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION));
502 self.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
506 /// Disallow attaching suggestions this diagnostic.
507 /// Any suggestions attached e.g. with the `span_suggestion_*` methods
508 /// (before and after the call to `disable_suggestions`) will be ignored.
509 pub fn disable_suggestions(&mut self) -> &mut Self {
510 self.suggestions = Err(SuggestionsDisabled);
514 /// Helper for pushing to `self.suggestions`, if available (not disable).
515 fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
516 if let Ok(suggestions) = &mut self.suggestions {
517 suggestions.push(suggestion);
521 /// Show a suggestion that has multiple parts to it.
522 /// In other words, multiple changes need to be applied as part of this suggestion.
523 pub fn multipart_suggestion(
525 msg: impl Into<SubdiagnosticMessage>,
526 suggestion: Vec<(Span, String)>,
527 applicability: Applicability,
529 self.multipart_suggestion_with_style(
533 SuggestionStyle::ShowCode,
537 /// Show a suggestion that has multiple parts to it, always as it's own subdiagnostic.
538 /// In other words, multiple changes need to be applied as part of this suggestion.
539 pub fn multipart_suggestion_verbose(
541 msg: impl Into<SubdiagnosticMessage>,
542 suggestion: Vec<(Span, String)>,
543 applicability: Applicability,
545 self.multipart_suggestion_with_style(
549 SuggestionStyle::ShowAlways,
552 /// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
553 pub fn multipart_suggestion_with_style(
555 msg: impl Into<SubdiagnosticMessage>,
556 suggestion: Vec<(Span, String)>,
557 applicability: Applicability,
558 style: SuggestionStyle,
560 assert!(!suggestion.is_empty());
561 self.push_suggestion(CodeSuggestion {
562 substitutions: vec![Substitution {
565 .map(|(span, snippet)| SubstitutionPart { snippet, span })
568 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
575 /// Prints out a message with for a multipart suggestion without showing the suggested code.
577 /// This is intended to be used for suggestions that are obvious in what the changes need to
578 /// be from the message, showing the span label inline would be visually unpleasant
579 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
580 /// improve understandability.
581 pub fn tool_only_multipart_suggestion(
583 msg: impl Into<SubdiagnosticMessage>,
584 suggestion: Vec<(Span, String)>,
585 applicability: Applicability,
587 assert!(!suggestion.is_empty());
588 self.push_suggestion(CodeSuggestion {
589 substitutions: vec![Substitution {
592 .map(|(span, snippet)| SubstitutionPart { snippet, span })
595 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
596 style: SuggestionStyle::CompletelyHidden,
602 /// Prints out a message with a suggested edit of the code.
604 /// In case of short messages and a simple suggestion, rustc displays it as a label:
607 /// try adding parentheses: `(tup.0).1`
612 /// * should not end in any punctuation (a `:` is added automatically)
613 /// * should not be a question (avoid language like "did you mean")
614 /// * should not contain any phrases like "the following", "as shown", etc.
615 /// * may look like "to do xyz, use" or "to do xyz, use abc"
616 /// * may contain a name of a function, variable, or type, but not whole expressions
618 /// See `CodeSuggestion` for more information.
619 pub fn span_suggestion(
622 msg: impl Into<SubdiagnosticMessage>,
623 suggestion: impl ToString,
624 applicability: Applicability,
626 self.span_suggestion_with_style(
631 SuggestionStyle::ShowCode,
636 /// [`Diagnostic::span_suggestion()`] but you can set the [`SuggestionStyle`].
637 pub fn span_suggestion_with_style(
640 msg: impl Into<SubdiagnosticMessage>,
641 suggestion: impl ToString,
642 applicability: Applicability,
643 style: SuggestionStyle,
645 self.push_suggestion(CodeSuggestion {
646 substitutions: vec![Substitution {
647 parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
649 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
656 /// Always show the suggested change.
657 pub fn span_suggestion_verbose(
660 msg: impl Into<SubdiagnosticMessage>,
661 suggestion: impl ToString,
662 applicability: Applicability,
664 self.span_suggestion_with_style(
669 SuggestionStyle::ShowAlways,
674 /// Prints out a message with multiple suggested edits of the code.
675 /// See also [`Diagnostic::span_suggestion()`].
676 pub fn span_suggestions(
679 msg: impl Into<SubdiagnosticMessage>,
680 suggestions: impl Iterator<Item = String>,
681 applicability: Applicability,
683 let mut suggestions: Vec<_> = suggestions.collect();
685 let substitutions = suggestions
687 .map(|snippet| Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] })
689 self.push_suggestion(CodeSuggestion {
691 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
692 style: SuggestionStyle::ShowCode,
698 /// Prints out a message with multiple suggested edits of the code.
699 /// See also [`Diagnostic::span_suggestion()`].
700 pub fn multipart_suggestions(
702 msg: impl Into<SubdiagnosticMessage>,
703 suggestions: impl Iterator<Item = Vec<(Span, String)>>,
704 applicability: Applicability,
706 self.push_suggestion(CodeSuggestion {
707 substitutions: suggestions
708 .map(|sugg| Substitution {
711 .map(|(span, snippet)| SubstitutionPart { snippet, span })
715 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
716 style: SuggestionStyle::ShowCode,
721 /// Prints out a message with a suggested edit of the code. If the suggestion is presented
722 /// inline, it will only show the message and not the suggestion.
724 /// See `CodeSuggestion` for more information.
725 pub fn span_suggestion_short(
728 msg: impl Into<SubdiagnosticMessage>,
729 suggestion: impl ToString,
730 applicability: Applicability,
732 self.span_suggestion_with_style(
737 SuggestionStyle::HideCodeInline,
742 /// Prints out a message for a suggestion without showing the suggested code.
744 /// This is intended to be used for suggestions that are obvious in what the changes need to
745 /// be from the message, showing the span label inline would be visually unpleasant
746 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
747 /// improve understandability.
748 pub fn span_suggestion_hidden(
751 msg: impl Into<SubdiagnosticMessage>,
752 suggestion: impl ToString,
753 applicability: Applicability,
755 self.span_suggestion_with_style(
760 SuggestionStyle::HideCodeAlways,
765 /// Adds a suggestion to the JSON output that will not be shown in the CLI.
767 /// This is intended to be used for suggestions that are *very* obvious in what the changes
768 /// need to be from the message, but we still want other tools to be able to apply them.
769 pub fn tool_only_span_suggestion(
772 msg: impl Into<SubdiagnosticMessage>,
773 suggestion: impl ToString,
774 applicability: Applicability,
776 self.span_suggestion_with_style(
781 SuggestionStyle::CompletelyHidden,
786 /// Add a subdiagnostic from a type that implements `SessionSubdiagnostic` - see
787 /// [rustc_macros::SessionSubdiagnostic].
788 pub fn subdiagnostic(&mut self, subdiagnostic: impl AddSubdiagnostic) -> &mut Self {
789 subdiagnostic.add_to_diagnostic(self);
793 pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
794 self.span = sp.into();
795 if let Some(span) = self.span.primary_span() {
796 self.sort_span = span;
801 pub fn set_is_lint(&mut self) -> &mut Self {
806 pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
811 pub fn clear_code(&mut self) -> &mut Self {
816 pub fn get_code(&self) -> Option<DiagnosticId> {
820 pub fn set_primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
821 self.message[0] = (msg.into(), Style::NoStyle);
825 pub fn args(&self) -> &[DiagnosticArg<'static>] {
831 name: impl Into<Cow<'static, str>>,
832 arg: impl IntoDiagnosticArg,
834 self.args.push((name.into(), arg.into_diagnostic_arg()));
838 pub fn styled_message(&self) -> &[(DiagnosticMessage, Style)] {
842 /// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
843 /// combining it with the primary message of the diagnostic (if translatable, otherwise it just
844 /// passes the user's string along).
845 fn subdiagnostic_message_to_diagnostic_message(
847 attr: impl Into<SubdiagnosticMessage>,
848 ) -> DiagnosticMessage {
850 self.message.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
851 msg.with_subdiagnostic_message(attr.into())
854 /// Convenience function for internal use, clients should use one of the
855 /// public methods above.
857 /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
861 message: impl Into<SubdiagnosticMessage>,
863 render_span: Option<MultiSpan>,
865 let sub = SubDiagnostic {
868 self.subdiagnostic_message_to_diagnostic_message(message),
874 self.children.push(sub);
877 /// Convenience function for internal use, clients should use one of the
878 /// public methods above.
879 fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
882 mut message: Vec<(M, Style)>,
884 render_span: Option<MultiSpan>,
886 let message = message
888 .map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1))
890 let sub = SubDiagnostic { level, message, span, render_span };
891 self.children.push(sub);
894 /// Fields used for Hash, and PartialEq trait
899 &[(DiagnosticMessage, Style)],
900 &Option<DiagnosticId>,
902 &Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
903 Option<&[SubDiagnostic]>,
911 (if self.is_lint { None } else { Some(&self.children) }),
916 impl Hash for Diagnostic {
917 fn hash<H>(&self, state: &mut H)
921 self.keys().hash(state);
925 impl PartialEq for Diagnostic {
926 fn eq(&self, other: &Self) -> bool {
927 self.keys() == other.keys()