]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_errors/src/diagnostic.rs
Add back MAX_SUGGESTION_HIGHLIGHT_LINES so clippy is happy & buildable
[rust.git] / compiler / rustc_errors / src / diagnostic.rs
1 use crate::snippet::Style;
2 use crate::{
3     CodeSuggestion, DiagnosticMessage, Level, MultiSpan, SubdiagnosticMessage, Substitution,
4     SubstitutionPart, SuggestionStyle,
5 };
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};
12 use std::borrow::Cow;
13 use std::fmt;
14 use std::hash::{Hash, Hasher};
15
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;
20
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>);
25
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>),
31     Number(usize),
32 }
33
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>;
40 }
41
42 impl IntoDiagnosticArg for String {
43     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
44         DiagnosticArgValue::Str(Cow::Owned(self))
45     }
46 }
47
48 impl IntoDiagnosticArg for Symbol {
49     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
50         self.to_ident_string().into_diagnostic_arg()
51     }
52 }
53
54 impl IntoDiagnosticArg for Ident {
55     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
56         self.to_string().into_diagnostic_arg()
57     }
58 }
59
60 impl<'a> IntoDiagnosticArg for &'a str {
61     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
62         self.to_string().into_diagnostic_arg()
63     }
64 }
65
66 impl IntoDiagnosticArg for usize {
67     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
68         DiagnosticArgValue::Number(self)
69     }
70 }
71
72 impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
73     fn into(self) -> FluentValue<'source> {
74         match self {
75             DiagnosticArgValue::Str(s) => From::from(s),
76             DiagnosticArgValue::Number(n) => From::from(n),
77         }
78     }
79 }
80
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);
87 }
88
89 #[must_use]
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,
95
96     pub message: Vec<(DiagnosticMessage, Style)>,
97     pub code: Option<DiagnosticId>,
98     pub span: MultiSpan,
99     pub children: Vec<SubDiagnostic>,
100     pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
101     args: Vec<DiagnosticArg<'static>>,
102
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`.
106     pub sort_span: Span,
107
108     /// If diagnostic is from Lint, custom hash function ignores notes
109     /// otherwise hash is based on the all the fields
110     pub is_lint: bool,
111 }
112
113 #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
114 pub enum DiagnosticId {
115     Error(String),
116     Lint { name: String, has_future_breakage: bool, is_force_warn: bool },
117 }
118
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 {
123     pub level: Level,
124     pub message: Vec<(DiagnosticMessage, Style)>,
125     pub span: MultiSpan,
126     pub render_span: Option<MultiSpan>,
127 }
128
129 #[derive(Debug, PartialEq, Eq)]
130 pub struct DiagnosticStyledString(pub Vec<StringPart>);
131
132 impl DiagnosticStyledString {
133     pub fn new() -> DiagnosticStyledString {
134         DiagnosticStyledString(vec![])
135     }
136     pub fn push_normal<S: Into<String>>(&mut self, t: S) {
137         self.0.push(StringPart::Normal(t.into()));
138     }
139     pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
140         self.0.push(StringPart::Highlighted(t.into()));
141     }
142     pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
143         if highlight {
144             self.push_highlighted(t);
145         } else {
146             self.push_normal(t);
147         }
148     }
149     pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
150         DiagnosticStyledString(vec![StringPart::Normal(t.into())])
151     }
152
153     pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
154         DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
155     }
156
157     pub fn content(&self) -> String {
158         self.0.iter().map(|x| x.content()).collect::<String>()
159     }
160 }
161
162 #[derive(Debug, PartialEq, Eq)]
163 pub enum StringPart {
164     Normal(String),
165     Highlighted(String),
166 }
167
168 impl StringPart {
169     pub fn content(&self) -> &str {
170         match self {
171             &StringPart::Normal(ref s) | &StringPart::Highlighted(ref s) => s,
172         }
173     }
174 }
175
176 impl Diagnostic {
177     pub fn new<M: Into<DiagnosticMessage>>(level: Level, message: M) -> Self {
178         Diagnostic::new_with_code(level, None, message)
179     }
180
181     pub fn new_with_code<M: Into<DiagnosticMessage>>(
182         level: Level,
183         code: Option<DiagnosticId>,
184         message: M,
185     ) -> Self {
186         Diagnostic {
187             level,
188             message: vec![(message.into(), Style::NoStyle)],
189             code,
190             span: MultiSpan::new(),
191             children: vec![],
192             suggestions: Ok(vec![]),
193             args: vec![],
194             sort_span: DUMMY_SP,
195             is_lint: false,
196         }
197     }
198
199     #[inline(always)]
200     pub fn level(&self) -> Level {
201         self.level
202     }
203
204     pub fn is_error(&self) -> bool {
205         match self.level {
206             Level::Bug
207             | Level::DelayedBug
208             | Level::Fatal
209             | Level::Error { .. }
210             | Level::FailureNote => true,
211
212             Level::Warning
213             | Level::Note
214             | Level::OnceNote
215             | Level::Help
216             | Level::Allow
217             | Level::Expect(_) => false,
218         }
219     }
220
221     pub fn update_unstable_expectation_id(
222         &mut self,
223         unstable_to_stable: &FxHashMap<LintExpectationId, LintExpectationId>,
224     ) {
225         if let Level::Expect(expectation_id) = &mut self.level {
226             if expectation_id.is_stable() {
227                 return;
228             }
229
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");
237
238             stable_id.set_lint_index(lint_index);
239             *expectation_id = stable_id;
240         }
241     }
242
243     pub fn has_future_breakage(&self) -> bool {
244         match self.code {
245             Some(DiagnosticId::Lint { has_future_breakage, .. }) => has_future_breakage,
246             _ => false,
247         }
248     }
249
250     pub fn is_force_warn(&self) -> bool {
251         match self.code {
252             Some(DiagnosticId::Lint { is_force_warn, .. }) => is_force_warn,
253             _ => false,
254         }
255     }
256
257     /// Delay emission of this diagnostic as a bug.
258     ///
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
263     /// emitted.
264     ///
265     /// In the meantime, though, callsites are required to deal with the "bug"
266     /// locally in whichever way makes the most sense.
267     #[track_caller]
268     pub fn downgrade_to_delayed_bug(&mut self) -> &mut Self {
269         assert!(
270             self.is_error(),
271             "downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
272             self.level
273         );
274         self.level = Level::DelayedBug;
275
276         self
277     }
278
279     /// Adds a span/label to be included in the resulting snippet.
280     ///
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.
284     ///
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));
290         self
291     }
292
293     /// Labels all the given spans with the provided label.
294     /// See [`Self::span_label()`] for more information.
295     pub fn span_labels(
296         &mut self,
297         spans: impl IntoIterator<Item = Span>,
298         label: impl AsRef<str>,
299     ) -> &mut Self {
300         let label = label.as_ref();
301         for span in spans {
302             self.span_label(span, label);
303         }
304         self
305     }
306
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);
313             }
314         }
315         self
316     }
317
318     pub fn note_expected_found(
319         &mut self,
320         expected_label: &dyn fmt::Display,
321         expected: DiagnosticStyledString,
322         found_label: &dyn fmt::Display,
323         found: DiagnosticStyledString,
324     ) -> &mut Self {
325         self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"")
326     }
327
328     pub fn note_unsuccessful_coercion(
329         &mut self,
330         expected: DiagnosticStyledString,
331         found: DiagnosticStyledString,
332     ) -> &mut Self {
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),
338         }));
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),
343         }));
344         msg.push(("`".to_string(), Style::NoStyle));
345
346         // For now, just attach these as notes
347         self.highlighted_note(msg);
348         self
349     }
350
351     pub fn note_expected_found_extra(
352         &mut self,
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,
359     ) -> &mut Self {
360         let expected_label = expected_label.to_string();
361         let expected_label = if expected_label.is_empty() {
362             "expected".to_string()
363         } else {
364             format!("expected {}", expected_label)
365         };
366         let found_label = found_label.to_string();
367         let found_label = if found_label.is_empty() {
368             "found".to_string()
369         } else {
370             format!("found {}", found_label)
371         };
372         let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
373             (expected_label.len() - found_label.len(), 0)
374         } else {
375             (0, found_label.len() - expected_label.len())
376         };
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),
382         }));
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),
388         }));
389         msg.push((format!("`{}", found_extra), Style::NoStyle));
390
391         // For now, just attach these as notes.
392         self.highlighted_note(msg);
393         self
394     }
395
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),
401         ]);
402         self
403     }
404
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);
409         self
410     }
411
412     pub fn highlighted_note<M: Into<SubdiagnosticMessage>>(
413         &mut self,
414         msg: Vec<(M, Style)>,
415     ) -> &mut Self {
416         self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
417         self
418     }
419
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);
424         self
425     }
426
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>>(
431         &mut self,
432         sp: S,
433         msg: impl Into<SubdiagnosticMessage>,
434     ) -> &mut Self {
435         self.sub(Level::Note, msg, sp.into(), None);
436         self
437     }
438
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>>(
442         &mut self,
443         sp: S,
444         msg: impl Into<SubdiagnosticMessage>,
445     ) -> &mut Self {
446         self.sub(Level::OnceNote, msg, sp.into(), None);
447         self
448     }
449
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);
454         self
455     }
456
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>>(
461         &mut self,
462         sp: S,
463         msg: impl Into<SubdiagnosticMessage>,
464     ) -> &mut Self {
465         self.sub(Level::Warning, msg, sp.into(), None);
466         self
467     }
468
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);
473         self
474     }
475
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);
479         self
480     }
481
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>>(
486         &mut self,
487         sp: S,
488         msg: impl Into<SubdiagnosticMessage>,
489     ) -> &mut Self {
490         self.sub(Level::Help, msg, sp.into(), None);
491         self
492     }
493
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));
499         } else {
500             self.help(&format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION));
501         }
502         self.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
503         self
504     }
505
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);
511         self
512     }
513
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);
518         }
519     }
520
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(
524         &mut self,
525         msg: impl Into<SubdiagnosticMessage>,
526         suggestion: Vec<(Span, String)>,
527         applicability: Applicability,
528     ) -> &mut Self {
529         self.multipart_suggestion_with_style(
530             msg,
531             suggestion,
532             applicability,
533             SuggestionStyle::ShowCode,
534         )
535     }
536
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(
540         &mut self,
541         msg: impl Into<SubdiagnosticMessage>,
542         suggestion: Vec<(Span, String)>,
543         applicability: Applicability,
544     ) -> &mut Self {
545         self.multipart_suggestion_with_style(
546             msg,
547             suggestion,
548             applicability,
549             SuggestionStyle::ShowAlways,
550         )
551     }
552     /// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
553     pub fn multipart_suggestion_with_style(
554         &mut self,
555         msg: impl Into<SubdiagnosticMessage>,
556         suggestion: Vec<(Span, String)>,
557         applicability: Applicability,
558         style: SuggestionStyle,
559     ) -> &mut Self {
560         assert!(!suggestion.is_empty());
561         self.push_suggestion(CodeSuggestion {
562             substitutions: vec![Substitution {
563                 parts: suggestion
564                     .into_iter()
565                     .map(|(span, snippet)| SubstitutionPart { snippet, span })
566                     .collect(),
567             }],
568             msg: self.subdiagnostic_message_to_diagnostic_message(msg),
569             style,
570             applicability,
571         });
572         self
573     }
574
575     /// Prints out a message with for a multipart suggestion without showing the suggested code.
576     ///
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(
582         &mut self,
583         msg: impl Into<SubdiagnosticMessage>,
584         suggestion: Vec<(Span, String)>,
585         applicability: Applicability,
586     ) -> &mut Self {
587         assert!(!suggestion.is_empty());
588         self.push_suggestion(CodeSuggestion {
589             substitutions: vec![Substitution {
590                 parts: suggestion
591                     .into_iter()
592                     .map(|(span, snippet)| SubstitutionPart { snippet, span })
593                     .collect(),
594             }],
595             msg: self.subdiagnostic_message_to_diagnostic_message(msg),
596             style: SuggestionStyle::CompletelyHidden,
597             applicability,
598         });
599         self
600     }
601
602     /// Prints out a message with a suggested edit of the code.
603     ///
604     /// In case of short messages and a simple suggestion, rustc displays it as a label:
605     ///
606     /// ```text
607     /// try adding parentheses: `(tup.0).1`
608     /// ```
609     ///
610     /// The message
611     ///
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
617     ///
618     /// See `CodeSuggestion` for more information.
619     pub fn span_suggestion(
620         &mut self,
621         sp: Span,
622         msg: impl Into<SubdiagnosticMessage>,
623         suggestion: impl ToString,
624         applicability: Applicability,
625     ) -> &mut Self {
626         self.span_suggestion_with_style(
627             sp,
628             msg,
629             suggestion,
630             applicability,
631             SuggestionStyle::ShowCode,
632         );
633         self
634     }
635
636     /// [`Diagnostic::span_suggestion()`] but you can set the [`SuggestionStyle`].
637     pub fn span_suggestion_with_style(
638         &mut self,
639         sp: Span,
640         msg: impl Into<SubdiagnosticMessage>,
641         suggestion: impl ToString,
642         applicability: Applicability,
643         style: SuggestionStyle,
644     ) -> &mut Self {
645         self.push_suggestion(CodeSuggestion {
646             substitutions: vec![Substitution {
647                 parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
648             }],
649             msg: self.subdiagnostic_message_to_diagnostic_message(msg),
650             style,
651             applicability,
652         });
653         self
654     }
655
656     /// Always show the suggested change.
657     pub fn span_suggestion_verbose(
658         &mut self,
659         sp: Span,
660         msg: impl Into<SubdiagnosticMessage>,
661         suggestion: impl ToString,
662         applicability: Applicability,
663     ) -> &mut Self {
664         self.span_suggestion_with_style(
665             sp,
666             msg,
667             suggestion,
668             applicability,
669             SuggestionStyle::ShowAlways,
670         );
671         self
672     }
673
674     /// Prints out a message with multiple suggested edits of the code.
675     /// See also [`Diagnostic::span_suggestion()`].
676     pub fn span_suggestions(
677         &mut self,
678         sp: Span,
679         msg: impl Into<SubdiagnosticMessage>,
680         suggestions: impl Iterator<Item = String>,
681         applicability: Applicability,
682     ) -> &mut Self {
683         let mut suggestions: Vec<_> = suggestions.collect();
684         suggestions.sort();
685         let substitutions = suggestions
686             .into_iter()
687             .map(|snippet| Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] })
688             .collect();
689         self.push_suggestion(CodeSuggestion {
690             substitutions,
691             msg: self.subdiagnostic_message_to_diagnostic_message(msg),
692             style: SuggestionStyle::ShowCode,
693             applicability,
694         });
695         self
696     }
697
698     /// Prints out a message with multiple suggested edits of the code.
699     /// See also [`Diagnostic::span_suggestion()`].
700     pub fn multipart_suggestions(
701         &mut self,
702         msg: impl Into<SubdiagnosticMessage>,
703         suggestions: impl Iterator<Item = Vec<(Span, String)>>,
704         applicability: Applicability,
705     ) -> &mut Self {
706         self.push_suggestion(CodeSuggestion {
707             substitutions: suggestions
708                 .map(|sugg| Substitution {
709                     parts: sugg
710                         .into_iter()
711                         .map(|(span, snippet)| SubstitutionPart { snippet, span })
712                         .collect(),
713                 })
714                 .collect(),
715             msg: self.subdiagnostic_message_to_diagnostic_message(msg),
716             style: SuggestionStyle::ShowCode,
717             applicability,
718         });
719         self
720     }
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.
723     ///
724     /// See `CodeSuggestion` for more information.
725     pub fn span_suggestion_short(
726         &mut self,
727         sp: Span,
728         msg: impl Into<SubdiagnosticMessage>,
729         suggestion: impl ToString,
730         applicability: Applicability,
731     ) -> &mut Self {
732         self.span_suggestion_with_style(
733             sp,
734             msg,
735             suggestion,
736             applicability,
737             SuggestionStyle::HideCodeInline,
738         );
739         self
740     }
741
742     /// Prints out a message for a suggestion without showing the suggested code.
743     ///
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(
749         &mut self,
750         sp: Span,
751         msg: impl Into<SubdiagnosticMessage>,
752         suggestion: impl ToString,
753         applicability: Applicability,
754     ) -> &mut Self {
755         self.span_suggestion_with_style(
756             sp,
757             msg,
758             suggestion,
759             applicability,
760             SuggestionStyle::HideCodeAlways,
761         );
762         self
763     }
764
765     /// Adds a suggestion to the JSON output that will not be shown in the CLI.
766     ///
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(
770         &mut self,
771         sp: Span,
772         msg: impl Into<SubdiagnosticMessage>,
773         suggestion: impl ToString,
774         applicability: Applicability,
775     ) -> &mut Self {
776         self.span_suggestion_with_style(
777             sp,
778             msg,
779             suggestion,
780             applicability,
781             SuggestionStyle::CompletelyHidden,
782         );
783         self
784     }
785
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);
790         self
791     }
792
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;
797         }
798         self
799     }
800
801     pub fn set_is_lint(&mut self) -> &mut Self {
802         self.is_lint = true;
803         self
804     }
805
806     pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
807         self.code = Some(s);
808         self
809     }
810
811     pub fn clear_code(&mut self) -> &mut Self {
812         self.code = None;
813         self
814     }
815
816     pub fn get_code(&self) -> Option<DiagnosticId> {
817         self.code.clone()
818     }
819
820     pub fn set_primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
821         self.message[0] = (msg.into(), Style::NoStyle);
822         self
823     }
824
825     pub fn args(&self) -> &[DiagnosticArg<'static>] {
826         &self.args
827     }
828
829     pub fn set_arg(
830         &mut self,
831         name: impl Into<Cow<'static, str>>,
832         arg: impl IntoDiagnosticArg,
833     ) -> &mut Self {
834         self.args.push((name.into(), arg.into_diagnostic_arg()));
835         self
836     }
837
838     pub fn styled_message(&self) -> &[(DiagnosticMessage, Style)] {
839         &self.message
840     }
841
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(
846         &self,
847         attr: impl Into<SubdiagnosticMessage>,
848     ) -> DiagnosticMessage {
849         let msg =
850             self.message.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
851         msg.with_subdiagnostic_message(attr.into())
852     }
853
854     /// Convenience function for internal use, clients should use one of the
855     /// public methods above.
856     ///
857     /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
858     pub fn sub(
859         &mut self,
860         level: Level,
861         message: impl Into<SubdiagnosticMessage>,
862         span: MultiSpan,
863         render_span: Option<MultiSpan>,
864     ) {
865         let sub = SubDiagnostic {
866             level,
867             message: vec![(
868                 self.subdiagnostic_message_to_diagnostic_message(message),
869                 Style::NoStyle,
870             )],
871             span,
872             render_span,
873         };
874         self.children.push(sub);
875     }
876
877     /// Convenience function for internal use, clients should use one of the
878     /// public methods above.
879     fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
880         &mut self,
881         level: Level,
882         mut message: Vec<(M, Style)>,
883         span: MultiSpan,
884         render_span: Option<MultiSpan>,
885     ) {
886         let message = message
887             .drain(..)
888             .map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1))
889             .collect();
890         let sub = SubDiagnostic { level, message, span, render_span };
891         self.children.push(sub);
892     }
893
894     /// Fields used for Hash, and PartialEq trait
895     fn keys(
896         &self,
897     ) -> (
898         &Level,
899         &[(DiagnosticMessage, Style)],
900         &Option<DiagnosticId>,
901         &MultiSpan,
902         &Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
903         Option<&[SubDiagnostic]>,
904     ) {
905         (
906             &self.level,
907             &self.message,
908             &self.code,
909             &self.span,
910             &self.suggestions,
911             (if self.is_lint { None } else { Some(&self.children) }),
912         )
913     }
914 }
915
916 impl Hash for Diagnostic {
917     fn hash<H>(&self, state: &mut H)
918     where
919         H: Hasher,
920     {
921         self.keys().hash(state);
922     }
923 }
924
925 impl PartialEq for Diagnostic {
926     fn eq(&self, other: &Self) -> bool {
927         self.keys() == other.keys()
928     }
929 }