]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_errors/src/diagnostic.rs
New upstream version 1.63.0+dfsg1
[rustc.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) | Level::Warning(Some(expectation_id)) =
226 &mut self.level
227 {
228 if expectation_id.is_stable() {
229 return;
230 }
231
232 // The unstable to stable map only maps the unstable `AttrId` to a stable `HirId` with an attribute index.
233 // The lint index inside the attribute is manually transferred here.
234 let lint_index = expectation_id.get_lint_index();
235 expectation_id.set_lint_index(None);
236 let mut stable_id = *unstable_to_stable
237 .get(&expectation_id)
238 .expect("each unstable `LintExpectationId` must have a matching stable id");
239
240 stable_id.set_lint_index(lint_index);
241 *expectation_id = stable_id;
242 }
243 }
244
245 pub fn has_future_breakage(&self) -> bool {
246 match self.code {
247 Some(DiagnosticId::Lint { has_future_breakage, .. }) => has_future_breakage,
248 _ => false,
249 }
250 }
251
252 pub fn is_force_warn(&self) -> bool {
253 match self.code {
254 Some(DiagnosticId::Lint { is_force_warn, .. }) => is_force_warn,
255 _ => false,
256 }
257 }
258
259 /// Delay emission of this diagnostic as a bug.
260 ///
261 /// This can be useful in contexts where an error indicates a bug but
262 /// typically this only happens when other compilation errors have already
263 /// happened. In those cases this can be used to defer emission of this
264 /// diagnostic as a bug in the compiler only if no other errors have been
265 /// emitted.
266 ///
267 /// In the meantime, though, callsites are required to deal with the "bug"
268 /// locally in whichever way makes the most sense.
269 #[track_caller]
270 pub fn downgrade_to_delayed_bug(&mut self) -> &mut Self {
271 assert!(
272 self.is_error(),
273 "downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
274 self.level
275 );
276 self.level = Level::DelayedBug;
277
278 self
279 }
280
281 /// Adds a span/label to be included in the resulting snippet.
282 ///
283 /// This is pushed onto the [`MultiSpan`] that was created when the diagnostic
284 /// was first built. That means it will be shown together with the original
285 /// span/label, *not* a span added by one of the `span_{note,warn,help,suggestions}` methods.
286 ///
287 /// This span is *not* considered a ["primary span"][`MultiSpan`]; only
288 /// the `Span` supplied when creating the diagnostic is primary.
289 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
290 pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self {
291 self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label));
292 self
293 }
294
295 /// Labels all the given spans with the provided label.
296 /// See [`Self::span_label()`] for more information.
297 pub fn span_labels(
298 &mut self,
299 spans: impl IntoIterator<Item = Span>,
300 label: impl AsRef<str>,
301 ) -> &mut Self {
302 let label = label.as_ref();
303 for span in spans {
304 self.span_label(span, label);
305 }
306 self
307 }
308
309 pub fn replace_span_with(&mut self, after: Span) -> &mut Self {
310 let before = self.span.clone();
311 self.set_span(after);
312 for span_label in before.span_labels() {
313 if let Some(label) = span_label.label {
314 self.span.push_span_label(after, label);
315 }
316 }
317 self
318 }
319
320 pub fn note_expected_found(
321 &mut self,
322 expected_label: &dyn fmt::Display,
323 expected: DiagnosticStyledString,
324 found_label: &dyn fmt::Display,
325 found: DiagnosticStyledString,
326 ) -> &mut Self {
327 self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"")
328 }
329
330 pub fn note_unsuccessful_coercion(
331 &mut self,
332 expected: DiagnosticStyledString,
333 found: DiagnosticStyledString,
334 ) -> &mut Self {
335 let mut msg: Vec<_> =
336 vec![("required when trying to coerce from type `".to_string(), Style::NoStyle)];
337 msg.extend(expected.0.iter().map(|x| match *x {
338 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
339 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
340 }));
341 msg.push(("` to type '".to_string(), Style::NoStyle));
342 msg.extend(found.0.iter().map(|x| match *x {
343 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
344 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
345 }));
346 msg.push(("`".to_string(), Style::NoStyle));
347
348 // For now, just attach these as notes
349 self.highlighted_note(msg);
350 self
351 }
352
353 pub fn note_expected_found_extra(
354 &mut self,
355 expected_label: &dyn fmt::Display,
356 expected: DiagnosticStyledString,
357 found_label: &dyn fmt::Display,
358 found: DiagnosticStyledString,
359 expected_extra: &dyn fmt::Display,
360 found_extra: &dyn fmt::Display,
361 ) -> &mut Self {
362 let expected_label = expected_label.to_string();
363 let expected_label = if expected_label.is_empty() {
364 "expected".to_string()
365 } else {
366 format!("expected {}", expected_label)
367 };
368 let found_label = found_label.to_string();
369 let found_label = if found_label.is_empty() {
370 "found".to_string()
371 } else {
372 format!("found {}", found_label)
373 };
374 let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
375 (expected_label.len() - found_label.len(), 0)
376 } else {
377 (0, found_label.len() - expected_label.len())
378 };
379 let mut msg: Vec<_> =
380 vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
381 msg.extend(expected.0.iter().map(|x| match *x {
382 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
383 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
384 }));
385 msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
386 msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
387 msg.extend(found.0.iter().map(|x| match *x {
388 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
389 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
390 }));
391 msg.push((format!("`{}", found_extra), Style::NoStyle));
392
393 // For now, just attach these as notes.
394 self.highlighted_note(msg);
395 self
396 }
397
398 pub fn note_trait_signature(&mut self, name: String, signature: String) -> &mut Self {
399 self.highlighted_note(vec![
400 (format!("`{}` from trait: `", name), Style::NoStyle),
401 (signature, Style::Highlight),
402 ("`".to_string(), Style::NoStyle),
403 ]);
404 self
405 }
406
407 /// Add a note attached to this diagnostic.
408 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
409 pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
410 self.sub(Level::Note, msg, MultiSpan::new(), None);
411 self
412 }
413
414 pub fn highlighted_note<M: Into<SubdiagnosticMessage>>(
415 &mut self,
416 msg: Vec<(M, Style)>,
417 ) -> &mut Self {
418 self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
419 self
420 }
421
422 /// Prints the span with a note above it.
423 /// This is like [`Diagnostic::note()`], but it gets its own span.
424 pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
425 self.sub(Level::OnceNote, msg, MultiSpan::new(), None);
426 self
427 }
428
429 /// Prints the span with a note above it.
430 /// This is like [`Diagnostic::note()`], but it gets its own span.
431 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
432 pub fn span_note<S: Into<MultiSpan>>(
433 &mut self,
434 sp: S,
435 msg: impl Into<SubdiagnosticMessage>,
436 ) -> &mut Self {
437 self.sub(Level::Note, msg, sp.into(), None);
438 self
439 }
440
441 /// Prints the span with a note above it.
442 /// This is like [`Diagnostic::note()`], but it gets its own span.
443 pub fn span_note_once<S: Into<MultiSpan>>(
444 &mut self,
445 sp: S,
446 msg: impl Into<SubdiagnosticMessage>,
447 ) -> &mut Self {
448 self.sub(Level::OnceNote, msg, sp.into(), None);
449 self
450 }
451
452 /// Add a warning attached to this diagnostic.
453 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
454 pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
455 self.sub(Level::Warning(None), msg, MultiSpan::new(), None);
456 self
457 }
458
459 /// Prints the span with a warning above it.
460 /// This is like [`Diagnostic::warn()`], but it gets its own span.
461 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
462 pub fn span_warn<S: Into<MultiSpan>>(
463 &mut self,
464 sp: S,
465 msg: impl Into<SubdiagnosticMessage>,
466 ) -> &mut Self {
467 self.sub(Level::Warning(None), msg, sp.into(), None);
468 self
469 }
470
471 /// Add a help message attached to this diagnostic.
472 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
473 pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
474 self.sub(Level::Help, msg, MultiSpan::new(), None);
475 self
476 }
477
478 /// Add a help message attached to this diagnostic with a customizable highlighted message.
479 pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
480 self.sub_with_highlights(Level::Help, msg, MultiSpan::new(), None);
481 self
482 }
483
484 /// Prints the span with some help above it.
485 /// This is like [`Diagnostic::help()`], but it gets its own span.
486 #[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
487 pub fn span_help<S: Into<MultiSpan>>(
488 &mut self,
489 sp: S,
490 msg: impl Into<SubdiagnosticMessage>,
491 ) -> &mut Self {
492 self.sub(Level::Help, msg, sp.into(), None);
493 self
494 }
495
496 /// Help the user upgrade to the latest edition.
497 /// This is factored out to make sure it does the right thing with `Cargo.toml`.
498 pub fn help_use_latest_edition(&mut self) -> &mut Self {
499 if std::env::var_os("CARGO").is_some() {
500 self.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
501 } else {
502 self.help(&format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION));
503 }
504 self.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
505 self
506 }
507
508 /// Disallow attaching suggestions this diagnostic.
509 /// Any suggestions attached e.g. with the `span_suggestion_*` methods
510 /// (before and after the call to `disable_suggestions`) will be ignored.
511 pub fn disable_suggestions(&mut self) -> &mut Self {
512 self.suggestions = Err(SuggestionsDisabled);
513 self
514 }
515
516 /// Helper for pushing to `self.suggestions`, if available (not disable).
517 fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
518 if let Ok(suggestions) = &mut self.suggestions {
519 suggestions.push(suggestion);
520 }
521 }
522
523 /// Show a suggestion that has multiple parts to it.
524 /// In other words, multiple changes need to be applied as part of this suggestion.
525 pub fn multipart_suggestion(
526 &mut self,
527 msg: impl Into<SubdiagnosticMessage>,
528 suggestion: Vec<(Span, String)>,
529 applicability: Applicability,
530 ) -> &mut Self {
531 self.multipart_suggestion_with_style(
532 msg,
533 suggestion,
534 applicability,
535 SuggestionStyle::ShowCode,
536 )
537 }
538
539 /// Show a suggestion that has multiple parts to it, always as it's own subdiagnostic.
540 /// In other words, multiple changes need to be applied as part of this suggestion.
541 pub fn multipart_suggestion_verbose(
542 &mut self,
543 msg: impl Into<SubdiagnosticMessage>,
544 suggestion: Vec<(Span, String)>,
545 applicability: Applicability,
546 ) -> &mut Self {
547 self.multipart_suggestion_with_style(
548 msg,
549 suggestion,
550 applicability,
551 SuggestionStyle::ShowAlways,
552 )
553 }
554 /// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
555 pub fn multipart_suggestion_with_style(
556 &mut self,
557 msg: impl Into<SubdiagnosticMessage>,
558 suggestion: Vec<(Span, String)>,
559 applicability: Applicability,
560 style: SuggestionStyle,
561 ) -> &mut Self {
562 assert!(!suggestion.is_empty());
563 self.push_suggestion(CodeSuggestion {
564 substitutions: vec![Substitution {
565 parts: suggestion
566 .into_iter()
567 .map(|(span, snippet)| SubstitutionPart { snippet, span })
568 .collect(),
569 }],
570 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
571 style,
572 applicability,
573 });
574 self
575 }
576
577 /// Prints out a message with for a multipart suggestion without showing the suggested code.
578 ///
579 /// This is intended to be used for suggestions that are obvious in what the changes need to
580 /// be from the message, showing the span label inline would be visually unpleasant
581 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
582 /// improve understandability.
583 pub fn tool_only_multipart_suggestion(
584 &mut self,
585 msg: impl Into<SubdiagnosticMessage>,
586 suggestion: Vec<(Span, String)>,
587 applicability: Applicability,
588 ) -> &mut Self {
589 assert!(!suggestion.is_empty());
590 self.push_suggestion(CodeSuggestion {
591 substitutions: vec![Substitution {
592 parts: suggestion
593 .into_iter()
594 .map(|(span, snippet)| SubstitutionPart { snippet, span })
595 .collect(),
596 }],
597 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
598 style: SuggestionStyle::CompletelyHidden,
599 applicability,
600 });
601 self
602 }
603
604 /// Prints out a message with a suggested edit of the code.
605 ///
606 /// In case of short messages and a simple suggestion, rustc displays it as a label:
607 ///
608 /// ```text
609 /// try adding parentheses: `(tup.0).1`
610 /// ```
611 ///
612 /// The message
613 ///
614 /// * should not end in any punctuation (a `:` is added automatically)
615 /// * should not be a question (avoid language like "did you mean")
616 /// * should not contain any phrases like "the following", "as shown", etc.
617 /// * may look like "to do xyz, use" or "to do xyz, use abc"
618 /// * may contain a name of a function, variable, or type, but not whole expressions
619 ///
620 /// See `CodeSuggestion` for more information.
621 pub fn span_suggestion(
622 &mut self,
623 sp: Span,
624 msg: impl Into<SubdiagnosticMessage>,
625 suggestion: impl ToString,
626 applicability: Applicability,
627 ) -> &mut Self {
628 self.span_suggestion_with_style(
629 sp,
630 msg,
631 suggestion,
632 applicability,
633 SuggestionStyle::ShowCode,
634 );
635 self
636 }
637
638 /// [`Diagnostic::span_suggestion()`] but you can set the [`SuggestionStyle`].
639 pub fn span_suggestion_with_style(
640 &mut self,
641 sp: Span,
642 msg: impl Into<SubdiagnosticMessage>,
643 suggestion: impl ToString,
644 applicability: Applicability,
645 style: SuggestionStyle,
646 ) -> &mut Self {
647 self.push_suggestion(CodeSuggestion {
648 substitutions: vec![Substitution {
649 parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
650 }],
651 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
652 style,
653 applicability,
654 });
655 self
656 }
657
658 /// Always show the suggested change.
659 pub fn span_suggestion_verbose(
660 &mut self,
661 sp: Span,
662 msg: impl Into<SubdiagnosticMessage>,
663 suggestion: impl ToString,
664 applicability: Applicability,
665 ) -> &mut Self {
666 self.span_suggestion_with_style(
667 sp,
668 msg,
669 suggestion,
670 applicability,
671 SuggestionStyle::ShowAlways,
672 );
673 self
674 }
675
676 /// Prints out a message with multiple suggested edits of the code.
677 /// See also [`Diagnostic::span_suggestion()`].
678 pub fn span_suggestions(
679 &mut self,
680 sp: Span,
681 msg: impl Into<SubdiagnosticMessage>,
682 suggestions: impl Iterator<Item = String>,
683 applicability: Applicability,
684 ) -> &mut Self {
685 let mut suggestions: Vec<_> = suggestions.collect();
686 suggestions.sort();
687 let substitutions = suggestions
688 .into_iter()
689 .map(|snippet| Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] })
690 .collect();
691 self.push_suggestion(CodeSuggestion {
692 substitutions,
693 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
694 style: SuggestionStyle::ShowCode,
695 applicability,
696 });
697 self
698 }
699
700 /// Prints out a message with multiple suggested edits of the code.
701 /// See also [`Diagnostic::span_suggestion()`].
702 pub fn multipart_suggestions(
703 &mut self,
704 msg: impl Into<SubdiagnosticMessage>,
705 suggestions: impl Iterator<Item = Vec<(Span, String)>>,
706 applicability: Applicability,
707 ) -> &mut Self {
708 self.push_suggestion(CodeSuggestion {
709 substitutions: suggestions
710 .map(|sugg| Substitution {
711 parts: sugg
712 .into_iter()
713 .map(|(span, snippet)| SubstitutionPart { snippet, span })
714 .collect(),
715 })
716 .collect(),
717 msg: self.subdiagnostic_message_to_diagnostic_message(msg),
718 style: SuggestionStyle::ShowCode,
719 applicability,
720 });
721 self
722 }
723 /// Prints out a message with a suggested edit of the code. If the suggestion is presented
724 /// inline, it will only show the message and not the suggestion.
725 ///
726 /// See `CodeSuggestion` for more information.
727 pub fn span_suggestion_short(
728 &mut self,
729 sp: Span,
730 msg: impl Into<SubdiagnosticMessage>,
731 suggestion: impl ToString,
732 applicability: Applicability,
733 ) -> &mut Self {
734 self.span_suggestion_with_style(
735 sp,
736 msg,
737 suggestion,
738 applicability,
739 SuggestionStyle::HideCodeInline,
740 );
741 self
742 }
743
744 /// Prints out a message for a suggestion without showing the suggested code.
745 ///
746 /// This is intended to be used for suggestions that are obvious in what the changes need to
747 /// be from the message, showing the span label inline would be visually unpleasant
748 /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
749 /// improve understandability.
750 pub fn span_suggestion_hidden(
751 &mut self,
752 sp: Span,
753 msg: impl Into<SubdiagnosticMessage>,
754 suggestion: impl ToString,
755 applicability: Applicability,
756 ) -> &mut Self {
757 self.span_suggestion_with_style(
758 sp,
759 msg,
760 suggestion,
761 applicability,
762 SuggestionStyle::HideCodeAlways,
763 );
764 self
765 }
766
767 /// Adds a suggestion to the JSON output that will not be shown in the CLI.
768 ///
769 /// This is intended to be used for suggestions that are *very* obvious in what the changes
770 /// need to be from the message, but we still want other tools to be able to apply them.
771 pub fn tool_only_span_suggestion(
772 &mut self,
773 sp: Span,
774 msg: impl Into<SubdiagnosticMessage>,
775 suggestion: impl ToString,
776 applicability: Applicability,
777 ) -> &mut Self {
778 self.span_suggestion_with_style(
779 sp,
780 msg,
781 suggestion,
782 applicability,
783 SuggestionStyle::CompletelyHidden,
784 );
785 self
786 }
787
788 /// Add a subdiagnostic from a type that implements `SessionSubdiagnostic` - see
789 /// [rustc_macros::SessionSubdiagnostic].
790 pub fn subdiagnostic(&mut self, subdiagnostic: impl AddSubdiagnostic) -> &mut Self {
791 subdiagnostic.add_to_diagnostic(self);
792 self
793 }
794
795 pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
796 self.span = sp.into();
797 if let Some(span) = self.span.primary_span() {
798 self.sort_span = span;
799 }
800 self
801 }
802
803 pub fn set_is_lint(&mut self) -> &mut Self {
804 self.is_lint = true;
805 self
806 }
807
808 pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
809 self.code = Some(s);
810 self
811 }
812
813 pub fn clear_code(&mut self) -> &mut Self {
814 self.code = None;
815 self
816 }
817
818 pub fn get_code(&self) -> Option<DiagnosticId> {
819 self.code.clone()
820 }
821
822 pub fn set_primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
823 self.message[0] = (msg.into(), Style::NoStyle);
824 self
825 }
826
827 pub fn args(&self) -> &[DiagnosticArg<'static>] {
828 &self.args
829 }
830
831 pub fn set_arg(
832 &mut self,
833 name: impl Into<Cow<'static, str>>,
834 arg: impl IntoDiagnosticArg,
835 ) -> &mut Self {
836 self.args.push((name.into(), arg.into_diagnostic_arg()));
837 self
838 }
839
840 pub fn styled_message(&self) -> &[(DiagnosticMessage, Style)] {
841 &self.message
842 }
843
844 /// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
845 /// combining it with the primary message of the diagnostic (if translatable, otherwise it just
846 /// passes the user's string along).
847 fn subdiagnostic_message_to_diagnostic_message(
848 &self,
849 attr: impl Into<SubdiagnosticMessage>,
850 ) -> DiagnosticMessage {
851 let msg =
852 self.message.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
853 msg.with_subdiagnostic_message(attr.into())
854 }
855
856 /// Convenience function for internal use, clients should use one of the
857 /// public methods above.
858 ///
859 /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
860 pub fn sub(
861 &mut self,
862 level: Level,
863 message: impl Into<SubdiagnosticMessage>,
864 span: MultiSpan,
865 render_span: Option<MultiSpan>,
866 ) {
867 let sub = SubDiagnostic {
868 level,
869 message: vec![(
870 self.subdiagnostic_message_to_diagnostic_message(message),
871 Style::NoStyle,
872 )],
873 span,
874 render_span,
875 };
876 self.children.push(sub);
877 }
878
879 /// Convenience function for internal use, clients should use one of the
880 /// public methods above.
881 fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
882 &mut self,
883 level: Level,
884 mut message: Vec<(M, Style)>,
885 span: MultiSpan,
886 render_span: Option<MultiSpan>,
887 ) {
888 let message = message
889 .drain(..)
890 .map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1))
891 .collect();
892 let sub = SubDiagnostic { level, message, span, render_span };
893 self.children.push(sub);
894 }
895
896 /// Fields used for Hash, and PartialEq trait
897 fn keys(
898 &self,
899 ) -> (
900 &Level,
901 &[(DiagnosticMessage, Style)],
902 &Option<DiagnosticId>,
903 &MultiSpan,
904 &Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
905 Option<&[SubDiagnostic]>,
906 ) {
907 (
908 &self.level,
909 &self.message,
910 &self.code,
911 &self.span,
912 &self.suggestions,
913 (if self.is_lint { None } else { Some(&self.children) }),
914 )
915 }
916 }
917
918 impl Hash for Diagnostic {
919 fn hash<H>(&self, state: &mut H)
920 where
921 H: Hasher,
922 {
923 self.keys().hash(state);
924 }
925 }
926
927 impl PartialEq for Diagnostic {
928 fn eq(&self, other: &Self) -> bool {
929 self.keys() == other.keys()
930 }
931 }