rustc_errors/
lib.rs

1//! Diagnostics creation and emission for `rustc`.
2//!
3//! This module contains the code for creating and emitting diagnostics.
4
5// tidy-alphabetical-start
6#![allow(incomplete_features)]
7#![allow(internal_features)]
8#![allow(rustc::diagnostic_outside_of_impl)]
9#![allow(rustc::untranslatable_diagnostic)]
10#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
11#![doc(rust_logo)]
12#![feature(array_windows)]
13#![feature(assert_matches)]
14#![feature(associated_type_defaults)]
15#![feature(box_into_inner)]
16#![feature(box_patterns)]
17#![feature(error_reporter)]
18#![feature(extract_if)]
19#![feature(if_let_guard)]
20#![feature(let_chains)]
21#![feature(negative_impls)]
22#![feature(never_type)]
23#![feature(rustc_attrs)]
24#![feature(rustdoc_internals)]
25#![feature(trait_alias)]
26#![feature(try_blocks)]
27#![feature(yeet_expr)]
28#![warn(unreachable_pub)]
29// tidy-alphabetical-end
30
31extern crate self as rustc_errors;
32
33use std::assert_matches::assert_matches;
34use std::backtrace::{Backtrace, BacktraceStatus};
35use std::borrow::Cow;
36use std::cell::Cell;
37use std::error::Report;
38use std::ffi::OsStr;
39use std::hash::Hash;
40use std::io::Write;
41use std::num::NonZero;
42use std::ops::DerefMut;
43use std::path::{Path, PathBuf};
44use std::{fmt, panic};
45
46use Level::*;
47pub use codes::*;
48pub use diagnostic::{
49    BugAbort, Diag, DiagArg, DiagArgMap, DiagArgName, DiagArgValue, DiagInner, DiagStyledString,
50    Diagnostic, EmissionGuarantee, FatalAbort, IntoDiagArg, LintDiagnostic, StringPart, Subdiag,
51    SubdiagMessageOp, Subdiagnostic,
52};
53pub use diagnostic_impls::{
54    DiagArgFromDisplay, DiagSymbolList, ElidedLifetimeInPathSubdiag, ExpectedLifetimeParameter,
55    IndicateAnonymousLifetime, SingleLabelManySpans,
56};
57pub use emitter::ColorConfig;
58use emitter::{DynEmitter, Emitter, is_case_difference, is_different};
59use rustc_data_structures::AtomicRef;
60use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
61use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
62use rustc_data_structures::sync::{DynSend, Lock};
63pub use rustc_error_messages::{
64    DiagMessage, FluentBundle, LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel,
65    SubdiagMessage, fallback_fluent_bundle, fluent_bundle,
66};
67use rustc_lint_defs::LintExpectationId;
68pub use rustc_lint_defs::{Applicability, listify, pluralize};
69use rustc_macros::{Decodable, Encodable};
70pub use rustc_span::ErrorGuaranteed;
71pub use rustc_span::fatal_error::{FatalError, FatalErrorMarker};
72use rustc_span::source_map::SourceMap;
73use rustc_span::{DUMMY_SP, Loc, Span};
74pub use snippet::Style;
75// Used by external projects such as `rust-gpu`.
76// See https://github.com/rust-lang/rust/pull/115393.
77pub use termcolor::{Color, ColorSpec, WriteColor};
78use tracing::debug;
79
80use crate::registry::Registry;
81
82pub mod annotate_snippet_emitter_writer;
83pub mod codes;
84mod diagnostic;
85mod diagnostic_impls;
86pub mod emitter;
87pub mod error;
88pub mod json;
89mod lock;
90pub mod markdown;
91pub mod registry;
92mod snippet;
93mod styled_buffer;
94#[cfg(test)]
95mod tests;
96pub mod translation;
97
98pub type PResult<'a, T> = Result<T, Diag<'a>>;
99
100rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
101
102// `PResult` is used a lot. Make sure it doesn't unintentionally get bigger.
103#[cfg(target_pointer_width = "64")]
104rustc_data_structures::static_assert_size!(PResult<'_, ()>, 24);
105#[cfg(target_pointer_width = "64")]
106rustc_data_structures::static_assert_size!(PResult<'_, bool>, 24);
107
108#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Encodable, Decodable)]
109pub enum SuggestionStyle {
110    /// Hide the suggested code when displaying this suggestion inline.
111    HideCodeInline,
112    /// Always hide the suggested code but display the message.
113    HideCodeAlways,
114    /// Do not display this suggestion in the cli output, it is only meant for tools.
115    CompletelyHidden,
116    /// Always show the suggested code.
117    /// This will *not* show the code if the suggestion is inline *and* the suggested code is
118    /// empty.
119    ShowCode,
120    /// Always show the suggested code independently.
121    ShowAlways,
122}
123
124impl SuggestionStyle {
125    fn hide_inline(&self) -> bool {
126        !matches!(*self, SuggestionStyle::ShowCode)
127    }
128}
129
130/// Represents the help messages seen on a diagnostic.
131#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
132pub enum Suggestions {
133    /// Indicates that new suggestions can be added or removed from this diagnostic.
134    ///
135    /// `DiagInner`'s new_* methods initialize the `suggestions` field with
136    /// this variant. Also, this is the default variant for `Suggestions`.
137    Enabled(Vec<CodeSuggestion>),
138    /// Indicates that suggestions cannot be added or removed from this diagnostic.
139    ///
140    /// Gets toggled when `.seal_suggestions()` is called on the `DiagInner`.
141    Sealed(Box<[CodeSuggestion]>),
142    /// Indicates that no suggestion is available for this diagnostic.
143    ///
144    /// Gets toggled when `.disable_suggestions()` is called on the `DiagInner`.
145    Disabled,
146}
147
148impl Suggestions {
149    /// Returns the underlying list of suggestions.
150    pub fn unwrap_tag(self) -> Vec<CodeSuggestion> {
151        match self {
152            Suggestions::Enabled(suggestions) => suggestions,
153            Suggestions::Sealed(suggestions) => suggestions.into_vec(),
154            Suggestions::Disabled => Vec::new(),
155        }
156    }
157}
158
159impl Default for Suggestions {
160    fn default() -> Self {
161        Self::Enabled(vec![])
162    }
163}
164
165#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
166pub struct CodeSuggestion {
167    /// Each substitute can have multiple variants due to multiple
168    /// applicable suggestions
169    ///
170    /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
171    /// `foo` and `bar` on their own:
172    ///
173    /// ```ignore (illustrative)
174    /// vec![
175    ///     Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
176    ///     Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
177    /// ]
178    /// ```
179    ///
180    /// or by replacing the entire span:
181    ///
182    /// ```ignore (illustrative)
183    /// vec![
184    ///     Substitution { parts: vec![(0..7, "a.b")] },
185    ///     Substitution { parts: vec![(0..7, "x.y")] },
186    /// ]
187    /// ```
188    pub substitutions: Vec<Substitution>,
189    pub msg: DiagMessage,
190    /// Visual representation of this suggestion.
191    pub style: SuggestionStyle,
192    /// Whether or not the suggestion is approximate
193    ///
194    /// Sometimes we may show suggestions with placeholders,
195    /// which are useful for users but not useful for
196    /// tools like rustfix
197    pub applicability: Applicability,
198}
199
200#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
201/// See the docs on `CodeSuggestion::substitutions`
202pub struct Substitution {
203    pub parts: Vec<SubstitutionPart>,
204}
205
206#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
207pub struct SubstitutionPart {
208    pub span: Span,
209    pub snippet: String,
210}
211
212/// Used to translate between `Span`s and byte positions within a single output line in highlighted
213/// code of structured suggestions.
214#[derive(Debug, Clone, Copy)]
215pub(crate) struct SubstitutionHighlight {
216    start: usize,
217    end: usize,
218}
219
220impl SubstitutionPart {
221    pub fn is_addition(&self, sm: &SourceMap) -> bool {
222        !self.snippet.is_empty() && !self.replaces_meaningful_content(sm)
223    }
224
225    pub fn is_deletion(&self, sm: &SourceMap) -> bool {
226        self.snippet.trim().is_empty() && self.replaces_meaningful_content(sm)
227    }
228
229    pub fn is_replacement(&self, sm: &SourceMap) -> bool {
230        !self.snippet.is_empty() && self.replaces_meaningful_content(sm)
231    }
232
233    /// Whether this is a replacement that overwrites source with a snippet
234    /// in a way that isn't a superset of the original string. For example,
235    /// replacing "abc" with "abcde" is not destructive, but replacing it
236    /// it with "abx" is, since the "c" character is lost.
237    pub fn is_destructive_replacement(&self, sm: &SourceMap) -> bool {
238        self.is_replacement(sm)
239            && !sm.span_to_snippet(self.span).is_ok_and(|snippet| {
240                self.snippet.trim_start().starts_with(snippet.trim_start())
241                    || self.snippet.trim_end().ends_with(snippet.trim_end())
242            })
243    }
244
245    fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool {
246        sm.span_to_snippet(self.span)
247            .map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
248    }
249
250    /// Try to turn a replacement into an addition when the span that is being
251    /// overwritten matches either the prefix or suffix of the replacement.
252    fn trim_trivial_replacements(&mut self, sm: &SourceMap) {
253        if self.snippet.is_empty() {
254            return;
255        }
256        let Ok(snippet) = sm.span_to_snippet(self.span) else {
257            return;
258        };
259        if self.snippet.starts_with(&snippet) {
260            self.span = self.span.shrink_to_hi();
261            self.snippet = self.snippet[snippet.len()..].to_string();
262        } else if self.snippet.ends_with(&snippet) {
263            self.span = self.span.shrink_to_lo();
264            self.snippet = self.snippet[..self.snippet.len() - snippet.len()].to_string();
265        }
266    }
267}
268
269impl CodeSuggestion {
270    /// Returns the assembled code suggestions, whether they should be shown with an underline
271    /// and whether the substitution only differs in capitalization.
272    pub(crate) fn splice_lines(
273        &self,
274        sm: &SourceMap,
275    ) -> Vec<(String, Vec<SubstitutionPart>, Vec<Vec<SubstitutionHighlight>>, bool)> {
276        // For the `Vec<Vec<SubstitutionHighlight>>` value, the first level of the vector
277        // corresponds to the output snippet's lines, while the second level corresponds to the
278        // substrings within that line that should be highlighted.
279
280        use rustc_span::{CharPos, Pos};
281
282        /// Extracts a substring from the provided `line_opt` based on the specified low and high
283        /// indices, appends it to the given buffer `buf`, and returns the count of newline
284        /// characters in the substring for accurate highlighting. If `line_opt` is `None`, a
285        /// newline character is appended to the buffer, and 0 is returned.
286        ///
287        /// ## Returns
288        ///
289        /// The count of newline characters in the extracted substring.
290        fn push_trailing(
291            buf: &mut String,
292            line_opt: Option<&Cow<'_, str>>,
293            lo: &Loc,
294            hi_opt: Option<&Loc>,
295        ) -> usize {
296            let mut line_count = 0;
297            // Convert CharPos to Usize, as CharPose is character offset
298            // Extract low index and high index
299            let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
300            if let Some(line) = line_opt {
301                if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
302                    // Get high index while account for rare unicode and emoji with char_indices
303                    let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
304                    match hi_opt {
305                        // If high index exist, take string from low to high index
306                        Some(hi) if hi > lo => {
307                            // count how many '\n' exist
308                            line_count = line[lo..hi].matches('\n').count();
309                            buf.push_str(&line[lo..hi])
310                        }
311                        Some(_) => (),
312                        // If high index absence, take string from low index till end string.len
313                        None => {
314                            // count how many '\n' exist
315                            line_count = line[lo..].matches('\n').count();
316                            buf.push_str(&line[lo..])
317                        }
318                    }
319                }
320                // If high index is None
321                if hi_opt.is_none() {
322                    buf.push('\n');
323                }
324            }
325            line_count
326        }
327
328        assert!(!self.substitutions.is_empty());
329
330        self.substitutions
331            .iter()
332            .filter(|subst| {
333                // Suggestions coming from macros can have malformed spans. This is a heavy
334                // handed approach to avoid ICEs by ignoring the suggestion outright.
335                let invalid = subst.parts.iter().any(|item| sm.is_valid_span(item.span).is_err());
336                if invalid {
337                    debug!("splice_lines: suggestion contains an invalid span: {:?}", subst);
338                }
339                !invalid
340            })
341            .cloned()
342            .filter_map(|mut substitution| {
343                // Assumption: all spans are in the same file, and all spans
344                // are disjoint. Sort in ascending order.
345                substitution.parts.sort_by_key(|part| part.span.lo());
346
347                // Find the bounding span.
348                let lo = substitution.parts.iter().map(|part| part.span.lo()).min()?;
349                let hi = substitution.parts.iter().map(|part| part.span.hi()).max()?;
350                let bounding_span = Span::with_root_ctxt(lo, hi);
351                // The different spans might belong to different contexts, if so ignore suggestion.
352                let lines = sm.span_to_lines(bounding_span).ok()?;
353                assert!(!lines.lines.is_empty() || bounding_span.is_dummy());
354
355                // We can't splice anything if the source is unavailable.
356                if !sm.ensure_source_file_source_present(&lines.file) {
357                    return None;
358                }
359
360                let mut highlights = vec![];
361                // To build up the result, we do this for each span:
362                // - push the line segment trailing the previous span
363                //   (at the beginning a "phantom" span pointing at the start of the line)
364                // - push lines between the previous and current span (if any)
365                // - if the previous and current span are not on the same line
366                //   push the line segment leading up to the current span
367                // - splice in the span substitution
368                //
369                // Finally push the trailing line segment of the last span
370                let sf = &lines.file;
371                let mut prev_hi = sm.lookup_char_pos(bounding_span.lo());
372                prev_hi.col = CharPos::from_usize(0);
373                let mut prev_line =
374                    lines.lines.get(0).and_then(|line0| sf.get_line(line0.line_index));
375                let mut buf = String::new();
376
377                let mut line_highlight = vec![];
378                // We need to keep track of the difference between the existing code and the added
379                // or deleted code in order to point at the correct column *after* substitution.
380                let mut acc = 0;
381                let mut only_capitalization = false;
382                for part in &substitution.parts {
383                    only_capitalization |= is_case_difference(sm, &part.snippet, part.span);
384                    let cur_lo = sm.lookup_char_pos(part.span.lo());
385                    if prev_hi.line == cur_lo.line {
386                        let mut count =
387                            push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
388                        while count > 0 {
389                            highlights.push(std::mem::take(&mut line_highlight));
390                            acc = 0;
391                            count -= 1;
392                        }
393                    } else {
394                        acc = 0;
395                        highlights.push(std::mem::take(&mut line_highlight));
396                        let mut count = push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
397                        while count > 0 {
398                            highlights.push(std::mem::take(&mut line_highlight));
399                            count -= 1;
400                        }
401                        // push lines between the previous and current span (if any)
402                        for idx in prev_hi.line..(cur_lo.line - 1) {
403                            if let Some(line) = sf.get_line(idx) {
404                                buf.push_str(line.as_ref());
405                                buf.push('\n');
406                                highlights.push(std::mem::take(&mut line_highlight));
407                            }
408                        }
409                        if let Some(cur_line) = sf.get_line(cur_lo.line - 1) {
410                            let end = match cur_line.char_indices().nth(cur_lo.col.to_usize()) {
411                                Some((i, _)) => i,
412                                None => cur_line.len(),
413                            };
414                            buf.push_str(&cur_line[..end]);
415                        }
416                    }
417                    // Add a whole line highlight per line in the snippet.
418                    let len: isize = part
419                        .snippet
420                        .split('\n')
421                        .next()
422                        .unwrap_or(&part.snippet)
423                        .chars()
424                        .map(|c| match c {
425                            '\t' => 4,
426                            _ => 1,
427                        })
428                        .sum();
429                    if !is_different(sm, &part.snippet, part.span) {
430                        // Account for cases where we are suggesting the same code that's already
431                        // there. This shouldn't happen often, but in some cases for multipart
432                        // suggestions it's much easier to handle it here than in the origin.
433                    } else {
434                        line_highlight.push(SubstitutionHighlight {
435                            start: (cur_lo.col.0 as isize + acc) as usize,
436                            end: (cur_lo.col.0 as isize + acc + len) as usize,
437                        });
438                    }
439                    buf.push_str(&part.snippet);
440                    let cur_hi = sm.lookup_char_pos(part.span.hi());
441                    // Account for the difference between the width of the current code and the
442                    // snippet being suggested, so that the *later* suggestions are correctly
443                    // aligned on the screen. Note that cur_hi and cur_lo can be on different
444                    // lines, so cur_hi.col can be smaller than cur_lo.col
445                    acc += len - (cur_hi.col.0 as isize - cur_lo.col.0 as isize);
446                    prev_hi = cur_hi;
447                    prev_line = sf.get_line(prev_hi.line - 1);
448                    for line in part.snippet.split('\n').skip(1) {
449                        acc = 0;
450                        highlights.push(std::mem::take(&mut line_highlight));
451                        let end: usize = line
452                            .chars()
453                            .map(|c| match c {
454                                '\t' => 4,
455                                _ => 1,
456                            })
457                            .sum();
458                        line_highlight.push(SubstitutionHighlight { start: 0, end });
459                    }
460                }
461                highlights.push(std::mem::take(&mut line_highlight));
462                // if the replacement already ends with a newline, don't print the next line
463                if !buf.ends_with('\n') {
464                    push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
465                }
466                // remove trailing newlines
467                while buf.ends_with('\n') {
468                    buf.pop();
469                }
470                if highlights.iter().all(|parts| parts.is_empty()) {
471                    None
472                } else {
473                    Some((buf, substitution.parts, highlights, only_capitalization))
474                }
475            })
476            .collect()
477    }
478}
479
480/// Signifies that the compiler died with an explicit call to `.bug`
481/// or `.span_bug` rather than a failed assertion, etc.
482pub struct ExplicitBug;
483
484/// Signifies that the compiler died due to a delayed bug rather than a failed
485/// assertion, etc.
486pub struct DelayedBugPanic;
487
488/// A `DiagCtxt` deals with errors and other compiler output.
489/// Certain errors (fatal, bug, unimpl) may cause immediate exit,
490/// others log errors for later reporting.
491pub struct DiagCtxt {
492    inner: Lock<DiagCtxtInner>,
493}
494
495#[derive(Copy, Clone)]
496pub struct DiagCtxtHandle<'a> {
497    dcx: &'a DiagCtxt,
498    /// Some contexts create `DiagCtxtHandle` with this field set, and thus all
499    /// errors emitted with it will automatically taint when emitting errors.
500    tainted_with_errors: Option<&'a Cell<Option<ErrorGuaranteed>>>,
501}
502
503impl<'a> std::ops::Deref for DiagCtxtHandle<'a> {
504    type Target = &'a DiagCtxt;
505
506    fn deref(&self) -> &Self::Target {
507        &self.dcx
508    }
509}
510
511/// This inner struct exists to keep it all behind a single lock;
512/// this is done to prevent possible deadlocks in a multi-threaded compiler,
513/// as well as inconsistent state observation.
514struct DiagCtxtInner {
515    flags: DiagCtxtFlags,
516
517    registry: Registry,
518
519    /// The error guarantees from all emitted errors. The length gives the error count.
520    err_guars: Vec<ErrorGuaranteed>,
521    /// The error guarantee from all emitted lint errors. The length gives the
522    /// lint error count.
523    lint_err_guars: Vec<ErrorGuaranteed>,
524    /// The delayed bugs and their error guarantees.
525    delayed_bugs: Vec<(DelayedDiagInner, ErrorGuaranteed)>,
526
527    /// The error count shown to the user at the end.
528    deduplicated_err_count: usize,
529    /// The warning count shown to the user at the end.
530    deduplicated_warn_count: usize,
531
532    emitter: Box<DynEmitter>,
533
534    /// Must we produce a diagnostic to justify the use of the expensive
535    /// `trimmed_def_paths` function? Backtrace is the location of the call.
536    must_produce_diag: Option<Backtrace>,
537
538    /// Has this diagnostic context printed any diagnostics? (I.e. has
539    /// `self.emitter.emit_diagnostic()` been called?
540    has_printed: bool,
541
542    /// This flag indicates that an expected diagnostic was emitted and suppressed.
543    /// This is used for the `must_produce_diag` check.
544    suppressed_expected_diag: bool,
545
546    /// This set contains the code of all emitted diagnostics to avoid
547    /// emitting the same diagnostic with extended help (`--teach`) twice, which
548    /// would be unnecessary repetition.
549    taught_diagnostics: FxHashSet<ErrCode>,
550
551    /// Used to suggest rustc --explain `<error code>`
552    emitted_diagnostic_codes: FxIndexSet<ErrCode>,
553
554    /// This set contains a hash of every diagnostic that has been emitted by
555    /// this `DiagCtxt`. These hashes is used to avoid emitting the same error
556    /// twice.
557    emitted_diagnostics: FxHashSet<Hash128>,
558
559    /// Stashed diagnostics emitted in one stage of the compiler that may be
560    /// stolen and emitted/cancelled by other stages (e.g. to improve them and
561    /// add more information). All stashed diagnostics must be emitted with
562    /// `emit_stashed_diagnostics` by the time the `DiagCtxtInner` is dropped,
563    /// otherwise an assertion failure will occur.
564    stashed_diagnostics: FxIndexMap<(Span, StashKey), (DiagInner, Option<ErrorGuaranteed>)>,
565
566    future_breakage_diagnostics: Vec<DiagInner>,
567
568    /// expected diagnostic will have the level `Expect` which additionally
569    /// carries the [`LintExpectationId`] of the expectation that can be
570    /// marked as fulfilled. This is a collection of all [`LintExpectationId`]s
571    /// that have been marked as fulfilled this way.
572    ///
573    /// Emitting expectations after having stolen this field can happen. In particular, an
574    /// `#[expect(warnings)]` can easily make the `UNFULFILLED_LINT_EXPECTATIONS` lint expect
575    /// itself. To avoid needless complexity in this corner case, we tolerate failing to track
576    /// those expectations.
577    ///
578    /// [RFC-2383]: https://rust-lang.github.io/rfcs/2383-lint-reasons.html
579    fulfilled_expectations: FxIndexSet<LintExpectationId>,
580
581    /// The file where the ICE information is stored. This allows delayed_span_bug backtraces to be
582    /// stored along side the main panic backtrace.
583    ice_file: Option<PathBuf>,
584}
585
586/// A key denoting where from a diagnostic was stashed.
587#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
588pub enum StashKey {
589    ItemNoType,
590    UnderscoreForArrayLengths,
591    EarlySyntaxWarning,
592    CallIntoMethod,
593    /// When an invalid lifetime e.g. `'2` should be reinterpreted
594    /// as a char literal in the parser
595    LifetimeIsChar,
596    /// Maybe there was a typo where a comma was forgotten before
597    /// FRU syntax
598    MaybeFruTypo,
599    CallAssocMethod,
600    AssociatedTypeSuggestion,
601    MaybeForgetReturn,
602    /// Query cycle detected, stashing in favor of a better error.
603    Cycle,
604    UndeterminedMacroResolution,
605    /// Used by `Parser::maybe_recover_trailing_expr`
606    ExprInPat,
607    /// If in the parser we detect a field expr with turbofish generic params it's possible that
608    /// it's a method call without parens. If later on in `hir_typeck` we find out that this is
609    /// the case we suppress this message and we give a better suggestion.
610    GenericInFieldExpr,
611}
612
613fn default_track_diagnostic<R>(diag: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
614    (*f)(diag)
615}
616
617/// Diagnostics emitted by `DiagCtxtInner::emit_diagnostic` are passed through this function. Used
618/// for tracking by incremental, to replay diagnostics as necessary.
619pub static TRACK_DIAGNOSTIC: AtomicRef<
620    fn(DiagInner, &mut dyn FnMut(DiagInner) -> Option<ErrorGuaranteed>) -> Option<ErrorGuaranteed>,
621> = AtomicRef::new(&(default_track_diagnostic as _));
622
623#[derive(Copy, Clone, Default)]
624pub struct DiagCtxtFlags {
625    /// If false, warning-level lints are suppressed.
626    /// (rustc: see `--allow warnings` and `--cap-lints`)
627    pub can_emit_warnings: bool,
628    /// If Some, the Nth error-level diagnostic is upgraded to bug-level.
629    /// (rustc: see `-Z treat-err-as-bug`)
630    pub treat_err_as_bug: Option<NonZero<usize>>,
631    /// Eagerly emit delayed bugs as errors, so that the compiler debugger may
632    /// see all of the errors being emitted at once.
633    pub eagerly_emit_delayed_bugs: bool,
634    /// Show macro backtraces.
635    /// (rustc: see `-Z macro-backtrace`)
636    pub macro_backtrace: bool,
637    /// If true, identical diagnostics are reported only once.
638    pub deduplicate_diagnostics: bool,
639    /// Track where errors are created. Enabled with `-Ztrack-diagnostics`.
640    pub track_diagnostics: bool,
641}
642
643impl Drop for DiagCtxtInner {
644    fn drop(&mut self) {
645        // For tools using `interface::run_compiler` (e.g. rustc, rustdoc)
646        // stashed diagnostics will have already been emitted. But for others
647        // that don't use `interface::run_compiler` (e.g. rustfmt, some clippy
648        // lints) this fallback is necessary.
649        //
650        // Important: it is sound to produce an `ErrorGuaranteed` when stashing
651        // errors because they are guaranteed to be emitted here or earlier.
652        self.emit_stashed_diagnostics();
653
654        // Important: it is sound to produce an `ErrorGuaranteed` when emitting
655        // delayed bugs because they are guaranteed to be emitted here if
656        // necessary.
657        self.flush_delayed();
658
659        // Sanity check: did we use some of the expensive `trimmed_def_paths` functions
660        // unexpectedly, that is, without producing diagnostics? If so, for debugging purposes, we
661        // suggest where this happened and how to avoid it.
662        if !self.has_printed && !self.suppressed_expected_diag && !std::thread::panicking() {
663            if let Some(backtrace) = &self.must_produce_diag {
664                let suggestion = match backtrace.status() {
665                    BacktraceStatus::Disabled => String::from(
666                        "Backtraces are currently disabled: set `RUST_BACKTRACE=1` and re-run \
667                        to see where it happened.",
668                    ),
669                    BacktraceStatus::Captured => format!(
670                        "This happened in the following `must_produce_diag` call's backtrace:\n\
671                        {backtrace}",
672                    ),
673                    _ => String::from("(impossible to capture backtrace where this happened)"),
674                };
675                panic!(
676                    "`trimmed_def_paths` called, diagnostics were expected but none were emitted. \
677                    Use `with_no_trimmed_paths` for debugging. {suggestion}"
678                );
679            }
680        }
681    }
682}
683
684impl DiagCtxt {
685    pub fn disable_warnings(mut self) -> Self {
686        self.inner.get_mut().flags.can_emit_warnings = false;
687        self
688    }
689
690    pub fn with_flags(mut self, flags: DiagCtxtFlags) -> Self {
691        self.inner.get_mut().flags = flags;
692        self
693    }
694
695    pub fn with_ice_file(mut self, ice_file: PathBuf) -> Self {
696        self.inner.get_mut().ice_file = Some(ice_file);
697        self
698    }
699
700    pub fn with_registry(mut self, registry: Registry) -> Self {
701        self.inner.get_mut().registry = registry;
702        self
703    }
704
705    pub fn new(emitter: Box<DynEmitter>) -> Self {
706        Self { inner: Lock::new(DiagCtxtInner::new(emitter)) }
707    }
708
709    pub fn make_silent(&self, fatal_note: Option<String>, emit_fatal_diagnostic: bool) {
710        // An empty type that implements `Emitter` to temporarily swap in place of the real one,
711        // which will be used in constructing its replacement.
712        struct FalseEmitter;
713
714        impl Emitter for FalseEmitter {
715            fn emit_diagnostic(&mut self, _: DiagInner, _: &Registry) {
716                unimplemented!("false emitter must only used during `make_silent`")
717            }
718
719            fn source_map(&self) -> Option<&SourceMap> {
720                unimplemented!("false emitter must only used during `make_silent`")
721            }
722        }
723
724        impl translation::Translate for FalseEmitter {
725            fn fluent_bundle(&self) -> Option<&FluentBundle> {
726                unimplemented!("false emitter must only used during `make_silent`")
727            }
728
729            fn fallback_fluent_bundle(&self) -> &FluentBundle {
730                unimplemented!("false emitter must only used during `make_silent`")
731            }
732        }
733
734        let mut inner = self.inner.borrow_mut();
735        let mut prev_emitter = Box::new(FalseEmitter) as Box<dyn Emitter + DynSend>;
736        std::mem::swap(&mut inner.emitter, &mut prev_emitter);
737        let new_emitter = Box::new(emitter::SilentEmitter {
738            fatal_emitter: prev_emitter,
739            fatal_note,
740            emit_fatal_diagnostic,
741        });
742        inner.emitter = new_emitter;
743    }
744
745    pub fn set_emitter(&self, emitter: Box<dyn Emitter + DynSend>) {
746        self.inner.borrow_mut().emitter = emitter;
747    }
748
749    /// Translate `message` eagerly with `args` to `SubdiagMessage::Eager`.
750    pub fn eagerly_translate<'a>(
751        &self,
752        message: DiagMessage,
753        args: impl Iterator<Item = DiagArg<'a>>,
754    ) -> SubdiagMessage {
755        let inner = self.inner.borrow();
756        inner.eagerly_translate(message, args)
757    }
758
759    /// Translate `message` eagerly with `args` to `String`.
760    pub fn eagerly_translate_to_string<'a>(
761        &self,
762        message: DiagMessage,
763        args: impl Iterator<Item = DiagArg<'a>>,
764    ) -> String {
765        let inner = self.inner.borrow();
766        inner.eagerly_translate_to_string(message, args)
767    }
768
769    // This is here to not allow mutation of flags;
770    // as of this writing it's used in Session::consider_optimizing and
771    // in tests in rustc_interface.
772    pub fn can_emit_warnings(&self) -> bool {
773        self.inner.borrow_mut().flags.can_emit_warnings
774    }
775
776    /// Resets the diagnostic error count as well as the cached emitted diagnostics.
777    ///
778    /// NOTE: *do not* call this function from rustc. It is only meant to be called from external
779    /// tools that want to reuse a `Parser` cleaning the previously emitted diagnostics as well as
780    /// the overall count of emitted error diagnostics.
781    pub fn reset_err_count(&self) {
782        // Use destructuring so that if a field gets added to `DiagCtxtInner`, it's impossible to
783        // fail to update this method as well.
784        let mut inner = self.inner.borrow_mut();
785        let DiagCtxtInner {
786            flags: _,
787            registry: _,
788            err_guars,
789            lint_err_guars,
790            delayed_bugs,
791            deduplicated_err_count,
792            deduplicated_warn_count,
793            emitter: _,
794            must_produce_diag,
795            has_printed,
796            suppressed_expected_diag,
797            taught_diagnostics,
798            emitted_diagnostic_codes,
799            emitted_diagnostics,
800            stashed_diagnostics,
801            future_breakage_diagnostics,
802            fulfilled_expectations,
803            ice_file: _,
804        } = inner.deref_mut();
805
806        // For the `Vec`s and `HashMap`s, we overwrite with an empty container to free the
807        // underlying memory (which `clear` would not do).
808        *err_guars = Default::default();
809        *lint_err_guars = Default::default();
810        *delayed_bugs = Default::default();
811        *deduplicated_err_count = 0;
812        *deduplicated_warn_count = 0;
813        *must_produce_diag = None;
814        *has_printed = false;
815        *suppressed_expected_diag = false;
816        *taught_diagnostics = Default::default();
817        *emitted_diagnostic_codes = Default::default();
818        *emitted_diagnostics = Default::default();
819        *stashed_diagnostics = Default::default();
820        *future_breakage_diagnostics = Default::default();
821        *fulfilled_expectations = Default::default();
822    }
823
824    pub fn handle<'a>(&'a self) -> DiagCtxtHandle<'a> {
825        DiagCtxtHandle { dcx: self, tainted_with_errors: None }
826    }
827
828    /// Link this to a taintable context so that emitting errors will automatically set
829    /// the `Option<ErrorGuaranteed>` instead of having to do that manually at every error
830    /// emission site.
831    pub fn taintable_handle<'a>(
832        &'a self,
833        tainted_with_errors: &'a Cell<Option<ErrorGuaranteed>>,
834    ) -> DiagCtxtHandle<'a> {
835        DiagCtxtHandle { dcx: self, tainted_with_errors: Some(tainted_with_errors) }
836    }
837}
838
839impl<'a> DiagCtxtHandle<'a> {
840    /// Stashes a diagnostic for possible later improvement in a different,
841    /// later stage of the compiler. Possible actions depend on the diagnostic
842    /// level:
843    /// - Level::Bug, Level:Fatal: not allowed, will trigger a panic.
844    /// - Level::Error: immediately counted as an error that has occurred, because it
845    ///   is guaranteed to be emitted eventually. Can be later accessed with the
846    ///   provided `span` and `key` through
847    ///   [`DiagCtxtHandle::try_steal_modify_and_emit_err`] or
848    ///   [`DiagCtxtHandle::try_steal_replace_and_emit_err`]. These do not allow
849    ///   cancellation or downgrading of the error. Returns
850    ///   `Some(ErrorGuaranteed)`.
851    /// - Level::DelayedBug: this does happen occasionally with errors that are
852    ///   downgraded to delayed bugs. It is not stashed, but immediately
853    ///   emitted as a delayed bug. This is because stashing it would cause it
854    ///   to be counted by `err_count` which we don't want. It doesn't matter
855    ///   that we cannot steal and improve it later, because it's not a
856    ///   user-facing error. Returns `Some(ErrorGuaranteed)` as is normal for
857    ///   delayed bugs.
858    /// - Level::Warning and lower (i.e. !is_error()): can be accessed with the
859    ///   provided `span` and `key` through [`DiagCtxtHandle::steal_non_err()`]. This
860    ///   allows cancelling and downgrading of the diagnostic. Returns `None`.
861    pub fn stash_diagnostic(
862        &self,
863        span: Span,
864        key: StashKey,
865        diag: DiagInner,
866    ) -> Option<ErrorGuaranteed> {
867        let guar = match diag.level {
868            Bug | Fatal => {
869                self.span_bug(
870                    span,
871                    format!("invalid level in `stash_diagnostic`: {:?}", diag.level),
872                );
873            }
874            // We delay a bug here so that `-Ztreat-err-as-bug -Zeagerly-emit-delayed-bugs`
875            // can be used to create a backtrace at the stashing site instead of whenever the
876            // diagnostic context is dropped and thus delayed bugs are emitted.
877            Error => Some(self.span_delayed_bug(span, format!("stashing {key:?}"))),
878            DelayedBug => {
879                return self.inner.borrow_mut().emit_diagnostic(diag, self.tainted_with_errors);
880            }
881            ForceWarning(_) | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
882            | Expect(_) => None,
883        };
884
885        // FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
886        // if/when we have a more robust macro-friendly replacement for `(span, key)` as a key.
887        // See the PR for a discussion.
888        let key = (span.with_parent(None), key);
889        self.inner.borrow_mut().stashed_diagnostics.insert(key, (diag, guar));
890
891        guar
892    }
893
894    /// Steal a previously stashed non-error diagnostic with the given `Span`
895    /// and [`StashKey`] as the key. Panics if the found diagnostic is an
896    /// error.
897    pub fn steal_non_err(self, span: Span, key: StashKey) -> Option<Diag<'a, ()>> {
898        let key = (span.with_parent(None), key);
899        // FIXME(#120456) - is `swap_remove` correct?
900        let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key)?;
901        assert!(!diag.is_error());
902        assert!(guar.is_none());
903        Some(Diag::new_diagnostic(self, diag))
904    }
905
906    /// Steals a previously stashed error with the given `Span` and
907    /// [`StashKey`] as the key, modifies it, and emits it. Returns `None` if
908    /// no matching diagnostic is found. Panics if the found diagnostic's level
909    /// isn't `Level::Error`.
910    pub fn try_steal_modify_and_emit_err<F>(
911        self,
912        span: Span,
913        key: StashKey,
914        mut modify_err: F,
915    ) -> Option<ErrorGuaranteed>
916    where
917        F: FnMut(&mut Diag<'_>),
918    {
919        let key = (span.with_parent(None), key);
920        // FIXME(#120456) - is `swap_remove` correct?
921        let err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
922        err.map(|(err, guar)| {
923            // The use of `::<ErrorGuaranteed>` is safe because level is `Level::Error`.
924            assert_eq!(err.level, Error);
925            assert!(guar.is_some());
926            let mut err = Diag::<ErrorGuaranteed>::new_diagnostic(self, err);
927            modify_err(&mut err);
928            assert_eq!(err.level, Error);
929            err.emit()
930        })
931    }
932
933    /// Steals a previously stashed error with the given `Span` and
934    /// [`StashKey`] as the key, cancels it if found, and emits `new_err`.
935    /// Panics if the found diagnostic's level isn't `Level::Error`.
936    pub fn try_steal_replace_and_emit_err(
937        self,
938        span: Span,
939        key: StashKey,
940        new_err: Diag<'_>,
941    ) -> ErrorGuaranteed {
942        let key = (span.with_parent(None), key);
943        // FIXME(#120456) - is `swap_remove` correct?
944        let old_err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
945        match old_err {
946            Some((old_err, guar)) => {
947                assert_eq!(old_err.level, Error);
948                assert!(guar.is_some());
949                // Because `old_err` has already been counted, it can only be
950                // safely cancelled because the `new_err` supplants it.
951                Diag::<ErrorGuaranteed>::new_diagnostic(self, old_err).cancel();
952            }
953            None => {}
954        };
955        new_err.emit()
956    }
957
958    pub fn has_stashed_diagnostic(&self, span: Span, key: StashKey) -> bool {
959        self.inner.borrow().stashed_diagnostics.get(&(span.with_parent(None), key)).is_some()
960    }
961
962    /// Emit all stashed diagnostics.
963    pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
964        self.inner.borrow_mut().emit_stashed_diagnostics()
965    }
966
967    /// This excludes delayed bugs.
968    #[inline]
969    pub fn err_count(&self) -> usize {
970        let inner = self.inner.borrow();
971        inner.err_guars.len()
972            + inner.lint_err_guars.len()
973            + inner.stashed_diagnostics.values().filter(|(_diag, guar)| guar.is_some()).count()
974    }
975
976    /// This excludes lint errors and delayed bugs. Unless absolutely
977    /// necessary, prefer `has_errors` to this method.
978    pub fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
979        self.inner.borrow().has_errors_excluding_lint_errors()
980    }
981
982    /// This excludes delayed bugs.
983    pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
984        self.inner.borrow().has_errors()
985    }
986
987    /// This excludes nothing. Unless absolutely necessary, prefer `has_errors`
988    /// to this method.
989    pub fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
990        self.inner.borrow().has_errors_or_delayed_bugs()
991    }
992
993    pub fn print_error_count(&self) {
994        let mut inner = self.inner.borrow_mut();
995
996        // Any stashed diagnostics should have been handled by
997        // `emit_stashed_diagnostics` by now.
998        assert!(inner.stashed_diagnostics.is_empty());
999
1000        if inner.treat_err_as_bug() {
1001            return;
1002        }
1003
1004        let warnings = match inner.deduplicated_warn_count {
1005            0 => Cow::from(""),
1006            1 => Cow::from("1 warning emitted"),
1007            count => Cow::from(format!("{count} warnings emitted")),
1008        };
1009        let errors = match inner.deduplicated_err_count {
1010            0 => Cow::from(""),
1011            1 => Cow::from("aborting due to 1 previous error"),
1012            count => Cow::from(format!("aborting due to {count} previous errors")),
1013        };
1014
1015        match (errors.len(), warnings.len()) {
1016            (0, 0) => return,
1017            (0, _) => {
1018                // Use `ForceWarning` rather than `Warning` to guarantee emission, e.g. with a
1019                // configuration like `--cap-lints allow --force-warn bare_trait_objects`.
1020                inner.emit_diagnostic(
1021                    DiagInner::new(ForceWarning(None), DiagMessage::Str(warnings)),
1022                    None,
1023                );
1024            }
1025            (_, 0) => {
1026                inner.emit_diagnostic(DiagInner::new(Error, errors), self.tainted_with_errors);
1027            }
1028            (_, _) => {
1029                inner.emit_diagnostic(
1030                    DiagInner::new(Error, format!("{errors}; {warnings}")),
1031                    self.tainted_with_errors,
1032                );
1033            }
1034        }
1035
1036        let can_show_explain = inner.emitter.should_show_explain();
1037        let are_there_diagnostics = !inner.emitted_diagnostic_codes.is_empty();
1038        if can_show_explain && are_there_diagnostics {
1039            let mut error_codes = inner
1040                .emitted_diagnostic_codes
1041                .iter()
1042                .filter_map(|&code| {
1043                    if inner.registry.try_find_description(code).is_ok() {
1044                        Some(code.to_string())
1045                    } else {
1046                        None
1047                    }
1048                })
1049                .collect::<Vec<_>>();
1050            if !error_codes.is_empty() {
1051                error_codes.sort();
1052                if error_codes.len() > 1 {
1053                    let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
1054                    let msg1 = format!(
1055                        "Some errors have detailed explanations: {}{}",
1056                        error_codes[..limit].join(", "),
1057                        if error_codes.len() > 9 { "..." } else { "." }
1058                    );
1059                    let msg2 = format!(
1060                        "For more information about an error, try `rustc --explain {}`.",
1061                        &error_codes[0]
1062                    );
1063                    inner.emit_diagnostic(DiagInner::new(FailureNote, msg1), None);
1064                    inner.emit_diagnostic(DiagInner::new(FailureNote, msg2), None);
1065                } else {
1066                    let msg = format!(
1067                        "For more information about this error, try `rustc --explain {}`.",
1068                        &error_codes[0]
1069                    );
1070                    inner.emit_diagnostic(DiagInner::new(FailureNote, msg), None);
1071                }
1072            }
1073        }
1074    }
1075
1076    /// This excludes delayed bugs. Used for early aborts after errors occurred
1077    /// -- e.g. because continuing in the face of errors is likely to lead to
1078    /// bad results, such as spurious/uninteresting additional errors -- when
1079    /// returning an error `Result` is difficult.
1080    pub fn abort_if_errors(&self) {
1081        if let Some(guar) = self.has_errors() {
1082            guar.raise_fatal();
1083        }
1084    }
1085
1086    /// `true` if we haven't taught a diagnostic with this code already.
1087    /// The caller must then teach the user about such a diagnostic.
1088    ///
1089    /// Used to suppress emitting the same error multiple times with extended explanation when
1090    /// calling `-Zteach`.
1091    pub fn must_teach(&self, code: ErrCode) -> bool {
1092        self.inner.borrow_mut().taught_diagnostics.insert(code)
1093    }
1094
1095    pub fn emit_diagnostic(&self, diagnostic: DiagInner) -> Option<ErrorGuaranteed> {
1096        self.inner.borrow_mut().emit_diagnostic(diagnostic, self.tainted_with_errors)
1097    }
1098
1099    pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
1100        self.inner.borrow_mut().emitter.emit_artifact_notification(path, artifact_type);
1101    }
1102
1103    pub fn emit_future_breakage_report(&self) {
1104        let inner = &mut *self.inner.borrow_mut();
1105        let diags = std::mem::take(&mut inner.future_breakage_diagnostics);
1106        if !diags.is_empty() {
1107            inner.emitter.emit_future_breakage_report(diags, &inner.registry);
1108        }
1109    }
1110
1111    pub fn emit_unused_externs(
1112        &self,
1113        lint_level: rustc_lint_defs::Level,
1114        loud: bool,
1115        unused_externs: &[&str],
1116    ) {
1117        let mut inner = self.inner.borrow_mut();
1118
1119        // This "error" is an odd duck.
1120        // - It's only produce with JSON output.
1121        // - It's not emitted the usual way, via `emit_diagnostic`.
1122        // - The `$message_type` field is "unused_externs" rather than the usual
1123        //   "diagnosic".
1124        //
1125        // We count it as a lint error because it has a lint level. The value
1126        // of `loud` (which comes from "unused-externs" or
1127        // "unused-externs-silent"), also affects whether it's treated like a
1128        // hard error or not.
1129        if loud && lint_level.is_error() {
1130            // This `unchecked_error_guaranteed` is valid. It is where the
1131            // `ErrorGuaranteed` for unused_extern errors originates.
1132            #[allow(deprecated)]
1133            inner.lint_err_guars.push(ErrorGuaranteed::unchecked_error_guaranteed());
1134            inner.panic_if_treat_err_as_bug();
1135        }
1136
1137        inner.emitter.emit_unused_externs(lint_level, unused_externs)
1138    }
1139
1140    /// This methods steals all [`LintExpectationId`]s that are stored inside
1141    /// [`DiagCtxtInner`] and indicate that the linked expectation has been fulfilled.
1142    #[must_use]
1143    pub fn steal_fulfilled_expectation_ids(&self) -> FxIndexSet<LintExpectationId> {
1144        std::mem::take(&mut self.inner.borrow_mut().fulfilled_expectations)
1145    }
1146
1147    pub fn flush_delayed(&self) {
1148        self.inner.borrow_mut().flush_delayed();
1149    }
1150
1151    /// Used when trimmed_def_paths is called and we must produce a diagnostic
1152    /// to justify its cost.
1153    #[track_caller]
1154    pub fn set_must_produce_diag(&self) {
1155        assert!(
1156            self.inner.borrow().must_produce_diag.is_none(),
1157            "should only need to collect a backtrace once"
1158        );
1159        self.inner.borrow_mut().must_produce_diag = Some(Backtrace::capture());
1160    }
1161}
1162
1163// This `impl` block contains only the public diagnostic creation/emission API.
1164//
1165// Functions beginning with `struct_`/`create_` create a diagnostic. Other
1166// functions create and emit a diagnostic all in one go.
1167impl<'a> DiagCtxtHandle<'a> {
1168    // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
1169    // user-facing.
1170    #[track_caller]
1171    pub fn struct_bug(self, msg: impl Into<Cow<'static, str>>) -> Diag<'a, BugAbort> {
1172        Diag::new(self, Bug, msg.into())
1173    }
1174
1175    // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
1176    // user-facing.
1177    #[track_caller]
1178    pub fn bug(self, msg: impl Into<Cow<'static, str>>) -> ! {
1179        self.struct_bug(msg).emit()
1180    }
1181
1182    // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
1183    // user-facing.
1184    #[track_caller]
1185    pub fn struct_span_bug(
1186        self,
1187        span: impl Into<MultiSpan>,
1188        msg: impl Into<Cow<'static, str>>,
1189    ) -> Diag<'a, BugAbort> {
1190        self.struct_bug(msg).with_span(span)
1191    }
1192
1193    // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
1194    // user-facing.
1195    #[track_caller]
1196    pub fn span_bug(self, span: impl Into<MultiSpan>, msg: impl Into<Cow<'static, str>>) -> ! {
1197        self.struct_span_bug(span, msg.into()).emit()
1198    }
1199
1200    #[track_caller]
1201    pub fn create_bug(self, bug: impl Diagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> {
1202        bug.into_diag(self, Bug)
1203    }
1204
1205    #[track_caller]
1206    pub fn emit_bug(self, bug: impl Diagnostic<'a, BugAbort>) -> ! {
1207        self.create_bug(bug).emit()
1208    }
1209
1210    #[rustc_lint_diagnostics]
1211    #[track_caller]
1212    pub fn struct_fatal(self, msg: impl Into<DiagMessage>) -> Diag<'a, FatalAbort> {
1213        Diag::new(self, Fatal, msg)
1214    }
1215
1216    #[rustc_lint_diagnostics]
1217    #[track_caller]
1218    pub fn fatal(self, msg: impl Into<DiagMessage>) -> ! {
1219        self.struct_fatal(msg).emit()
1220    }
1221
1222    #[rustc_lint_diagnostics]
1223    #[track_caller]
1224    pub fn struct_span_fatal(
1225        self,
1226        span: impl Into<MultiSpan>,
1227        msg: impl Into<DiagMessage>,
1228    ) -> Diag<'a, FatalAbort> {
1229        self.struct_fatal(msg).with_span(span)
1230    }
1231
1232    #[rustc_lint_diagnostics]
1233    #[track_caller]
1234    pub fn span_fatal(self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) -> ! {
1235        self.struct_span_fatal(span, msg).emit()
1236    }
1237
1238    #[track_caller]
1239    pub fn create_fatal(self, fatal: impl Diagnostic<'a, FatalAbort>) -> Diag<'a, FatalAbort> {
1240        fatal.into_diag(self, Fatal)
1241    }
1242
1243    #[track_caller]
1244    pub fn emit_fatal(self, fatal: impl Diagnostic<'a, FatalAbort>) -> ! {
1245        self.create_fatal(fatal).emit()
1246    }
1247
1248    #[track_caller]
1249    pub fn create_almost_fatal(
1250        self,
1251        fatal: impl Diagnostic<'a, FatalError>,
1252    ) -> Diag<'a, FatalError> {
1253        fatal.into_diag(self, Fatal)
1254    }
1255
1256    #[track_caller]
1257    pub fn emit_almost_fatal(self, fatal: impl Diagnostic<'a, FatalError>) -> FatalError {
1258        self.create_almost_fatal(fatal).emit()
1259    }
1260
1261    // FIXME: This method should be removed (every error should have an associated error code).
1262    #[rustc_lint_diagnostics]
1263    #[track_caller]
1264    pub fn struct_err(self, msg: impl Into<DiagMessage>) -> Diag<'a> {
1265        Diag::new(self, Error, msg)
1266    }
1267
1268    #[rustc_lint_diagnostics]
1269    #[track_caller]
1270    pub fn err(self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed {
1271        self.struct_err(msg).emit()
1272    }
1273
1274    #[rustc_lint_diagnostics]
1275    #[track_caller]
1276    pub fn struct_span_err(
1277        self,
1278        span: impl Into<MultiSpan>,
1279        msg: impl Into<DiagMessage>,
1280    ) -> Diag<'a> {
1281        self.struct_err(msg).with_span(span)
1282    }
1283
1284    #[rustc_lint_diagnostics]
1285    #[track_caller]
1286    pub fn span_err(
1287        self,
1288        span: impl Into<MultiSpan>,
1289        msg: impl Into<DiagMessage>,
1290    ) -> ErrorGuaranteed {
1291        self.struct_span_err(span, msg).emit()
1292    }
1293
1294    #[track_caller]
1295    pub fn create_err(self, err: impl Diagnostic<'a>) -> Diag<'a> {
1296        err.into_diag(self, Error)
1297    }
1298
1299    #[track_caller]
1300    pub fn emit_err(self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
1301        self.create_err(err).emit()
1302    }
1303
1304    /// Ensures that an error is printed. See `Level::DelayedBug`.
1305    //
1306    // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
1307    // user-facing.
1308    #[track_caller]
1309    pub fn delayed_bug(self, msg: impl Into<Cow<'static, str>>) -> ErrorGuaranteed {
1310        Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg.into()).emit()
1311    }
1312
1313    /// Ensures that an error is printed. See [`Level::DelayedBug`].
1314    ///
1315    /// Note: this function used to be called `delay_span_bug`. It was renamed
1316    /// to match similar functions like `span_err`, `span_warn`, etc.
1317    //
1318    // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
1319    // user-facing.
1320    #[track_caller]
1321    pub fn span_delayed_bug(
1322        self,
1323        sp: impl Into<MultiSpan>,
1324        msg: impl Into<Cow<'static, str>>,
1325    ) -> ErrorGuaranteed {
1326        Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg.into()).with_span(sp).emit()
1327    }
1328
1329    #[rustc_lint_diagnostics]
1330    #[track_caller]
1331    pub fn struct_warn(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
1332        Diag::new(self, Warning, msg)
1333    }
1334
1335    #[rustc_lint_diagnostics]
1336    #[track_caller]
1337    pub fn warn(self, msg: impl Into<DiagMessage>) {
1338        self.struct_warn(msg).emit()
1339    }
1340
1341    #[rustc_lint_diagnostics]
1342    #[track_caller]
1343    pub fn struct_span_warn(
1344        self,
1345        span: impl Into<MultiSpan>,
1346        msg: impl Into<DiagMessage>,
1347    ) -> Diag<'a, ()> {
1348        self.struct_warn(msg).with_span(span)
1349    }
1350
1351    #[rustc_lint_diagnostics]
1352    #[track_caller]
1353    pub fn span_warn(self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
1354        self.struct_span_warn(span, msg).emit()
1355    }
1356
1357    #[track_caller]
1358    pub fn create_warn(self, warning: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
1359        warning.into_diag(self, Warning)
1360    }
1361
1362    #[track_caller]
1363    pub fn emit_warn(self, warning: impl Diagnostic<'a, ()>) {
1364        self.create_warn(warning).emit()
1365    }
1366
1367    #[rustc_lint_diagnostics]
1368    #[track_caller]
1369    pub fn struct_note(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
1370        Diag::new(self, Note, msg)
1371    }
1372
1373    #[rustc_lint_diagnostics]
1374    #[track_caller]
1375    pub fn note(&self, msg: impl Into<DiagMessage>) {
1376        self.struct_note(msg).emit()
1377    }
1378
1379    #[rustc_lint_diagnostics]
1380    #[track_caller]
1381    pub fn struct_span_note(
1382        self,
1383        span: impl Into<MultiSpan>,
1384        msg: impl Into<DiagMessage>,
1385    ) -> Diag<'a, ()> {
1386        self.struct_note(msg).with_span(span)
1387    }
1388
1389    #[rustc_lint_diagnostics]
1390    #[track_caller]
1391    pub fn span_note(self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
1392        self.struct_span_note(span, msg).emit()
1393    }
1394
1395    #[track_caller]
1396    pub fn create_note(self, note: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
1397        note.into_diag(self, Note)
1398    }
1399
1400    #[track_caller]
1401    pub fn emit_note(self, note: impl Diagnostic<'a, ()>) {
1402        self.create_note(note).emit()
1403    }
1404
1405    #[rustc_lint_diagnostics]
1406    #[track_caller]
1407    pub fn struct_help(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
1408        Diag::new(self, Help, msg)
1409    }
1410
1411    #[rustc_lint_diagnostics]
1412    #[track_caller]
1413    pub fn struct_failure_note(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
1414        Diag::new(self, FailureNote, msg)
1415    }
1416
1417    #[rustc_lint_diagnostics]
1418    #[track_caller]
1419    pub fn struct_allow(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
1420        Diag::new(self, Allow, msg)
1421    }
1422
1423    #[rustc_lint_diagnostics]
1424    #[track_caller]
1425    pub fn struct_expect(self, msg: impl Into<DiagMessage>, id: LintExpectationId) -> Diag<'a, ()> {
1426        Diag::new(self, Expect(id), msg)
1427    }
1428}
1429
1430// Note: we prefer implementing operations on `DiagCtxt`, rather than
1431// `DiagCtxtInner`, whenever possible. This minimizes functions where
1432// `DiagCtxt::foo()` just borrows `inner` and forwards a call to
1433// `DiagCtxtInner::foo`.
1434impl DiagCtxtInner {
1435    fn new(emitter: Box<DynEmitter>) -> Self {
1436        Self {
1437            flags: DiagCtxtFlags { can_emit_warnings: true, ..Default::default() },
1438            registry: Registry::new(&[]),
1439            err_guars: Vec::new(),
1440            lint_err_guars: Vec::new(),
1441            delayed_bugs: Vec::new(),
1442            deduplicated_err_count: 0,
1443            deduplicated_warn_count: 0,
1444            emitter,
1445            must_produce_diag: None,
1446            has_printed: false,
1447            suppressed_expected_diag: false,
1448            taught_diagnostics: Default::default(),
1449            emitted_diagnostic_codes: Default::default(),
1450            emitted_diagnostics: Default::default(),
1451            stashed_diagnostics: Default::default(),
1452            future_breakage_diagnostics: Vec::new(),
1453            fulfilled_expectations: Default::default(),
1454            ice_file: None,
1455        }
1456    }
1457
1458    /// Emit all stashed diagnostics.
1459    fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
1460        let mut guar = None;
1461        let has_errors = !self.err_guars.is_empty();
1462        for (_, (diag, _guar)) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
1463            if !diag.is_error() {
1464                // Unless they're forced, don't flush stashed warnings when
1465                // there are errors, to avoid causing warning overload. The
1466                // stash would've been stolen already if it were important.
1467                if !diag.is_force_warn() && has_errors {
1468                    continue;
1469                }
1470            }
1471            guar = guar.or(self.emit_diagnostic(diag, None));
1472        }
1473        guar
1474    }
1475
1476    // Return value is only `Some` if the level is `Error` or `DelayedBug`.
1477    fn emit_diagnostic(
1478        &mut self,
1479        mut diagnostic: DiagInner,
1480        taint: Option<&Cell<Option<ErrorGuaranteed>>>,
1481    ) -> Option<ErrorGuaranteed> {
1482        if diagnostic.has_future_breakage() {
1483            // Future breakages aren't emitted if they're `Level::Allow` or
1484            // `Level::Expect`, but they still need to be constructed and
1485            // stashed below, so they'll trigger the must_produce_diag check.
1486            assert_matches!(diagnostic.level, Error | Warning | Allow | Expect(_));
1487            self.future_breakage_diagnostics.push(diagnostic.clone());
1488        }
1489
1490        // We call TRACK_DIAGNOSTIC with an empty closure for the cases that
1491        // return early *and* have some kind of side-effect, except where
1492        // noted.
1493        match diagnostic.level {
1494            Bug => {}
1495            Fatal | Error => {
1496                if self.treat_next_err_as_bug() {
1497                    // `Fatal` and `Error` can be promoted to `Bug`.
1498                    diagnostic.level = Bug;
1499                }
1500            }
1501            DelayedBug => {
1502                // Note that because we check these conditions first,
1503                // `-Zeagerly-emit-delayed-bugs` and `-Ztreat-err-as-bug`
1504                // continue to work even after we've issued an error and
1505                // stopped recording new delayed bugs.
1506                if self.flags.eagerly_emit_delayed_bugs {
1507                    // `DelayedBug` can be promoted to `Error` or `Bug`.
1508                    if self.treat_next_err_as_bug() {
1509                        diagnostic.level = Bug;
1510                    } else {
1511                        diagnostic.level = Error;
1512                    }
1513                } else {
1514                    // If we have already emitted at least one error, we don't need
1515                    // to record the delayed bug, because it'll never be used.
1516                    return if let Some(guar) = self.has_errors() {
1517                        Some(guar)
1518                    } else {
1519                        // No `TRACK_DIAGNOSTIC` call is needed, because the
1520                        // incremental session is deleted if there is a delayed
1521                        // bug. This also saves us from cloning the diagnostic.
1522                        let backtrace = std::backtrace::Backtrace::capture();
1523                        // This `unchecked_error_guaranteed` is valid. It is where the
1524                        // `ErrorGuaranteed` for delayed bugs originates. See
1525                        // `DiagCtxtInner::drop`.
1526                        #[allow(deprecated)]
1527                        let guar = ErrorGuaranteed::unchecked_error_guaranteed();
1528                        self.delayed_bugs
1529                            .push((DelayedDiagInner::with_backtrace(diagnostic, backtrace), guar));
1530                        Some(guar)
1531                    };
1532                }
1533            }
1534            ForceWarning(None) => {} // `ForceWarning(Some(...))` is below, with `Expect`
1535            Warning => {
1536                if !self.flags.can_emit_warnings {
1537                    // We are not emitting warnings.
1538                    if diagnostic.has_future_breakage() {
1539                        // The side-effect is at the top of this method.
1540                        TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
1541                    }
1542                    return None;
1543                }
1544            }
1545            Note | Help | FailureNote => {}
1546            OnceNote | OnceHelp => panic!("bad level: {:?}", diagnostic.level),
1547            Allow => {
1548                // Nothing emitted for allowed lints.
1549                if diagnostic.has_future_breakage() {
1550                    // The side-effect is at the top of this method.
1551                    TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
1552                    self.suppressed_expected_diag = true;
1553                }
1554                return None;
1555            }
1556            Expect(expect_id) | ForceWarning(Some(expect_id)) => {
1557                self.fulfilled_expectations.insert(expect_id);
1558                if let Expect(_) = diagnostic.level {
1559                    // Nothing emitted here for expected lints.
1560                    TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
1561                    self.suppressed_expected_diag = true;
1562                    return None;
1563                }
1564            }
1565        }
1566
1567        TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| {
1568            if let Some(code) = diagnostic.code {
1569                self.emitted_diagnostic_codes.insert(code);
1570            }
1571
1572            let already_emitted = {
1573                let mut hasher = StableHasher::new();
1574                diagnostic.hash(&mut hasher);
1575                let diagnostic_hash = hasher.finish();
1576                !self.emitted_diagnostics.insert(diagnostic_hash)
1577            };
1578
1579            let is_error = diagnostic.is_error();
1580            let is_lint = diagnostic.is_lint.is_some();
1581
1582            // Only emit the diagnostic if we've been asked to deduplicate or
1583            // haven't already emitted an equivalent diagnostic.
1584            if !(self.flags.deduplicate_diagnostics && already_emitted) {
1585                debug!(?diagnostic);
1586                debug!(?self.emitted_diagnostics);
1587
1588                let not_yet_emitted = |sub: &mut Subdiag| {
1589                    debug!(?sub);
1590                    if sub.level != OnceNote && sub.level != OnceHelp {
1591                        return true;
1592                    }
1593                    let mut hasher = StableHasher::new();
1594                    sub.hash(&mut hasher);
1595                    let diagnostic_hash = hasher.finish();
1596                    debug!(?diagnostic_hash);
1597                    self.emitted_diagnostics.insert(diagnostic_hash)
1598                };
1599                diagnostic.children.retain_mut(not_yet_emitted);
1600                if already_emitted {
1601                    let msg = "duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`";
1602                    diagnostic.sub(Note, msg, MultiSpan::new());
1603                }
1604
1605                if is_error {
1606                    self.deduplicated_err_count += 1;
1607                } else if matches!(diagnostic.level, ForceWarning(_) | Warning) {
1608                    self.deduplicated_warn_count += 1;
1609                }
1610                self.has_printed = true;
1611
1612                self.emitter.emit_diagnostic(diagnostic, &self.registry);
1613            }
1614
1615            if is_error {
1616                // If we have any delayed bugs recorded, we can discard them
1617                // because they won't be used. (This should only occur if there
1618                // have been no errors previously emitted, because we don't add
1619                // new delayed bugs once the first error is emitted.)
1620                if !self.delayed_bugs.is_empty() {
1621                    assert_eq!(self.lint_err_guars.len() + self.err_guars.len(), 0);
1622                    self.delayed_bugs.clear();
1623                    self.delayed_bugs.shrink_to_fit();
1624                }
1625
1626                // This `unchecked_error_guaranteed` is valid. It is where the
1627                // `ErrorGuaranteed` for errors and lint errors originates.
1628                #[allow(deprecated)]
1629                let guar = ErrorGuaranteed::unchecked_error_guaranteed();
1630                if is_lint {
1631                    self.lint_err_guars.push(guar);
1632                } else {
1633                    if let Some(taint) = taint {
1634                        taint.set(Some(guar));
1635                    }
1636                    self.err_guars.push(guar);
1637                }
1638                self.panic_if_treat_err_as_bug();
1639                Some(guar)
1640            } else {
1641                None
1642            }
1643        })
1644    }
1645
1646    fn treat_err_as_bug(&self) -> bool {
1647        self.flags
1648            .treat_err_as_bug
1649            .is_some_and(|c| self.err_guars.len() + self.lint_err_guars.len() >= c.get())
1650    }
1651
1652    // Use this one before incrementing `err_count`.
1653    fn treat_next_err_as_bug(&self) -> bool {
1654        self.flags
1655            .treat_err_as_bug
1656            .is_some_and(|c| self.err_guars.len() + self.lint_err_guars.len() + 1 >= c.get())
1657    }
1658
1659    fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
1660        self.err_guars.get(0).copied().or_else(|| {
1661            if let Some((_diag, guar)) = self
1662                .stashed_diagnostics
1663                .values()
1664                .find(|(diag, guar)| guar.is_some() && diag.is_lint.is_none())
1665            {
1666                *guar
1667            } else {
1668                None
1669            }
1670        })
1671    }
1672
1673    fn has_errors(&self) -> Option<ErrorGuaranteed> {
1674        self.err_guars.get(0).copied().or_else(|| self.lint_err_guars.get(0).copied()).or_else(
1675            || {
1676                if let Some((_diag, guar)) =
1677                    self.stashed_diagnostics.values().find(|(_diag, guar)| guar.is_some())
1678                {
1679                    *guar
1680                } else {
1681                    None
1682                }
1683            },
1684        )
1685    }
1686
1687    fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1688        self.has_errors().or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
1689    }
1690
1691    /// Translate `message` eagerly with `args` to `SubdiagMessage::Eager`.
1692    fn eagerly_translate<'a>(
1693        &self,
1694        message: DiagMessage,
1695        args: impl Iterator<Item = DiagArg<'a>>,
1696    ) -> SubdiagMessage {
1697        SubdiagMessage::Translated(Cow::from(self.eagerly_translate_to_string(message, args)))
1698    }
1699
1700    /// Translate `message` eagerly with `args` to `String`.
1701    fn eagerly_translate_to_string<'a>(
1702        &self,
1703        message: DiagMessage,
1704        args: impl Iterator<Item = DiagArg<'a>>,
1705    ) -> String {
1706        let args = crate::translation::to_fluent_args(args);
1707        self.emitter.translate_message(&message, &args).map_err(Report::new).unwrap().to_string()
1708    }
1709
1710    fn eagerly_translate_for_subdiag(
1711        &self,
1712        diag: &DiagInner,
1713        msg: impl Into<SubdiagMessage>,
1714    ) -> SubdiagMessage {
1715        let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
1716        self.eagerly_translate(msg, diag.args.iter())
1717    }
1718
1719    fn flush_delayed(&mut self) {
1720        // Stashed diagnostics must be emitted before delayed bugs are flushed.
1721        // Otherwise, we might ICE prematurely when errors would have
1722        // eventually happened.
1723        assert!(self.stashed_diagnostics.is_empty());
1724
1725        if !self.err_guars.is_empty() {
1726            // If an error happened already. We shouldn't expose delayed bugs.
1727            return;
1728        }
1729
1730        if self.delayed_bugs.is_empty() {
1731            // Nothing to do.
1732            return;
1733        }
1734
1735        let bugs: Vec<_> =
1736            std::mem::take(&mut self.delayed_bugs).into_iter().map(|(b, _)| b).collect();
1737
1738        let backtrace = std::env::var_os("RUST_BACKTRACE").as_deref() != Some(OsStr::new("0"));
1739        let decorate = backtrace || self.ice_file.is_none();
1740        let mut out = self
1741            .ice_file
1742            .as_ref()
1743            .and_then(|file| std::fs::File::options().create(true).append(true).open(file).ok());
1744
1745        // Put the overall explanation before the `DelayedBug`s, to frame them
1746        // better (e.g. separate warnings from them). Also, use notes, which
1747        // don't count as errors, to avoid possibly triggering
1748        // `-Ztreat-err-as-bug`, which we don't want.
1749        let note1 = "no errors encountered even though delayed bugs were created";
1750        let note2 = "those delayed bugs will now be shown as internal compiler errors";
1751        self.emit_diagnostic(DiagInner::new(Note, note1), None);
1752        self.emit_diagnostic(DiagInner::new(Note, note2), None);
1753
1754        for bug in bugs {
1755            if let Some(out) = &mut out {
1756                _ = write!(
1757                    out,
1758                    "delayed bug: {}\n{}\n",
1759                    bug.inner
1760                        .messages
1761                        .iter()
1762                        .filter_map(|(msg, _)| msg.as_str())
1763                        .collect::<String>(),
1764                    &bug.note
1765                );
1766            }
1767
1768            let mut bug = if decorate { bug.decorate(self) } else { bug.inner };
1769
1770            // "Undelay" the delayed bugs into plain bugs.
1771            if bug.level != DelayedBug {
1772                // NOTE(eddyb) not panicking here because we're already producing
1773                // an ICE, and the more information the merrier.
1774                //
1775                // We are at the `DiagInner`/`DiagCtxtInner` level rather than
1776                // the usual `Diag`/`DiagCtxt` level, so we must augment `bug`
1777                // in a lower-level fashion.
1778                bug.arg("level", bug.level);
1779                let msg = crate::fluent_generated::errors_invalid_flushed_delayed_diagnostic_level;
1780                let msg = self.eagerly_translate_for_subdiag(&bug, msg); // after the `arg` call
1781                bug.sub(Note, msg, bug.span.primary_span().unwrap().into());
1782            }
1783            bug.level = Bug;
1784
1785            self.emit_diagnostic(bug, None);
1786        }
1787
1788        // Panic with `DelayedBugPanic` to avoid "unexpected panic" messages.
1789        panic::panic_any(DelayedBugPanic);
1790    }
1791
1792    fn panic_if_treat_err_as_bug(&self) {
1793        if self.treat_err_as_bug() {
1794            let n = self.flags.treat_err_as_bug.map(|c| c.get()).unwrap();
1795            assert_eq!(n, self.err_guars.len() + self.lint_err_guars.len());
1796            if n == 1 {
1797                panic!("aborting due to `-Z treat-err-as-bug=1`");
1798            } else {
1799                panic!("aborting after {n} errors due to `-Z treat-err-as-bug={n}`");
1800            }
1801        }
1802    }
1803}
1804
1805struct DelayedDiagInner {
1806    inner: DiagInner,
1807    note: Backtrace,
1808}
1809
1810impl DelayedDiagInner {
1811    fn with_backtrace(diagnostic: DiagInner, backtrace: Backtrace) -> Self {
1812        DelayedDiagInner { inner: diagnostic, note: backtrace }
1813    }
1814
1815    fn decorate(self, dcx: &DiagCtxtInner) -> DiagInner {
1816        // We are at the `DiagInner`/`DiagCtxtInner` level rather than the
1817        // usual `Diag`/`DiagCtxt` level, so we must construct `diag` in a
1818        // lower-level fashion.
1819        let mut diag = self.inner;
1820        let msg = match self.note.status() {
1821            BacktraceStatus::Captured => crate::fluent_generated::errors_delayed_at_with_newline,
1822            // Avoid the needless newline when no backtrace has been captured,
1823            // the display impl should just be a single line.
1824            _ => crate::fluent_generated::errors_delayed_at_without_newline,
1825        };
1826        diag.arg("emitted_at", diag.emitted_at.clone());
1827        diag.arg("note", self.note);
1828        let msg = dcx.eagerly_translate_for_subdiag(&diag, msg); // after the `arg` calls
1829        diag.sub(Note, msg, diag.span.primary_span().unwrap_or(DUMMY_SP).into());
1830        diag
1831    }
1832}
1833
1834/// | Level        | is_error | EmissionGuarantee            | Top-level | Sub | Used in lints?
1835/// | -----        | -------- | -----------------            | --------- | --- | --------------
1836/// | Bug          | yes      | BugAbort                     | yes       | -   | -
1837/// | Fatal        | yes      | FatalAbort/FatalError[^star] | yes       | -   | -
1838/// | Error        | yes      | ErrorGuaranteed              | yes       | -   | yes
1839/// | DelayedBug   | yes      | ErrorGuaranteed              | yes       | -   | -
1840/// | ForceWarning | -        | ()                           | yes       | -   | lint-only
1841/// | Warning      | -        | ()                           | yes       | yes | yes
1842/// | Note         | -        | ()                           | rare      | yes | -
1843/// | OnceNote     | -        | ()                           | -         | yes | lint-only
1844/// | Help         | -        | ()                           | rare      | yes | -
1845/// | OnceHelp     | -        | ()                           | -         | yes | lint-only
1846/// | FailureNote  | -        | ()                           | rare      | -   | -
1847/// | Allow        | -        | ()                           | yes       | -   | lint-only
1848/// | Expect       | -        | ()                           | yes       | -   | lint-only
1849///
1850/// [^star]: `FatalAbort` normally, `FatalError` in the non-aborting "almost fatal" case that is
1851///     occasionally used.
1852///
1853#[derive(Copy, PartialEq, Eq, Clone, Hash, Debug, Encodable, Decodable)]
1854pub enum Level {
1855    /// For bugs in the compiler. Manifests as an ICE (internal compiler error) panic.
1856    Bug,
1857
1858    /// An error that causes an immediate abort. Used for things like configuration errors,
1859    /// internal overflows, some file operation errors.
1860    Fatal,
1861
1862    /// An error in the code being compiled, which prevents compilation from finishing. This is the
1863    /// most common case.
1864    Error,
1865
1866    /// This is a strange one: lets you register an error without emitting it. If compilation ends
1867    /// without any other errors occurring, this will be emitted as a bug. Otherwise, it will be
1868    /// silently dropped. I.e. "expect other errors are emitted" semantics. Useful on code paths
1869    /// that should only be reached when compiling erroneous code.
1870    DelayedBug,
1871
1872    /// A `force-warn` lint warning about the code being compiled. Does not prevent compilation
1873    /// from finishing.
1874    ///
1875    /// The [`LintExpectationId`] is used for expected lint diagnostics. In all other cases this
1876    /// should be `None`.
1877    ForceWarning(Option<LintExpectationId>),
1878
1879    /// A warning about the code being compiled. Does not prevent compilation from finishing.
1880    /// Will be skipped if `can_emit_warnings` is false.
1881    Warning,
1882
1883    /// A message giving additional context.
1884    Note,
1885
1886    /// A note that is only emitted once.
1887    OnceNote,
1888
1889    /// A message suggesting how to fix something.
1890    Help,
1891
1892    /// A help that is only emitted once.
1893    OnceHelp,
1894
1895    /// Similar to `Note`, but used in cases where compilation has failed. When printed for human
1896    /// consumption, it doesn't have any kind of `note:` label.
1897    FailureNote,
1898
1899    /// Only used for lints.
1900    Allow,
1901
1902    /// Only used for lints.
1903    Expect(LintExpectationId),
1904}
1905
1906impl fmt::Display for Level {
1907    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1908        self.to_str().fmt(f)
1909    }
1910}
1911
1912impl Level {
1913    fn color(self) -> ColorSpec {
1914        let mut spec = ColorSpec::new();
1915        match self {
1916            Bug | Fatal | Error | DelayedBug => {
1917                spec.set_fg(Some(Color::Red)).set_intense(true);
1918            }
1919            ForceWarning(_) | Warning => {
1920                spec.set_fg(Some(Color::Yellow)).set_intense(cfg!(windows));
1921            }
1922            Note | OnceNote => {
1923                spec.set_fg(Some(Color::Green)).set_intense(true);
1924            }
1925            Help | OnceHelp => {
1926                spec.set_fg(Some(Color::Cyan)).set_intense(true);
1927            }
1928            FailureNote => {}
1929            Allow | Expect(_) => unreachable!(),
1930        }
1931        spec
1932    }
1933
1934    pub fn to_str(self) -> &'static str {
1935        match self {
1936            Bug | DelayedBug => "error: internal compiler error",
1937            Fatal | Error => "error",
1938            ForceWarning(_) | Warning => "warning",
1939            Note | OnceNote => "note",
1940            Help | OnceHelp => "help",
1941            FailureNote => "failure-note",
1942            Allow | Expect(_) => unreachable!(),
1943        }
1944    }
1945
1946    pub fn is_failure_note(&self) -> bool {
1947        matches!(*self, FailureNote)
1948    }
1949
1950    // Can this level be used in a subdiagnostic message?
1951    fn can_be_subdiag(&self) -> bool {
1952        match self {
1953            Bug | DelayedBug | Fatal | Error | ForceWarning(_) | FailureNote | Allow
1954            | Expect(_) => false,
1955
1956            Warning | Note | Help | OnceNote | OnceHelp => true,
1957        }
1958    }
1959}
1960
1961// FIXME(eddyb) this doesn't belong here AFAICT, should be moved to callsite.
1962pub fn elided_lifetime_in_path_suggestion(
1963    source_map: &SourceMap,
1964    n: usize,
1965    path_span: Span,
1966    incl_angl_brckt: bool,
1967    insertion_span: Span,
1968) -> ElidedLifetimeInPathSubdiag {
1969    let expected = ExpectedLifetimeParameter { span: path_span, count: n };
1970    // Do not try to suggest anything if generated by a proc-macro.
1971    let indicate = source_map.is_span_accessible(insertion_span).then(|| {
1972        let anon_lts = vec!["'_"; n].join(", ");
1973        let suggestion =
1974            if incl_angl_brckt { format!("<{anon_lts}>") } else { format!("{anon_lts}, ") };
1975
1976        IndicateAnonymousLifetime { span: insertion_span.shrink_to_hi(), count: n, suggestion }
1977    });
1978
1979    ElidedLifetimeInPathSubdiag { expected, indicate }
1980}
1981
1982pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
1983    diag: &mut Diag<'a, G>,
1984    ambiguity: rustc_lint_defs::AmbiguityErrorDiag,
1985) {
1986    diag.span_label(ambiguity.label_span, ambiguity.label_msg);
1987    diag.note(ambiguity.note_msg);
1988    diag.span_note(ambiguity.b1_span, ambiguity.b1_note_msg);
1989    for help_msg in ambiguity.b1_help_msgs {
1990        diag.help(help_msg);
1991    }
1992    diag.span_note(ambiguity.b2_span, ambiguity.b2_note_msg);
1993    for help_msg in ambiguity.b2_help_msgs {
1994        diag.help(help_msg);
1995    }
1996}
1997
1998/// Grammatical tool for displaying messages to end users in a nice form.
1999///
2000/// Returns "an" if the given string starts with a vowel, and "a" otherwise.
2001pub fn a_or_an(s: &str) -> &'static str {
2002    let mut chars = s.chars();
2003    let Some(mut first_alpha_char) = chars.next() else {
2004        return "a";
2005    };
2006    if first_alpha_char == '`' {
2007        let Some(next) = chars.next() else {
2008            return "a";
2009        };
2010        first_alpha_char = next;
2011    }
2012    if ["a", "e", "i", "o", "u", "&"].contains(&&first_alpha_char.to_lowercase().to_string()[..]) {
2013        "an"
2014    } else {
2015        "a"
2016    }
2017}
2018
2019#[derive(Clone, Copy, PartialEq, Hash, Debug)]
2020pub enum TerminalUrl {
2021    No,
2022    Yes,
2023    Auto,
2024}