rustc_errors/
diagnostic.rs

1use std::borrow::Cow;
2use std::fmt::{self, Debug};
3use std::hash::{Hash, Hasher};
4use std::marker::PhantomData;
5use std::ops::{Deref, DerefMut};
6use std::panic;
7use std::path::PathBuf;
8use std::thread::panicking;
9
10use rustc_data_structures::fx::FxIndexMap;
11use rustc_error_messages::{DiagArgName, DiagArgValue, IntoDiagArg};
12use rustc_lint_defs::{Applicability, LintExpectationId};
13use rustc_macros::{Decodable, Encodable};
14use rustc_span::source_map::Spanned;
15use rustc_span::{DUMMY_SP, Span, Symbol};
16use tracing::debug;
17
18use crate::snippet::Style;
19use crate::{
20    CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
21    MultiSpan, StashKey, SubdiagMessage, Substitution, SubstitutionPart, SuggestionStyle,
22    Suggestions,
23};
24
25pub type DiagArgMap = FxIndexMap<DiagArgName, DiagArgValue>;
26
27/// Trait for types that `Diag::emit` can return as a "guarantee" (or "proof")
28/// token that the emission happened.
29pub trait EmissionGuarantee: Sized {
30    /// This exists so that bugs and fatal errors can both result in `!` (an
31    /// abort) when emitted, but have different aborting behaviour.
32    type EmitResult = Self;
33
34    /// Implementation of `Diag::emit`, fully controlled by each `impl` of
35    /// `EmissionGuarantee`, to make it impossible to create a value of
36    /// `Self::EmitResult` without actually performing the emission.
37    #[track_caller]
38    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult;
39}
40
41impl EmissionGuarantee for ErrorGuaranteed {
42    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
43        diag.emit_producing_error_guaranteed()
44    }
45}
46
47impl EmissionGuarantee for () {
48    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
49        diag.emit_producing_nothing();
50    }
51}
52
53/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
54/// bug diagnostics.
55#[derive(Copy, Clone)]
56pub struct BugAbort;
57
58impl EmissionGuarantee for BugAbort {
59    type EmitResult = !;
60
61    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
62        diag.emit_producing_nothing();
63        panic::panic_any(ExplicitBug);
64    }
65}
66
67/// Marker type which enables implementation of `create_fatal` and `emit_fatal` functions for
68/// fatal diagnostics.
69#[derive(Copy, Clone)]
70pub struct FatalAbort;
71
72impl EmissionGuarantee for FatalAbort {
73    type EmitResult = !;
74
75    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
76        diag.emit_producing_nothing();
77        crate::FatalError.raise()
78    }
79}
80
81impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
82    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
83        diag.emit_producing_nothing();
84        rustc_span::fatal_error::FatalError
85    }
86}
87
88/// Trait implemented by error types. This is rarely implemented manually. Instead, use
89/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
90///
91/// When implemented manually, it should be generic over the emission
92/// guarantee, i.e.:
93/// ```ignore (fragment)
94/// impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Foo { ... }
95/// ```
96/// rather than being specific:
97/// ```ignore (fragment)
98/// impl<'a> Diagnostic<'a> for Bar { ... }  // the default type param is `ErrorGuaranteed`
99/// impl<'a> Diagnostic<'a, ()> for Baz { ... }
100/// ```
101/// There are two reasons for this.
102/// - A diagnostic like `Foo` *could* be emitted at any level -- `level` is
103///   passed in to `into_diag` from outside. Even if in practice it is
104///   always emitted at a single level, we let the diagnostic creation/emission
105///   site determine the level (by using `create_err`, `emit_warn`, etc.)
106///   rather than the `Diagnostic` impl.
107/// - Derived impls are always generic, and it's good for the hand-written
108///   impls to be consistent with them.
109#[rustc_diagnostic_item = "Diagnostic"]
110pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
111    /// Write out as a diagnostic out of `DiagCtxt`.
112    #[must_use]
113    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>;
114}
115
116impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
117where
118    T: Diagnostic<'a, G>,
119    G: EmissionGuarantee,
120{
121    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
122        self.node.into_diag(dcx, level).with_span(self.span)
123    }
124}
125
126/// Trait implemented by error types. This should not be implemented manually. Instead, use
127/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
128#[rustc_diagnostic_item = "Subdiagnostic"]
129pub trait Subdiagnostic
130where
131    Self: Sized,
132{
133    /// Add a subdiagnostic to an existing diagnostic.
134    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>);
135}
136
137/// Trait implemented by lint types. This should not be implemented manually. Instead, use
138/// `#[derive(LintDiagnostic)]` -- see [rustc_macros::LintDiagnostic].
139#[rustc_diagnostic_item = "LintDiagnostic"]
140pub trait LintDiagnostic<'a, G: EmissionGuarantee> {
141    /// Decorate a lint with the information from this type.
142    fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, G>);
143}
144
145pub trait LintDiagnosticBox<'a, G: EmissionGuarantee> {
146    fn decorate_lint_box<'b>(self: Box<Self>, diag: &'b mut Diag<'a, G>);
147}
148
149impl<'a, G: EmissionGuarantee, D: LintDiagnostic<'a, G>> LintDiagnosticBox<'a, G> for D {
150    fn decorate_lint_box<'b>(self: Box<Self>, diag: &'b mut Diag<'a, G>) {
151        self.decorate_lint(diag);
152    }
153}
154
155#[derive(Clone, Debug, Encodable, Decodable)]
156pub(crate) struct DiagLocation {
157    file: Cow<'static, str>,
158    line: u32,
159    col: u32,
160}
161
162impl DiagLocation {
163    #[track_caller]
164    fn caller() -> Self {
165        let loc = panic::Location::caller();
166        DiagLocation { file: loc.file().into(), line: loc.line(), col: loc.column() }
167    }
168}
169
170impl fmt::Display for DiagLocation {
171    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172        write!(f, "{}:{}:{}", self.file, self.line, self.col)
173    }
174}
175
176#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
177pub struct IsLint {
178    /// The lint name.
179    pub(crate) name: String,
180    /// Indicates whether this lint should show up in cargo's future breakage report.
181    has_future_breakage: bool,
182}
183
184#[derive(Debug, PartialEq, Eq)]
185pub struct DiagStyledString(pub Vec<StringPart>);
186
187impl DiagStyledString {
188    pub fn new() -> DiagStyledString {
189        DiagStyledString(vec![])
190    }
191    pub fn push_normal<S: Into<String>>(&mut self, t: S) {
192        self.0.push(StringPart::normal(t));
193    }
194    pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
195        self.0.push(StringPart::highlighted(t));
196    }
197    pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
198        if highlight {
199            self.push_highlighted(t);
200        } else {
201            self.push_normal(t);
202        }
203    }
204    pub fn normal<S: Into<String>>(t: S) -> DiagStyledString {
205        DiagStyledString(vec![StringPart::normal(t)])
206    }
207
208    pub fn highlighted<S: Into<String>>(t: S) -> DiagStyledString {
209        DiagStyledString(vec![StringPart::highlighted(t)])
210    }
211
212    pub fn content(&self) -> String {
213        self.0.iter().map(|x| x.content.as_str()).collect::<String>()
214    }
215}
216
217#[derive(Debug, PartialEq, Eq)]
218pub struct StringPart {
219    content: String,
220    style: Style,
221}
222
223impl StringPart {
224    pub fn normal<S: Into<String>>(content: S) -> StringPart {
225        StringPart { content: content.into(), style: Style::NoStyle }
226    }
227
228    pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
229        StringPart { content: content.into(), style: Style::Highlight }
230    }
231}
232
233/// The main part of a diagnostic. Note that `Diag`, which wraps this type, is
234/// used for most operations, and should be used instead whenever possible.
235/// This type should only be used when `Diag`'s lifetime causes difficulties,
236/// e.g. when storing diagnostics within `DiagCtxt`.
237#[must_use]
238#[derive(Clone, Debug, Encodable, Decodable)]
239pub struct DiagInner {
240    // NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
241    // outside of what methods in this crate themselves allow.
242    pub(crate) level: Level,
243
244    pub messages: Vec<(DiagMessage, Style)>,
245    pub code: Option<ErrCode>,
246    pub lint_id: Option<LintExpectationId>,
247    pub span: MultiSpan,
248    pub children: Vec<Subdiag>,
249    pub suggestions: Suggestions,
250    pub args: DiagArgMap,
251
252    // This is used to store args and restore them after a subdiagnostic is rendered.
253    pub reserved_args: DiagArgMap,
254
255    /// This is not used for highlighting or rendering any error message. Rather, it can be used
256    /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
257    /// `span` if there is one. Otherwise, it is `DUMMY_SP`.
258    pub sort_span: Span,
259
260    pub is_lint: Option<IsLint>,
261
262    pub long_ty_path: Option<PathBuf>,
263    /// With `-Ztrack_diagnostics` enabled,
264    /// we print where in rustc this error was emitted.
265    pub(crate) emitted_at: DiagLocation,
266}
267
268impl DiagInner {
269    #[track_caller]
270    pub fn new<M: Into<DiagMessage>>(level: Level, message: M) -> Self {
271        DiagInner::new_with_messages(level, vec![(message.into(), Style::NoStyle)])
272    }
273
274    #[track_caller]
275    pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> Self {
276        DiagInner {
277            level,
278            lint_id: None,
279            messages,
280            code: None,
281            span: MultiSpan::new(),
282            children: vec![],
283            suggestions: Suggestions::Enabled(vec![]),
284            args: Default::default(),
285            reserved_args: Default::default(),
286            sort_span: DUMMY_SP,
287            is_lint: None,
288            long_ty_path: None,
289            emitted_at: DiagLocation::caller(),
290        }
291    }
292
293    #[inline(always)]
294    pub fn level(&self) -> Level {
295        self.level
296    }
297
298    pub fn is_error(&self) -> bool {
299        match self.level {
300            Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
301
302            Level::ForceWarning
303            | Level::Warning
304            | Level::Note
305            | Level::OnceNote
306            | Level::Help
307            | Level::OnceHelp
308            | Level::FailureNote
309            | Level::Allow
310            | Level::Expect => false,
311        }
312    }
313
314    /// Indicates whether this diagnostic should show up in cargo's future breakage report.
315    pub(crate) fn has_future_breakage(&self) -> bool {
316        matches!(self.is_lint, Some(IsLint { has_future_breakage: true, .. }))
317    }
318
319    pub(crate) fn is_force_warn(&self) -> bool {
320        match self.level {
321            Level::ForceWarning => {
322                assert!(self.is_lint.is_some());
323                true
324            }
325            _ => false,
326        }
327    }
328
329    // See comment on `Diag::subdiagnostic_message_to_diagnostic_message`.
330    pub(crate) fn subdiagnostic_message_to_diagnostic_message(
331        &self,
332        attr: impl Into<SubdiagMessage>,
333    ) -> DiagMessage {
334        let msg =
335            self.messages.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
336        msg.with_subdiagnostic_message(attr.into())
337    }
338
339    pub(crate) fn sub(
340        &mut self,
341        level: Level,
342        message: impl Into<SubdiagMessage>,
343        span: MultiSpan,
344    ) {
345        let sub = Subdiag {
346            level,
347            messages: vec![(
348                self.subdiagnostic_message_to_diagnostic_message(message),
349                Style::NoStyle,
350            )],
351            span,
352        };
353        self.children.push(sub);
354    }
355
356    pub(crate) fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg) {
357        let name = name.into();
358        let value = arg.into_diag_arg(&mut self.long_ty_path);
359        // This assertion is to avoid subdiagnostics overwriting an existing diagnostic arg.
360        debug_assert!(
361            !self.args.contains_key(&name) || self.args.get(&name) == Some(&value),
362            "arg {} already exists",
363            name
364        );
365        self.args.insert(name, value);
366    }
367
368    pub fn remove_arg(&mut self, name: &str) {
369        self.args.swap_remove(name);
370    }
371
372    pub fn store_args(&mut self) {
373        self.reserved_args = self.args.clone();
374    }
375
376    pub fn restore_args(&mut self) {
377        self.args = std::mem::take(&mut self.reserved_args);
378    }
379
380    pub fn emitted_at_sub_diag(&self) -> Subdiag {
381        let track = format!("-Ztrack-diagnostics: created at {}", self.emitted_at);
382        Subdiag {
383            level: crate::Level::Note,
384            messages: vec![(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)],
385            span: MultiSpan::new(),
386        }
387    }
388
389    /// Fields used for Hash, and PartialEq trait.
390    fn keys(
391        &self,
392    ) -> (
393        &Level,
394        &[(DiagMessage, Style)],
395        &Option<ErrCode>,
396        &MultiSpan,
397        &[Subdiag],
398        &Suggestions,
399        Vec<(&DiagArgName, &DiagArgValue)>,
400        &Option<IsLint>,
401    ) {
402        (
403            &self.level,
404            &self.messages,
405            &self.code,
406            &self.span,
407            &self.children,
408            &self.suggestions,
409            self.args.iter().collect(),
410            // omit self.sort_span
411            &self.is_lint,
412            // omit self.emitted_at
413        )
414    }
415}
416
417impl Hash for DiagInner {
418    fn hash<H>(&self, state: &mut H)
419    where
420        H: Hasher,
421    {
422        self.keys().hash(state);
423    }
424}
425
426impl PartialEq for DiagInner {
427    fn eq(&self, other: &Self) -> bool {
428        self.keys() == other.keys()
429    }
430}
431
432/// A "sub"-diagnostic attached to a parent diagnostic.
433/// For example, a note attached to an error.
434#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
435pub struct Subdiag {
436    pub level: Level,
437    pub messages: Vec<(DiagMessage, Style)>,
438    pub span: MultiSpan,
439}
440
441/// Used for emitting structured error messages and other diagnostic information.
442/// Wraps a `DiagInner`, adding some useful things.
443/// - The `dcx` field, allowing it to (a) emit itself, and (b) do a drop check
444///   that it has been emitted or cancelled.
445/// - The `EmissionGuarantee`, which determines the type returned from `emit`.
446///
447/// Each constructed `Diag` must be consumed by a function such as `emit`,
448/// `cancel`, `delay_as_bug`, or `into_diag`. A panic occurs if a `Diag`
449/// is dropped without being consumed by one of these functions.
450///
451/// If there is some state in a downstream crate you would like to access in
452/// the methods of `Diag` here, consider extending `DiagCtxtFlags`.
453#[must_use]
454pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> {
455    pub dcx: DiagCtxtHandle<'a>,
456
457    /// Why the `Option`? It is always `Some` until the `Diag` is consumed via
458    /// `emit`, `cancel`, etc. At that point it is consumed and replaced with
459    /// `None`. Then `drop` checks that it is `None`; if not, it panics because
460    /// a diagnostic was built but not used.
461    ///
462    /// Why the Box? `DiagInner` is a large type, and `Diag` is often used as a
463    /// return value, especially within the frequently-used `PResult` type. In
464    /// theory, return value optimization (RVO) should avoid unnecessary
465    /// copying. In practice, it does not (at the time of writing).
466    diag: Option<Box<DiagInner>>,
467
468    _marker: PhantomData<G>,
469}
470
471// Cloning a `Diag` is a recipe for a diagnostic being emitted twice, which
472// would be bad.
473impl<G> !Clone for Diag<'_, G> {}
474
475rustc_data_structures::static_assert_size!(Diag<'_, ()>, 3 * size_of::<usize>());
476
477impl<G: EmissionGuarantee> Deref for Diag<'_, G> {
478    type Target = DiagInner;
479
480    fn deref(&self) -> &DiagInner {
481        self.diag.as_ref().unwrap()
482    }
483}
484
485impl<G: EmissionGuarantee> DerefMut for Diag<'_, G> {
486    fn deref_mut(&mut self) -> &mut DiagInner {
487        self.diag.as_mut().unwrap()
488    }
489}
490
491impl<G: EmissionGuarantee> Debug for Diag<'_, G> {
492    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493        self.diag.fmt(f)
494    }
495}
496
497/// `Diag` impls many `&mut self -> &mut Self` methods. Each one modifies an
498/// existing diagnostic, either in a standalone fashion, e.g.
499/// `err.code(code);`, or in a chained fashion to make multiple modifications,
500/// e.g. `err.code(code).span(span);`.
501///
502/// This macro creates an equivalent `self -> Self` method, with a `with_`
503/// prefix. This can be used in a chained fashion when making a new diagnostic,
504/// e.g. `let err = struct_err(msg).with_code(code);`, or emitting a new
505/// diagnostic, e.g. `struct_err(msg).with_code(code).emit();`.
506///
507/// Although the latter method can be used to modify an existing diagnostic,
508/// e.g. `err = err.with_code(code);`, this should be avoided because the former
509/// method gives shorter code, e.g. `err.code(code);`.
510///
511/// Note: the `with_` methods are added only when needed. If you want to use
512/// one and it's not defined, feel free to add it.
513///
514/// Note: any doc comments must be within the `with_fn!` call.
515macro_rules! with_fn {
516    {
517        $with_f:ident,
518        $(#[$attrs:meta])*
519        pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
520            $($body:tt)*
521        }
522    } => {
523        // The original function.
524        $(#[$attrs])*
525        #[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
526        pub fn $f(&mut $self, $($name: $ty),*) -> &mut Self {
527            $($body)*
528        }
529
530        // The `with_*` variant.
531        $(#[$attrs])*
532        #[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
533        pub fn $with_f(mut $self, $($name: $ty),*) -> Self {
534            $self.$f($($name),*);
535            $self
536        }
537    };
538}
539
540impl<'a, G: EmissionGuarantee> Diag<'a, G> {
541    #[rustc_lint_diagnostics]
542    #[track_caller]
543    pub fn new(dcx: DiagCtxtHandle<'a>, level: Level, message: impl Into<DiagMessage>) -> Self {
544        Self::new_diagnostic(dcx, DiagInner::new(level, message))
545    }
546
547    /// Allow moving diagnostics between different error tainting contexts
548    pub fn with_dcx(mut self, dcx: DiagCtxtHandle<'_>) -> Diag<'_, G> {
549        Diag { dcx, diag: self.diag.take(), _marker: PhantomData }
550    }
551
552    /// Creates a new `Diag` with an already constructed diagnostic.
553    #[track_caller]
554    pub(crate) fn new_diagnostic(dcx: DiagCtxtHandle<'a>, diag: DiagInner) -> Self {
555        debug!("Created new diagnostic");
556        Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
557    }
558
559    /// Delay emission of this diagnostic as a bug.
560    ///
561    /// This can be useful in contexts where an error indicates a bug but
562    /// typically this only happens when other compilation errors have already
563    /// happened. In those cases this can be used to defer emission of this
564    /// diagnostic as a bug in the compiler only if no other errors have been
565    /// emitted.
566    ///
567    /// In the meantime, though, callsites are required to deal with the "bug"
568    /// locally in whichever way makes the most sense.
569    #[rustc_lint_diagnostics]
570    #[track_caller]
571    pub fn downgrade_to_delayed_bug(&mut self) {
572        assert!(
573            matches!(self.level, Level::Error | Level::DelayedBug),
574            "downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
575            self.level
576        );
577        self.level = Level::DelayedBug;
578    }
579
580    /// Make emitting this diagnostic fatal
581    ///
582    /// Changes the level of this diagnostic to Fatal, and importantly also changes the emission guarantee.
583    /// This is sound for errors that would otherwise be printed, but now simply exit the process instead.
584    /// This function still gives an emission guarantee, the guarantee is now just that it exits fatally.
585    /// For delayed bugs this is different, since those are buffered. If we upgrade one to fatal, another
586    /// might now be ignored.
587    #[rustc_lint_diagnostics]
588    #[track_caller]
589    pub fn upgrade_to_fatal(mut self) -> Diag<'a, FatalAbort> {
590        assert!(
591            matches!(self.level, Level::Error),
592            "upgrade_to_fatal: cannot upgrade {:?} to Fatal: not an error",
593            self.level
594        );
595        self.level = Level::Fatal;
596
597        // Take is okay since we immediately rewrap it in another diagnostic.
598        // i.e. we do emit it despite defusing the original diagnostic's drop bomb.
599        let diag = self.diag.take();
600        Diag { dcx: self.dcx, diag, _marker: PhantomData }
601    }
602
603    with_fn! { with_span_label,
604    /// Appends a labeled span to the diagnostic.
605    ///
606    /// Labels are used to convey additional context for the diagnostic's primary span. They will
607    /// be shown together with the original diagnostic's span, *not* with spans added by
608    /// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
609    /// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
610    /// either.
611    ///
612    /// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
613    /// the diagnostic was constructed. However, the label span is *not* considered a
614    /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
615    /// primary.
616    #[rustc_lint_diagnostics]
617    pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagMessage>) -> &mut Self {
618        let msg = self.subdiagnostic_message_to_diagnostic_message(label);
619        self.span.push_span_label(span, msg);
620        self
621    } }
622
623    with_fn! { with_span_labels,
624    /// Labels all the given spans with the provided label.
625    /// See [`Self::span_label()`] for more information.
626    #[rustc_lint_diagnostics]
627    pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>, label: &str) -> &mut Self {
628        for span in spans {
629            self.span_label(span, label.to_string());
630        }
631        self
632    } }
633
634    #[rustc_lint_diagnostics]
635    pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
636        let before = self.span.clone();
637        self.span(after);
638        for span_label in before.span_labels() {
639            if let Some(label) = span_label.label {
640                if span_label.is_primary && keep_label {
641                    self.span.push_span_label(after, label);
642                } else {
643                    self.span.push_span_label(span_label.span, label);
644                }
645            }
646        }
647        self
648    }
649
650    #[rustc_lint_diagnostics]
651    pub fn note_expected_found(
652        &mut self,
653        expected_label: &str,
654        expected: DiagStyledString,
655        found_label: &str,
656        found: DiagStyledString,
657    ) -> &mut Self {
658        self.note_expected_found_extra(
659            expected_label,
660            expected,
661            found_label,
662            found,
663            DiagStyledString::normal(""),
664            DiagStyledString::normal(""),
665        )
666    }
667
668    #[rustc_lint_diagnostics]
669    pub fn note_expected_found_extra(
670        &mut self,
671        expected_label: &str,
672        expected: DiagStyledString,
673        found_label: &str,
674        found: DiagStyledString,
675        expected_extra: DiagStyledString,
676        found_extra: DiagStyledString,
677    ) -> &mut Self {
678        let expected_label = expected_label.to_string();
679        let expected_label = if expected_label.is_empty() {
680            "expected".to_string()
681        } else {
682            format!("expected {expected_label}")
683        };
684        let found_label = found_label.to_string();
685        let found_label = if found_label.is_empty() {
686            "found".to_string()
687        } else {
688            format!("found {found_label}")
689        };
690        let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
691            (expected_label.len() - found_label.len(), 0)
692        } else {
693            (0, found_label.len() - expected_label.len())
694        };
695        let mut msg = vec![StringPart::normal(format!(
696            "{}{} `",
697            " ".repeat(expected_padding),
698            expected_label
699        ))];
700        msg.extend(expected.0);
701        msg.push(StringPart::normal(format!("`")));
702        msg.extend(expected_extra.0);
703        msg.push(StringPart::normal(format!("\n")));
704        msg.push(StringPart::normal(format!("{}{} `", " ".repeat(found_padding), found_label)));
705        msg.extend(found.0);
706        msg.push(StringPart::normal(format!("`")));
707        msg.extend(found_extra.0);
708
709        // For now, just attach these as notes.
710        self.highlighted_note(msg);
711        self
712    }
713
714    #[rustc_lint_diagnostics]
715    pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
716        self.highlighted_note(vec![
717            StringPart::normal(format!("`{name}` from trait: `")),
718            StringPart::highlighted(signature),
719            StringPart::normal("`"),
720        ]);
721        self
722    }
723
724    with_fn! { with_note,
725    /// Add a note attached to this diagnostic.
726    #[rustc_lint_diagnostics]
727    pub fn note(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
728        self.sub(Level::Note, msg, MultiSpan::new());
729        self
730    } }
731
732    #[rustc_lint_diagnostics]
733    pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
734        self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
735        self
736    }
737
738    #[rustc_lint_diagnostics]
739    pub fn highlighted_span_note(
740        &mut self,
741        span: impl Into<MultiSpan>,
742        msg: Vec<StringPart>,
743    ) -> &mut Self {
744        self.sub_with_highlights(Level::Note, msg, span.into());
745        self
746    }
747
748    /// This is like [`Diag::note()`], but it's only printed once.
749    #[rustc_lint_diagnostics]
750    pub fn note_once(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
751        self.sub(Level::OnceNote, msg, MultiSpan::new());
752        self
753    }
754
755    with_fn! { with_span_note,
756    /// Prints the span with a note above it.
757    /// This is like [`Diag::note()`], but it gets its own span.
758    #[rustc_lint_diagnostics]
759    pub fn span_note(
760        &mut self,
761        sp: impl Into<MultiSpan>,
762        msg: impl Into<SubdiagMessage>,
763    ) -> &mut Self {
764        self.sub(Level::Note, msg, sp.into());
765        self
766    } }
767
768    /// Prints the span with a note above it.
769    /// This is like [`Diag::note_once()`], but it gets its own span.
770    #[rustc_lint_diagnostics]
771    pub fn span_note_once<S: Into<MultiSpan>>(
772        &mut self,
773        sp: S,
774        msg: impl Into<SubdiagMessage>,
775    ) -> &mut Self {
776        self.sub(Level::OnceNote, msg, sp.into());
777        self
778    }
779
780    with_fn! { with_warn,
781    /// Add a warning attached to this diagnostic.
782    #[rustc_lint_diagnostics]
783    pub fn warn(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
784        self.sub(Level::Warning, msg, MultiSpan::new());
785        self
786    } }
787
788    /// Prints the span with a warning above it.
789    /// This is like [`Diag::warn()`], but it gets its own span.
790    #[rustc_lint_diagnostics]
791    pub fn span_warn<S: Into<MultiSpan>>(
792        &mut self,
793        sp: S,
794        msg: impl Into<SubdiagMessage>,
795    ) -> &mut Self {
796        self.sub(Level::Warning, msg, sp.into());
797        self
798    }
799
800    with_fn! { with_help,
801    /// Add a help message attached to this diagnostic.
802    #[rustc_lint_diagnostics]
803    pub fn help(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
804        self.sub(Level::Help, msg, MultiSpan::new());
805        self
806    } }
807
808    /// This is like [`Diag::help()`], but it's only printed once.
809    #[rustc_lint_diagnostics]
810    pub fn help_once(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
811        self.sub(Level::OnceHelp, msg, MultiSpan::new());
812        self
813    }
814
815    /// Add a help message attached to this diagnostic with a customizable highlighted message.
816    #[rustc_lint_diagnostics]
817    pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
818        self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
819        self
820    }
821
822    /// Add a help message attached to this diagnostic with a customizable highlighted message.
823    #[rustc_lint_diagnostics]
824    pub fn highlighted_span_help(
825        &mut self,
826        span: impl Into<MultiSpan>,
827        msg: Vec<StringPart>,
828    ) -> &mut Self {
829        self.sub_with_highlights(Level::Help, msg, span.into());
830        self
831    }
832
833    with_fn! { with_span_help,
834    /// Prints the span with some help above it.
835    /// This is like [`Diag::help()`], but it gets its own span.
836    #[rustc_lint_diagnostics]
837    pub fn span_help(
838        &mut self,
839        sp: impl Into<MultiSpan>,
840        msg: impl Into<SubdiagMessage>,
841    ) -> &mut Self {
842        self.sub(Level::Help, msg, sp.into());
843        self
844    } }
845
846    /// Disallow attaching suggestions to this diagnostic.
847    /// Any suggestions attached e.g. with the `span_suggestion_*` methods
848    /// (before and after the call to `disable_suggestions`) will be ignored.
849    #[rustc_lint_diagnostics]
850    pub fn disable_suggestions(&mut self) -> &mut Self {
851        self.suggestions = Suggestions::Disabled;
852        self
853    }
854
855    /// Prevent new suggestions from being added to this diagnostic.
856    ///
857    /// Suggestions added before the call to `.seal_suggestions()` will be preserved
858    /// and new suggestions will be ignored.
859    #[rustc_lint_diagnostics]
860    pub fn seal_suggestions(&mut self) -> &mut Self {
861        if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
862            let suggestions_slice = std::mem::take(suggestions).into_boxed_slice();
863            self.suggestions = Suggestions::Sealed(suggestions_slice);
864        }
865        self
866    }
867
868    /// Helper for pushing to `self.suggestions`.
869    ///
870    /// A new suggestion is added if suggestions are enabled for this diagnostic.
871    /// Otherwise, they are ignored.
872    #[rustc_lint_diagnostics]
873    fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
874        for subst in &suggestion.substitutions {
875            for part in &subst.parts {
876                let span = part.span;
877                let call_site = span.ctxt().outer_expn_data().call_site;
878                if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
879                    // Ignore if spans is from derive macro.
880                    return;
881                }
882            }
883        }
884
885        if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
886            suggestions.push(suggestion);
887        }
888    }
889
890    with_fn! { with_multipart_suggestion,
891    /// Show a suggestion that has multiple parts to it.
892    /// In other words, multiple changes need to be applied as part of this suggestion.
893    #[rustc_lint_diagnostics]
894    pub fn multipart_suggestion(
895        &mut self,
896        msg: impl Into<SubdiagMessage>,
897        suggestion: Vec<(Span, String)>,
898        applicability: Applicability,
899    ) -> &mut Self {
900        self.multipart_suggestion_with_style(
901            msg,
902            suggestion,
903            applicability,
904            SuggestionStyle::ShowCode,
905        )
906    } }
907
908    /// Show a suggestion that has multiple parts to it, always as its own subdiagnostic.
909    /// In other words, multiple changes need to be applied as part of this suggestion.
910    #[rustc_lint_diagnostics]
911    pub fn multipart_suggestion_verbose(
912        &mut self,
913        msg: impl Into<SubdiagMessage>,
914        suggestion: Vec<(Span, String)>,
915        applicability: Applicability,
916    ) -> &mut Self {
917        self.multipart_suggestion_with_style(
918            msg,
919            suggestion,
920            applicability,
921            SuggestionStyle::ShowAlways,
922        )
923    }
924
925    /// [`Diag::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
926    #[rustc_lint_diagnostics]
927    pub fn multipart_suggestion_with_style(
928        &mut self,
929        msg: impl Into<SubdiagMessage>,
930        mut suggestion: Vec<(Span, String)>,
931        applicability: Applicability,
932        style: SuggestionStyle,
933    ) -> &mut Self {
934        let mut seen = crate::FxHashSet::default();
935        suggestion.retain(|(span, msg)| seen.insert((span.lo(), span.hi(), msg.clone())));
936
937        let parts = suggestion
938            .into_iter()
939            .map(|(span, snippet)| SubstitutionPart { snippet, span })
940            .collect::<Vec<_>>();
941
942        assert!(!parts.is_empty());
943        debug_assert_eq!(
944            parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
945            None,
946            "Span must not be empty and have no suggestion",
947        );
948        debug_assert_eq!(
949            parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
950            None,
951            "suggestion must not have overlapping parts",
952        );
953
954        self.push_suggestion(CodeSuggestion {
955            substitutions: vec![Substitution { parts }],
956            msg: self.subdiagnostic_message_to_diagnostic_message(msg),
957            style,
958            applicability,
959        });
960        self
961    }
962
963    /// Prints out a message with for a multipart suggestion without showing the suggested code.
964    ///
965    /// This is intended to be used for suggestions that are obvious in what the changes need to
966    /// be from the message, showing the span label inline would be visually unpleasant
967    /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
968    /// improve understandability.
969    #[rustc_lint_diagnostics]
970    pub fn tool_only_multipart_suggestion(
971        &mut self,
972        msg: impl Into<SubdiagMessage>,
973        suggestion: Vec<(Span, String)>,
974        applicability: Applicability,
975    ) -> &mut Self {
976        self.multipart_suggestion_with_style(
977            msg,
978            suggestion,
979            applicability,
980            SuggestionStyle::CompletelyHidden,
981        )
982    }
983
984    with_fn! { with_span_suggestion,
985    /// Prints out a message with a suggested edit of the code.
986    ///
987    /// In case of short messages and a simple suggestion, rustc displays it as a label:
988    ///
989    /// ```text
990    /// try adding parentheses: `(tup.0).1`
991    /// ```
992    ///
993    /// The message
994    ///
995    /// * should not end in any punctuation (a `:` is added automatically)
996    /// * should not be a question (avoid language like "did you mean")
997    /// * should not contain any phrases like "the following", "as shown", etc.
998    /// * may look like "to do xyz, use" or "to do xyz, use abc"
999    /// * may contain a name of a function, variable, or type, but not whole expressions
1000    ///
1001    /// See [`CodeSuggestion`] for more information.
1002    #[rustc_lint_diagnostics]
1003    pub fn span_suggestion(
1004        &mut self,
1005        sp: Span,
1006        msg: impl Into<SubdiagMessage>,
1007        suggestion: impl ToString,
1008        applicability: Applicability,
1009    ) -> &mut Self {
1010        self.span_suggestion_with_style(
1011            sp,
1012            msg,
1013            suggestion,
1014            applicability,
1015            SuggestionStyle::ShowCode,
1016        );
1017        self
1018    } }
1019
1020    /// [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`].
1021    #[rustc_lint_diagnostics]
1022    pub fn span_suggestion_with_style(
1023        &mut self,
1024        sp: Span,
1025        msg: impl Into<SubdiagMessage>,
1026        suggestion: impl ToString,
1027        applicability: Applicability,
1028        style: SuggestionStyle,
1029    ) -> &mut Self {
1030        debug_assert!(
1031            !(sp.is_empty() && suggestion.to_string().is_empty()),
1032            "Span must not be empty and have no suggestion"
1033        );
1034        self.push_suggestion(CodeSuggestion {
1035            substitutions: vec![Substitution {
1036                parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
1037            }],
1038            msg: self.subdiagnostic_message_to_diagnostic_message(msg),
1039            style,
1040            applicability,
1041        });
1042        self
1043    }
1044
1045    with_fn! { with_span_suggestion_verbose,
1046    /// Always show the suggested change.
1047    #[rustc_lint_diagnostics]
1048    pub fn span_suggestion_verbose(
1049        &mut self,
1050        sp: Span,
1051        msg: impl Into<SubdiagMessage>,
1052        suggestion: impl ToString,
1053        applicability: Applicability,
1054    ) -> &mut Self {
1055        self.span_suggestion_with_style(
1056            sp,
1057            msg,
1058            suggestion,
1059            applicability,
1060            SuggestionStyle::ShowAlways,
1061        );
1062        self
1063    } }
1064
1065    with_fn! { with_span_suggestions,
1066    /// Prints out a message with multiple suggested edits of the code.
1067    /// See also [`Diag::span_suggestion()`].
1068    #[rustc_lint_diagnostics]
1069    pub fn span_suggestions(
1070        &mut self,
1071        sp: Span,
1072        msg: impl Into<SubdiagMessage>,
1073        suggestions: impl IntoIterator<Item = String>,
1074        applicability: Applicability,
1075    ) -> &mut Self {
1076        self.span_suggestions_with_style(
1077            sp,
1078            msg,
1079            suggestions,
1080            applicability,
1081            SuggestionStyle::ShowCode,
1082        )
1083    } }
1084
1085    #[rustc_lint_diagnostics]
1086    pub fn span_suggestions_with_style(
1087        &mut self,
1088        sp: Span,
1089        msg: impl Into<SubdiagMessage>,
1090        suggestions: impl IntoIterator<Item = String>,
1091        applicability: Applicability,
1092        style: SuggestionStyle,
1093    ) -> &mut Self {
1094        let substitutions = suggestions
1095            .into_iter()
1096            .map(|snippet| {
1097                debug_assert!(
1098                    !(sp.is_empty() && snippet.is_empty()),
1099                    "Span `{sp:?}` must not be empty and have no suggestion"
1100                );
1101                Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
1102            })
1103            .collect();
1104        self.push_suggestion(CodeSuggestion {
1105            substitutions,
1106            msg: self.subdiagnostic_message_to_diagnostic_message(msg),
1107            style,
1108            applicability,
1109        });
1110        self
1111    }
1112
1113    /// Prints out a message with multiple suggested edits of the code, where each edit consists of
1114    /// multiple parts.
1115    /// See also [`Diag::multipart_suggestion()`].
1116    #[rustc_lint_diagnostics]
1117    pub fn multipart_suggestions(
1118        &mut self,
1119        msg: impl Into<SubdiagMessage>,
1120        suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
1121        applicability: Applicability,
1122    ) -> &mut Self {
1123        let substitutions = suggestions
1124            .into_iter()
1125            .map(|sugg| {
1126                let mut parts = sugg
1127                    .into_iter()
1128                    .map(|(span, snippet)| SubstitutionPart { snippet, span })
1129                    .collect::<Vec<_>>();
1130
1131                parts.sort_unstable_by_key(|part| part.span);
1132
1133                assert!(!parts.is_empty());
1134                debug_assert_eq!(
1135                    parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
1136                    None,
1137                    "Span must not be empty and have no suggestion",
1138                );
1139                debug_assert_eq!(
1140                    parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
1141                    None,
1142                    "suggestion must not have overlapping parts",
1143                );
1144
1145                Substitution { parts }
1146            })
1147            .collect();
1148
1149        self.push_suggestion(CodeSuggestion {
1150            substitutions,
1151            msg: self.subdiagnostic_message_to_diagnostic_message(msg),
1152            style: SuggestionStyle::ShowAlways,
1153            applicability,
1154        });
1155        self
1156    }
1157
1158    with_fn! { with_span_suggestion_short,
1159    /// Prints out a message with a suggested edit of the code. If the suggestion is presented
1160    /// inline, it will only show the message and not the suggestion.
1161    ///
1162    /// See [`CodeSuggestion`] for more information.
1163    #[rustc_lint_diagnostics]
1164    pub fn span_suggestion_short(
1165        &mut self,
1166        sp: Span,
1167        msg: impl Into<SubdiagMessage>,
1168        suggestion: impl ToString,
1169        applicability: Applicability,
1170    ) -> &mut Self {
1171        self.span_suggestion_with_style(
1172            sp,
1173            msg,
1174            suggestion,
1175            applicability,
1176            SuggestionStyle::HideCodeInline,
1177        );
1178        self
1179    } }
1180
1181    /// Prints out a message for a suggestion without showing the suggested code.
1182    ///
1183    /// This is intended to be used for suggestions that are obvious in what the changes need to
1184    /// be from the message, showing the span label inline would be visually unpleasant
1185    /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
1186    /// improve understandability.
1187    #[rustc_lint_diagnostics]
1188    pub fn span_suggestion_hidden(
1189        &mut self,
1190        sp: Span,
1191        msg: impl Into<SubdiagMessage>,
1192        suggestion: impl ToString,
1193        applicability: Applicability,
1194    ) -> &mut Self {
1195        self.span_suggestion_with_style(
1196            sp,
1197            msg,
1198            suggestion,
1199            applicability,
1200            SuggestionStyle::HideCodeAlways,
1201        );
1202        self
1203    }
1204
1205    with_fn! { with_tool_only_span_suggestion,
1206    /// Adds a suggestion to the JSON output that will not be shown in the CLI.
1207    ///
1208    /// This is intended to be used for suggestions that are *very* obvious in what the changes
1209    /// need to be from the message, but we still want other tools to be able to apply them.
1210    #[rustc_lint_diagnostics]
1211    pub fn tool_only_span_suggestion(
1212        &mut self,
1213        sp: Span,
1214        msg: impl Into<SubdiagMessage>,
1215        suggestion: impl ToString,
1216        applicability: Applicability,
1217    ) -> &mut Self {
1218        self.span_suggestion_with_style(
1219            sp,
1220            msg,
1221            suggestion,
1222            applicability,
1223            SuggestionStyle::CompletelyHidden,
1224        );
1225        self
1226    } }
1227
1228    /// Add a subdiagnostic from a type that implements `Subdiagnostic` (see
1229    /// [rustc_macros::Subdiagnostic]). Performs eager translation of any translatable messages
1230    /// used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of
1231    /// interpolated variables).
1232    #[rustc_lint_diagnostics]
1233    pub fn subdiagnostic(&mut self, subdiagnostic: impl Subdiagnostic) -> &mut Self {
1234        subdiagnostic.add_to_diag(self);
1235        self
1236    }
1237
1238    /// Fluent variables are not namespaced from each other, so when
1239    /// `Diagnostic`s and `Subdiagnostic`s use the same variable name,
1240    /// one value will clobber the other. Eagerly translating the
1241    /// diagnostic uses the variables defined right then, before the
1242    /// clobbering occurs.
1243    pub fn eagerly_translate(&self, msg: impl Into<SubdiagMessage>) -> SubdiagMessage {
1244        let args = self.args.iter();
1245        let msg = self.subdiagnostic_message_to_diagnostic_message(msg.into());
1246        self.dcx.eagerly_translate(msg, args)
1247    }
1248
1249    with_fn! { with_span,
1250    /// Add a span.
1251    #[rustc_lint_diagnostics]
1252    pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
1253        self.span = sp.into();
1254        if let Some(span) = self.span.primary_span() {
1255            self.sort_span = span;
1256        }
1257        self
1258    } }
1259
1260    #[rustc_lint_diagnostics]
1261    pub fn is_lint(&mut self, name: String, has_future_breakage: bool) -> &mut Self {
1262        self.is_lint = Some(IsLint { name, has_future_breakage });
1263        self
1264    }
1265
1266    with_fn! { with_code,
1267    /// Add an error code.
1268    #[rustc_lint_diagnostics]
1269    pub fn code(&mut self, code: ErrCode) -> &mut Self {
1270        self.code = Some(code);
1271        self
1272    } }
1273
1274    with_fn! { with_lint_id,
1275    /// Add an argument.
1276    #[rustc_lint_diagnostics]
1277    pub fn lint_id(
1278        &mut self,
1279        id: LintExpectationId,
1280    ) -> &mut Self {
1281        self.lint_id = Some(id);
1282        self
1283    } }
1284
1285    with_fn! { with_primary_message,
1286    /// Add a primary message.
1287    #[rustc_lint_diagnostics]
1288    pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
1289        self.messages[0] = (msg.into(), Style::NoStyle);
1290        self
1291    } }
1292
1293    with_fn! { with_arg,
1294    /// Add an argument.
1295    #[rustc_lint_diagnostics]
1296    pub fn arg(
1297        &mut self,
1298        name: impl Into<DiagArgName>,
1299        arg: impl IntoDiagArg,
1300    ) -> &mut Self {
1301        self.deref_mut().arg(name, arg);
1302        self
1303    } }
1304
1305    /// Helper function that takes a `SubdiagMessage` and returns a `DiagMessage` by
1306    /// combining it with the primary message of the diagnostic (if translatable, otherwise it just
1307    /// passes the user's string along).
1308    pub(crate) fn subdiagnostic_message_to_diagnostic_message(
1309        &self,
1310        attr: impl Into<SubdiagMessage>,
1311    ) -> DiagMessage {
1312        self.deref().subdiagnostic_message_to_diagnostic_message(attr)
1313    }
1314
1315    /// Convenience function for internal use, clients should use one of the
1316    /// public methods above.
1317    ///
1318    /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
1319    pub fn sub(&mut self, level: Level, message: impl Into<SubdiagMessage>, span: MultiSpan) {
1320        self.deref_mut().sub(level, message, span);
1321    }
1322
1323    /// Convenience function for internal use, clients should use one of the
1324    /// public methods above.
1325    fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
1326        let messages = messages
1327            .into_iter()
1328            .map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.content), m.style))
1329            .collect();
1330        let sub = Subdiag { level, messages, span };
1331        self.children.push(sub);
1332    }
1333
1334    /// Takes the diagnostic. For use by methods that consume the Diag: `emit`,
1335    /// `cancel`, etc. Afterwards, `drop` is the only code that will be run on
1336    /// `self`.
1337    fn take_diag(&mut self) -> DiagInner {
1338        if let Some(path) = &self.long_ty_path {
1339            self.note(format!(
1340                "the full name for the type has been written to '{}'",
1341                path.display()
1342            ));
1343            self.note("consider using `--verbose` to print the full type name to the console");
1344        }
1345        *self.diag.take().unwrap()
1346    }
1347
1348    /// This method allows us to access the path of the file where "long types" are written to.
1349    ///
1350    /// When calling `Diag::emit`, as part of that we will check if a `long_ty_path` has been set,
1351    /// and if it has been then we add a note mentioning the file where the "long types" were
1352    /// written to.
1353    ///
1354    /// When calling `tcx.short_string()` after a `Diag` is constructed, the preferred way of doing
1355    /// so is `tcx.short_string(ty, diag.long_ty_path())`. The diagnostic itself is the one that
1356    /// keeps the existence of a "long type" anywhere in the diagnostic, so the note telling the
1357    /// user where we wrote the file to is only printed once at most, *and* it makes it much harder
1358    /// to forget to set it.
1359    ///
1360    /// If the diagnostic hasn't been created before a "short ty string" is created, then you should
1361    /// ensure that this method is called to set it `*diag.long_ty_path() = path`.
1362    ///
1363    /// As a rule of thumb, if you see or add at least one `tcx.short_string()` call anywhere, in a
1364    /// scope, `diag.long_ty_path()` should be called once somewhere close by.
1365    pub fn long_ty_path(&mut self) -> &mut Option<PathBuf> {
1366        &mut self.long_ty_path
1367    }
1368
1369    pub fn with_long_ty_path(mut self, long_ty_path: Option<PathBuf>) -> Self {
1370        self.long_ty_path = long_ty_path;
1371        self
1372    }
1373
1374    /// Most `emit_producing_guarantee` functions use this as a starting point.
1375    fn emit_producing_nothing(mut self) {
1376        let diag = self.take_diag();
1377        self.dcx.emit_diagnostic(diag);
1378    }
1379
1380    /// `ErrorGuaranteed::emit_producing_guarantee` uses this.
1381    fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
1382        let diag = self.take_diag();
1383
1384        // The only error levels that produce `ErrorGuaranteed` are
1385        // `Error` and `DelayedBug`. But `DelayedBug` should never occur here
1386        // because delayed bugs have their level changed to `Bug` when they are
1387        // actually printed, so they produce an ICE.
1388        //
1389        // (Also, even though `level` isn't `pub`, the whole `DiagInner` could
1390        // be overwritten with a new one thanks to `DerefMut`. So this assert
1391        // protects against that, too.)
1392        assert!(
1393            matches!(diag.level, Level::Error | Level::DelayedBug),
1394            "invalid diagnostic level ({:?})",
1395            diag.level,
1396        );
1397
1398        let guar = self.dcx.emit_diagnostic(diag);
1399        guar.unwrap()
1400    }
1401
1402    /// Emit and consume the diagnostic.
1403    #[track_caller]
1404    pub fn emit(self) -> G::EmitResult {
1405        G::emit_producing_guarantee(self)
1406    }
1407
1408    /// Emit the diagnostic unless `delay` is true,
1409    /// in which case the emission will be delayed as a bug.
1410    ///
1411    /// See `emit` and `delay_as_bug` for details.
1412    #[track_caller]
1413    pub fn emit_unless_delay(mut self, delay: bool) -> G::EmitResult {
1414        if delay {
1415            self.downgrade_to_delayed_bug();
1416        }
1417        self.emit()
1418    }
1419
1420    /// Cancel and consume the diagnostic. (A diagnostic must either be emitted or
1421    /// cancelled or it will panic when dropped).
1422    pub fn cancel(mut self) {
1423        self.diag = None;
1424        drop(self);
1425    }
1426
1427    /// See `DiagCtxt::stash_diagnostic` for details.
1428    pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
1429        let diag = self.take_diag();
1430        self.dcx.stash_diagnostic(span, key, diag)
1431    }
1432
1433    /// Delay emission of this diagnostic as a bug.
1434    ///
1435    /// This can be useful in contexts where an error indicates a bug but
1436    /// typically this only happens when other compilation errors have already
1437    /// happened. In those cases this can be used to defer emission of this
1438    /// diagnostic as a bug in the compiler only if no other errors have been
1439    /// emitted.
1440    ///
1441    /// In the meantime, though, callsites are required to deal with the "bug"
1442    /// locally in whichever way makes the most sense.
1443    #[track_caller]
1444    pub fn delay_as_bug(mut self) -> G::EmitResult {
1445        self.downgrade_to_delayed_bug();
1446        self.emit()
1447    }
1448
1449    pub fn remove_arg(&mut self, name: &str) {
1450        if let Some(diag) = self.diag.as_mut() {
1451            diag.remove_arg(name);
1452        }
1453    }
1454}
1455
1456/// Destructor bomb: every `Diag` must be consumed (emitted, cancelled, etc.)
1457/// or we emit a bug.
1458impl<G: EmissionGuarantee> Drop for Diag<'_, G> {
1459    fn drop(&mut self) {
1460        match self.diag.take() {
1461            Some(diag) if !panicking() => {
1462                self.dcx.emit_diagnostic(DiagInner::new(
1463                    Level::Bug,
1464                    DiagMessage::from("the following error was constructed but not emitted"),
1465                ));
1466                self.dcx.emit_diagnostic(*diag);
1467                panic!("error was constructed but not emitted");
1468            }
1469            _ => {}
1470        }
1471    }
1472}
1473
1474#[macro_export]
1475macro_rules! struct_span_code_err {
1476    ($dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
1477        $dcx.struct_span_err($span, format!($($message)*)).with_code($code)
1478    })
1479}