1#![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)]
29extern 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;
75pub 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#[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 HideCodeInline,
112 HideCodeAlways,
114 CompletelyHidden,
116 ShowCode,
120 ShowAlways,
122}
123
124impl SuggestionStyle {
125 fn hide_inline(&self) -> bool {
126 !matches!(*self, SuggestionStyle::ShowCode)
127 }
128}
129
130#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
132pub enum Suggestions {
133 Enabled(Vec<CodeSuggestion>),
138 Sealed(Box<[CodeSuggestion]>),
142 Disabled,
146}
147
148impl Suggestions {
149 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 pub substitutions: Vec<Substitution>,
189 pub msg: DiagMessage,
190 pub style: SuggestionStyle,
192 pub applicability: Applicability,
198}
199
200#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
201pub 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#[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 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 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 pub(crate) fn splice_lines(
273 &self,
274 sm: &SourceMap,
275 ) -> Vec<(String, Vec<SubstitutionPart>, Vec<Vec<SubstitutionHighlight>>, bool)> {
276 use rustc_span::{CharPos, Pos};
281
282 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 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 let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
304 match hi_opt {
305 Some(hi) if hi > lo => {
307 line_count = line[lo..hi].matches('\n').count();
309 buf.push_str(&line[lo..hi])
310 }
311 Some(_) => (),
312 None => {
314 line_count = line[lo..].matches('\n').count();
316 buf.push_str(&line[lo..])
317 }
318 }
319 }
320 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 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 substitution.parts.sort_by_key(|part| part.span.lo());
346
347 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 let lines = sm.span_to_lines(bounding_span).ok()?;
353 assert!(!lines.lines.is_empty() || bounding_span.is_dummy());
354
355 if !sm.ensure_source_file_source_present(&lines.file) {
357 return None;
358 }
359
360 let mut highlights = vec![];
361 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 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 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 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 } 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 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 !buf.ends_with('\n') {
464 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
465 }
466 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
480pub struct ExplicitBug;
483
484pub struct DelayedBugPanic;
487
488pub struct DiagCtxt {
492 inner: Lock<DiagCtxtInner>,
493}
494
495#[derive(Copy, Clone)]
496pub struct DiagCtxtHandle<'a> {
497 dcx: &'a DiagCtxt,
498 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
511struct DiagCtxtInner {
515 flags: DiagCtxtFlags,
516
517 registry: Registry,
518
519 err_guars: Vec<ErrorGuaranteed>,
521 lint_err_guars: Vec<ErrorGuaranteed>,
524 delayed_bugs: Vec<(DelayedDiagInner, ErrorGuaranteed)>,
526
527 deduplicated_err_count: usize,
529 deduplicated_warn_count: usize,
531
532 emitter: Box<DynEmitter>,
533
534 must_produce_diag: Option<Backtrace>,
537
538 has_printed: bool,
541
542 suppressed_expected_diag: bool,
545
546 taught_diagnostics: FxHashSet<ErrCode>,
550
551 emitted_diagnostic_codes: FxIndexSet<ErrCode>,
553
554 emitted_diagnostics: FxHashSet<Hash128>,
558
559 stashed_diagnostics: FxIndexMap<(Span, StashKey), (DiagInner, Option<ErrorGuaranteed>)>,
565
566 future_breakage_diagnostics: Vec<DiagInner>,
567
568 fulfilled_expectations: FxIndexSet<LintExpectationId>,
580
581 ice_file: Option<PathBuf>,
584}
585
586#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
588pub enum StashKey {
589 ItemNoType,
590 UnderscoreForArrayLengths,
591 EarlySyntaxWarning,
592 CallIntoMethod,
593 LifetimeIsChar,
596 MaybeFruTypo,
599 CallAssocMethod,
600 AssociatedTypeSuggestion,
601 MaybeForgetReturn,
602 Cycle,
604 UndeterminedMacroResolution,
605 ExprInPat,
607 GenericInFieldExpr,
611}
612
613fn default_track_diagnostic<R>(diag: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
614 (*f)(diag)
615}
616
617pub 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 pub can_emit_warnings: bool,
628 pub treat_err_as_bug: Option<NonZero<usize>>,
631 pub eagerly_emit_delayed_bugs: bool,
634 pub macro_backtrace: bool,
637 pub deduplicate_diagnostics: bool,
639 pub track_diagnostics: bool,
641}
642
643impl Drop for DiagCtxtInner {
644 fn drop(&mut self) {
645 self.emit_stashed_diagnostics();
653
654 self.flush_delayed();
658
659 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 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 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 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 pub fn can_emit_warnings(&self) -> bool {
773 self.inner.borrow_mut().flags.can_emit_warnings
774 }
775
776 pub fn reset_err_count(&self) {
782 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 *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 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 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 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 let key = (span.with_parent(None), key);
889 self.inner.borrow_mut().stashed_diagnostics.insert(key, (diag, guar));
890
891 guar
892 }
893
894 pub fn steal_non_err(self, span: Span, key: StashKey) -> Option<Diag<'a, ()>> {
898 let key = (span.with_parent(None), key);
899 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 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 let err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
922 err.map(|(err, guar)| {
923 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 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 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 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 pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
964 self.inner.borrow_mut().emit_stashed_diagnostics()
965 }
966
967 #[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 pub fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
979 self.inner.borrow().has_errors_excluding_lint_errors()
980 }
981
982 pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
984 self.inner.borrow().has_errors()
985 }
986
987 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 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 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 pub fn abort_if_errors(&self) {
1081 if let Some(guar) = self.has_errors() {
1082 guar.raise_fatal();
1083 }
1084 }
1085
1086 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 if loud && lint_level.is_error() {
1130 #[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 #[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 #[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
1163impl<'a> DiagCtxtHandle<'a> {
1168 #[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 #[track_caller]
1178 pub fn bug(self, msg: impl Into<Cow<'static, str>>) -> ! {
1179 self.struct_bug(msg).emit()
1180 }
1181
1182 #[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 #[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 #[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 #[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 #[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
1430impl 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 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 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 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 assert_matches!(diagnostic.level, Error | Warning | Allow | Expect(_));
1487 self.future_breakage_diagnostics.push(diagnostic.clone());
1488 }
1489
1490 match diagnostic.level {
1494 Bug => {}
1495 Fatal | Error => {
1496 if self.treat_next_err_as_bug() {
1497 diagnostic.level = Bug;
1499 }
1500 }
1501 DelayedBug => {
1502 if self.flags.eagerly_emit_delayed_bugs {
1507 if self.treat_next_err_as_bug() {
1509 diagnostic.level = Bug;
1510 } else {
1511 diagnostic.level = Error;
1512 }
1513 } else {
1514 return if let Some(guar) = self.has_errors() {
1517 Some(guar)
1518 } else {
1519 let backtrace = std::backtrace::Backtrace::capture();
1523 #[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) => {} Warning => {
1536 if !self.flags.can_emit_warnings {
1537 if diagnostic.has_future_breakage() {
1539 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 if diagnostic.has_future_breakage() {
1550 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 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 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 !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 #[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 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 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 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 assert!(self.stashed_diagnostics.is_empty());
1724
1725 if !self.err_guars.is_empty() {
1726 return;
1728 }
1729
1730 if self.delayed_bugs.is_empty() {
1731 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 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 if bug.level != DelayedBug {
1772 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); 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::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 let mut diag = self.inner;
1820 let msg = match self.note.status() {
1821 BacktraceStatus::Captured => crate::fluent_generated::errors_delayed_at_with_newline,
1822 _ => 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); diag.sub(Note, msg, diag.span.primary_span().unwrap_or(DUMMY_SP).into());
1830 diag
1831 }
1832}
1833
1834#[derive(Copy, PartialEq, Eq, Clone, Hash, Debug, Encodable, Decodable)]
1854pub enum Level {
1855 Bug,
1857
1858 Fatal,
1861
1862 Error,
1865
1866 DelayedBug,
1871
1872 ForceWarning(Option<LintExpectationId>),
1878
1879 Warning,
1882
1883 Note,
1885
1886 OnceNote,
1888
1889 Help,
1891
1892 OnceHelp,
1894
1895 FailureNote,
1898
1899 Allow,
1901
1902 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 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
1961pub 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 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
1998pub 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}