1 //! Diagnostics creation and emission for `rustc`.
3 //! This module contains the code for creating and emitting diagnostics.
5 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
7 #![feature(crate_visibility_modifier)]
8 #![cfg_attr(unix, feature(libc))]
10 #![feature(optin_builtin_traits)]
12 pub use emitter::ColorConfig;
16 use emitter::{Emitter, EmitterWriter, is_case_difference};
17 use registry::Registry;
18 use rustc_data_structures::sync::{self, Lrc, Lock};
19 use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
20 use rustc_data_structures::stable_hasher::StableHasher;
24 use std::{error, fmt};
28 use termcolor::{ColorSpec, Color};
31 mod diagnostic_builder;
33 pub mod annotate_snippet_emitter_writer;
50 pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
52 // `PResult` is used a lot. Make sure it doesn't unintentionally get bigger.
53 // (See also the comment on `DiagnosticBuilderInner`.)
54 #[cfg(target_arch = "x86_64")]
55 rustc_data_structures::static_assert_size!(PResult<'_, bool>, 16);
57 /// Indicates the confidence in the correctness of a suggestion.
59 /// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
60 /// to determine whether it should be automatically applied or if the user should be consulted
61 /// before applying the suggestion.
62 #[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
63 pub enum Applicability {
64 /// The suggestion is definitely what the user intended. This suggestion should be
65 /// automatically applied.
68 /// The suggestion may be what the user intended, but it is uncertain. The suggestion should
69 /// result in valid Rust code if it is applied.
72 /// The suggestion contains placeholders like `(...)` or `{ /* fields */ }`. The suggestion
73 /// cannot be applied automatically because it will not result in valid Rust code. The user
74 /// will need to fill in the placeholders.
77 /// The applicability of the suggestion is unknown.
81 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, RustcEncodable, RustcDecodable)]
82 pub enum SuggestionStyle {
83 /// Hide the suggested code when displaying this suggestion inline.
85 /// Always hide the suggested code but display the message.
87 /// Do not display this suggestion in the cli output, it is only meant for tools.
89 /// Always show the suggested code.
90 /// This will *not* show the code if the suggestion is inline *and* the suggested code is
93 /// Always show the suggested code independently.
97 impl SuggestionStyle {
98 fn hide_inline(&self) -> bool {
100 SuggestionStyle::ShowCode => false,
106 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
107 pub struct CodeSuggestion {
108 /// Each substitute can have multiple variants due to multiple
109 /// applicable suggestions
111 /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
112 /// `foo` and `bar` on their own:
116 /// Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
117 /// Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
121 /// or by replacing the entire span:
125 /// Substitution { parts: vec![(0..7, "a.b")] },
126 /// Substitution { parts: vec![(0..7, "x.y")] },
129 pub substitutions: Vec<Substitution>,
131 /// Visual representation of this suggestion.
132 pub style: SuggestionStyle,
133 /// Whether or not the suggestion is approximate
135 /// Sometimes we may show suggestions with placeholders,
136 /// which are useful for users but not useful for
137 /// tools like rustfix
138 pub applicability: Applicability,
141 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
142 /// See the docs on `CodeSuggestion::substitutions`
143 pub struct Substitution {
144 pub parts: Vec<SubstitutionPart>,
147 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
148 pub struct SubstitutionPart {
153 pub type SourceMapperDyn = dyn SourceMapper + sync::Send + sync::Sync;
155 pub trait SourceMapper {
156 fn lookup_char_pos(&self, pos: BytePos) -> Loc;
157 fn span_to_lines(&self, sp: Span) -> FileLinesResult;
158 fn span_to_string(&self, sp: Span) -> String;
159 fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError>;
160 fn span_to_filename(&self, sp: Span) -> FileName;
161 fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
162 fn call_span_if_macro(&self, sp: Span) -> Span;
163 fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool;
164 fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize;
167 impl CodeSuggestion {
168 /// Returns the assembled code suggestions, whether they should be shown with an underline
169 /// and whether the substitution only differs in capitalization.
172 cm: &SourceMapperDyn,
173 ) -> Vec<(String, Vec<SubstitutionPart>, bool)> {
174 use syntax_pos::{CharPos, Pos};
176 fn push_trailing(buf: &mut String,
177 line_opt: Option<&Cow<'_, str>>,
179 hi_opt: Option<&Loc>) {
180 let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
181 if let Some(line) = line_opt {
182 if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
183 let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
185 Some(hi) if hi > lo => buf.push_str(&line[lo..hi]),
187 None => buf.push_str(&line[lo..]),
190 if let None = hi_opt {
196 assert!(!self.substitutions.is_empty());
198 self.substitutions.iter().cloned().map(|mut substitution| {
199 // Assumption: all spans are in the same file, and all spans
200 // are disjoint. Sort in ascending order.
201 substitution.parts.sort_by_key(|part| part.span.lo());
203 // Find the bounding span.
204 let lo = substitution.parts.iter().map(|part| part.span.lo()).min().unwrap();
205 let hi = substitution.parts.iter().map(|part| part.span.hi()).min().unwrap();
206 let bounding_span = Span::with_root_ctxt(lo, hi);
207 let lines = cm.span_to_lines(bounding_span).unwrap();
208 assert!(!lines.lines.is_empty());
210 // To build up the result, we do this for each span:
211 // - push the line segment trailing the previous span
212 // (at the beginning a "phantom" span pointing at the start of the line)
213 // - push lines between the previous and current span (if any)
214 // - if the previous and current span are not on the same line
215 // push the line segment leading up to the current span
216 // - splice in the span substitution
218 // Finally push the trailing line segment of the last span
219 let fm = &lines.file;
220 let mut prev_hi = cm.lookup_char_pos(bounding_span.lo());
221 prev_hi.col = CharPos::from_usize(0);
223 let mut prev_line = fm.get_line(lines.lines[0].line_index);
224 let mut buf = String::new();
226 for part in &substitution.parts {
227 let cur_lo = cm.lookup_char_pos(part.span.lo());
228 if prev_hi.line == cur_lo.line {
229 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
231 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
232 // push lines between the previous and current span (if any)
233 for idx in prev_hi.line..(cur_lo.line - 1) {
234 if let Some(line) = fm.get_line(idx) {
235 buf.push_str(line.as_ref());
239 if let Some(cur_line) = fm.get_line(cur_lo.line - 1) {
240 let end = std::cmp::min(cur_line.len(), cur_lo.col.to_usize());
241 buf.push_str(&cur_line[..end]);
244 buf.push_str(&part.snippet);
245 prev_hi = cm.lookup_char_pos(part.span.hi());
246 prev_line = fm.get_line(prev_hi.line - 1);
248 let only_capitalization = is_case_difference(cm, &buf, bounding_span);
249 // if the replacement already ends with a newline, don't print the next line
250 if !buf.ends_with('\n') {
251 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
253 // remove trailing newlines
254 while buf.ends_with('\n') {
257 (buf, substitution.parts, only_capitalization)
262 /// Used as a return value to signify a fatal error occurred. (It is also
263 /// used as the argument to panic at the moment, but that will eventually
265 #[derive(Copy, Clone, Debug)]
267 pub struct FatalError;
269 pub struct FatalErrorMarker;
271 // Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
272 // We don't want to invoke the panic handler and print a backtrace for fatal errors.
273 impl !Send for FatalError {}
276 pub fn raise(self) -> ! {
277 panic::resume_unwind(Box::new(FatalErrorMarker))
281 impl fmt::Display for FatalError {
282 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283 write!(f, "parser fatal error")
287 impl error::Error for FatalError {
288 fn description(&self) -> &str {
289 "The parser has encountered a fatal error"
293 /// Signifies that the compiler died with an explicit call to `.bug`
294 /// or `.span_bug` rather than a failed assertion, etc.
295 #[derive(Copy, Clone, Debug)]
296 pub struct ExplicitBug;
298 impl fmt::Display for ExplicitBug {
299 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
300 write!(f, "parser internal bug")
304 impl error::Error for ExplicitBug {
305 fn description(&self) -> &str {
306 "The parser has encountered an internal bug"
310 pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
311 pub use diagnostic_builder::DiagnosticBuilder;
313 /// A handler deals with errors and other compiler output.
314 /// Certain errors (fatal, bug, unimpl) may cause immediate exit,
315 /// others log errors for later reporting.
318 inner: Lock<HandlerInner>,
321 /// This inner struct exists to keep it all behind a single lock;
322 /// this is done to prevent possible deadlocks in a multi-threaded compiler,
323 /// as well as inconsistent state observation.
324 struct HandlerInner {
326 /// The number of errors that have been emitted, including duplicates.
328 /// This is not necessarily the count that's reported to the user once
329 /// compilation ends.
331 deduplicated_err_count: usize,
332 emitter: Box<dyn Emitter + sync::Send>,
333 continue_after_error: bool,
334 delayed_span_bugs: Vec<Diagnostic>,
336 /// This set contains the `DiagnosticId` of all emitted diagnostics to avoid
337 /// emitting the same diagnostic with extended help (`--teach`) twice, which
338 /// would be uneccessary repetition.
339 taught_diagnostics: FxHashSet<DiagnosticId>,
341 /// Used to suggest rustc --explain <error code>
342 emitted_diagnostic_codes: FxHashSet<DiagnosticId>,
344 /// This set contains a hash of every diagnostic that has been emitted by
345 /// this handler. These hashes is used to avoid emitting the same error
347 emitted_diagnostics: FxHashSet<u128>,
349 /// Stashed diagnostics emitted in one stage of the compiler that may be
350 /// stolen by other stages (e.g. to improve them and add more information).
351 /// The stashed diagnostics count towards the total error count.
352 /// When `.abort_if_errors()` is called, these are also emitted.
353 stashed_diagnostics: FxIndexMap<(Span, StashKey), Diagnostic>,
356 /// A key denoting where from a diagnostic was stashed.
357 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
362 fn default_track_diagnostic(_: &Diagnostic) {}
364 thread_local!(pub static TRACK_DIAGNOSTICS: Cell<fn(&Diagnostic)> =
365 Cell::new(default_track_diagnostic));
367 #[derive(Copy, Clone, Default)]
368 pub struct HandlerFlags {
369 /// If false, warning-level lints are suppressed.
370 /// (rustc: see `--allow warnings` and `--cap-lints`)
371 pub can_emit_warnings: bool,
372 /// If true, error-level diagnostics are upgraded to bug-level.
373 /// (rustc: see `-Z treat-err-as-bug`)
374 pub treat_err_as_bug: Option<usize>,
375 /// If true, immediately emit diagnostics that would otherwise be buffered.
376 /// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
377 pub dont_buffer_diagnostics: bool,
378 /// If true, immediately print bugs registered with `delay_span_bug`.
379 /// (rustc: see `-Z report-delayed-bugs`)
380 pub report_delayed_bugs: bool,
381 /// show macro backtraces even for non-local macros.
382 /// (rustc: see `-Z external-macro-backtrace`)
383 pub external_macro_backtrace: bool,
386 impl Drop for HandlerInner {
388 self.emit_stashed_diagnostics();
390 if !self.has_errors() {
391 let bugs = std::mem::replace(&mut self.delayed_span_bugs, Vec::new());
392 let has_bugs = !bugs.is_empty();
394 self.emit_diagnostic(&bug);
397 panic!("no errors encountered even though `delay_span_bug` issued");
404 pub fn with_tty_emitter(
405 color_config: ColorConfig,
406 can_emit_warnings: bool,
407 treat_err_as_bug: Option<usize>,
408 cm: Option<Lrc<SourceMapperDyn>>,
410 Self::with_tty_emitter_and_flags(
416 .. Default::default()
421 pub fn with_tty_emitter_and_flags(
422 color_config: ColorConfig,
423 cm: Option<Lrc<SourceMapperDyn>>,
426 let emitter = Box::new(EmitterWriter::stderr(
432 flags.external_macro_backtrace,
434 Self::with_emitter_and_flags(emitter, flags)
438 can_emit_warnings: bool,
439 treat_err_as_bug: Option<usize>,
440 emitter: Box<dyn Emitter + sync::Send>,
442 Handler::with_emitter_and_flags(
447 .. Default::default()
452 pub fn with_emitter_and_flags(
453 emitter: Box<dyn Emitter + sync::Send>,
458 inner: Lock::new(HandlerInner {
461 deduplicated_err_count: 0,
463 continue_after_error: true,
464 delayed_span_bugs: Vec::new(),
465 taught_diagnostics: Default::default(),
466 emitted_diagnostic_codes: Default::default(),
467 emitted_diagnostics: Default::default(),
468 stashed_diagnostics: Default::default(),
473 pub fn set_continue_after_error(&self, continue_after_error: bool) {
474 self.inner.borrow_mut().continue_after_error = continue_after_error;
477 // This is here to not allow mutation of flags;
478 // as of this writing it's only used in tests in librustc.
479 pub fn can_emit_warnings(&self) -> bool {
480 self.flags.can_emit_warnings
483 /// Resets the diagnostic error count as well as the cached emitted diagnostics.
485 /// NOTE: *do not* call this function from rustc. It is only meant to be called from external
486 /// tools that want to reuse a `Parser` cleaning the previously emitted diagnostics as well as
487 /// the overall count of emitted error diagnostics.
488 pub fn reset_err_count(&self) {
489 let mut inner = self.inner.borrow_mut();
491 inner.deduplicated_err_count = 0;
493 // actually free the underlying memory (which `clear` would not do)
494 inner.delayed_span_bugs = Default::default();
495 inner.taught_diagnostics = Default::default();
496 inner.emitted_diagnostic_codes = Default::default();
497 inner.emitted_diagnostics = Default::default();
498 inner.stashed_diagnostics = Default::default();
501 /// Stash a given diagnostic with the given `Span` and `StashKey` as the key for later stealing.
502 /// If the diagnostic with this `(span, key)` already exists, this will result in an ICE.
503 pub fn stash_diagnostic(&self, span: Span, key: StashKey, diag: Diagnostic) {
504 let mut inner = self.inner.borrow_mut();
505 if let Some(mut old_diag) = inner.stashed_diagnostics.insert((span, key), diag) {
506 // We are removing a previously stashed diagnostic which should not happen.
507 old_diag.level = Bug;
508 old_diag.note(&format!(
509 "{}:{}: already existing stashed diagnostic with (span = {:?}, key = {:?})",
510 file!(), line!(), span, key
512 inner.emit_diag_at_span(old_diag, span);
517 /// Steal a previously stashed diagnostic with the given `Span` and `StashKey` as the key.
518 pub fn steal_diagnostic(&self, span: Span, key: StashKey) -> Option<DiagnosticBuilder<'_>> {
522 .remove(&(span, key))
523 .map(|diag| DiagnosticBuilder::new_diagnostic(self, diag))
526 /// Emit all stashed diagnostics.
527 pub fn emit_stashed_diagnostics(&self) {
528 self.inner.borrow_mut().emit_stashed_diagnostics();
531 /// Construct a dummy builder with `Level::Cancelled`.
533 /// Using this will neither report anything to the user (e.g. a warning),
534 /// nor will compilation cancel as a result.
535 pub fn struct_dummy(&self) -> DiagnosticBuilder<'_> {
536 DiagnosticBuilder::new(self, Level::Cancelled, "")
539 /// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
540 pub fn struct_span_warn(&self, span: impl Into<MultiSpan>, msg: &str) -> DiagnosticBuilder<'_> {
541 let mut result = self.struct_warn(msg);
542 result.set_span(span);
546 /// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
547 /// Also include a code.
548 pub fn struct_span_warn_with_code(
550 span: impl Into<MultiSpan>,
553 ) -> DiagnosticBuilder<'_> {
554 let mut result = self.struct_span_warn(span, msg);
559 /// Construct a builder at the `Warning` level with the `msg`.
560 pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
561 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
562 if !self.flags.can_emit_warnings {
568 /// Construct a builder at the `Error` level at the given `span` and with the `msg`.
569 pub fn struct_span_err(&self, span: impl Into<MultiSpan>, msg: &str) -> DiagnosticBuilder<'_> {
570 let mut result = self.struct_err(msg);
571 result.set_span(span);
575 /// Construct a builder at the `Error` level at the given `span`, with the `msg`, and `code`.
576 pub fn struct_span_err_with_code(
578 span: impl Into<MultiSpan>,
581 ) -> DiagnosticBuilder<'_> {
582 let mut result = self.struct_span_err(span, msg);
587 /// Construct a builder at the `Error` level with the `msg`.
588 // FIXME: This method should be removed (every error should have an associated error code).
589 pub fn struct_err(&self, msg: &str) -> DiagnosticBuilder<'_> {
590 DiagnosticBuilder::new(self, Level::Error, msg)
593 /// Construct a builder at the `Error` level with the `msg` and the `code`.
594 pub fn struct_err_with_code(&self, msg: &str, code: DiagnosticId) -> DiagnosticBuilder<'_> {
595 let mut result = self.struct_err(msg);
600 /// Construct a builder at the `Fatal` level at the given `span` and with the `msg`.
601 pub fn struct_span_fatal(
603 span: impl Into<MultiSpan>,
605 ) -> DiagnosticBuilder<'_> {
606 let mut result = self.struct_fatal(msg);
607 result.set_span(span);
611 /// Construct a builder at the `Fatal` level at the given `span`, with the `msg`, and `code`.
612 pub fn struct_span_fatal_with_code(
614 span: impl Into<MultiSpan>,
617 ) -> DiagnosticBuilder<'_> {
618 let mut result = self.struct_span_fatal(span, msg);
623 /// Construct a builder at the `Error` level with the `msg`.
624 pub fn struct_fatal(&self, msg: &str) -> DiagnosticBuilder<'_> {
625 DiagnosticBuilder::new(self, Level::Fatal, msg)
628 pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> FatalError {
629 self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
633 pub fn span_fatal_with_code(
635 span: impl Into<MultiSpan>,
639 self.emit_diag_at_span(Diagnostic::new_with_code(Fatal, Some(code), msg), span);
643 pub fn span_err(&self, span: impl Into<MultiSpan>, msg: &str) {
644 self.emit_diag_at_span(Diagnostic::new(Error, msg), span);
647 pub fn span_err_with_code(&self, span: impl Into<MultiSpan>, msg: &str, code: DiagnosticId) {
648 self.emit_diag_at_span(Diagnostic::new_with_code(Error, Some(code), msg), span);
651 pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: &str) {
652 self.emit_diag_at_span(Diagnostic::new(Warning, msg), span);
655 pub fn span_warn_with_code(&self, span: impl Into<MultiSpan>, msg: &str, code: DiagnosticId) {
656 self.emit_diag_at_span(Diagnostic::new_with_code(Warning, Some(code), msg), span);
659 pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: &str) -> ! {
660 self.inner.borrow_mut().span_bug(span, msg)
663 pub fn delay_span_bug(&self, span: impl Into<MultiSpan>, msg: &str) {
664 self.inner.borrow_mut().delay_span_bug(span, msg)
667 pub fn span_bug_no_panic(&self, span: impl Into<MultiSpan>, msg: &str) {
668 self.emit_diag_at_span(Diagnostic::new(Bug, msg), span);
671 pub fn span_note_without_error(&self, span: impl Into<MultiSpan>, msg: &str) {
672 self.emit_diag_at_span(Diagnostic::new(Note, msg), span);
675 pub fn span_note_diag(&self, span: Span, msg: &str) -> DiagnosticBuilder<'_> {
676 let mut db = DiagnosticBuilder::new(self, Note, msg);
681 pub fn failure(&self, msg: &str) {
682 self.inner.borrow_mut().failure(msg);
685 pub fn fatal(&self, msg: &str) -> FatalError {
686 self.inner.borrow_mut().fatal(msg)
689 pub fn err(&self, msg: &str) {
690 self.inner.borrow_mut().err(msg);
693 pub fn warn(&self, msg: &str) {
694 let mut db = DiagnosticBuilder::new(self, Warning, msg);
698 pub fn note_without_error(&self, msg: &str) {
699 DiagnosticBuilder::new(self, Note, msg).emit();
702 pub fn bug(&self, msg: &str) -> ! {
703 self.inner.borrow_mut().bug(msg)
706 pub fn err_count(&self) -> usize {
707 self.inner.borrow().err_count()
710 pub fn has_errors(&self) -> bool {
711 self.inner.borrow().has_errors()
713 pub fn has_errors_or_delayed_span_bugs(&self) -> bool {
714 self.inner.borrow().has_errors_or_delayed_span_bugs()
717 pub fn print_error_count(&self, registry: &Registry) {
718 self.inner.borrow_mut().print_error_count(registry)
721 pub fn abort_if_errors(&self) {
722 self.inner.borrow_mut().abort_if_errors()
725 pub fn abort_if_errors_and_should_abort(&self) {
726 self.inner.borrow_mut().abort_if_errors_and_should_abort()
729 /// `true` if we haven't taught a diagnostic with this code already.
730 /// The caller must then teach the user about such a diagnostic.
732 /// Used to suppress emitting the same error multiple times with extended explanation when
733 /// calling `-Zteach`.
734 pub fn must_teach(&self, code: &DiagnosticId) -> bool {
735 self.inner.borrow_mut().must_teach(code)
738 pub fn force_print_diagnostic(&self, db: Diagnostic) {
739 self.inner.borrow_mut().force_print_diagnostic(db)
742 pub fn emit_diagnostic(&self, diagnostic: &Diagnostic) {
743 self.inner.borrow_mut().emit_diagnostic(diagnostic)
746 fn emit_diag_at_span(&self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
747 let mut inner = self.inner.borrow_mut();
748 inner.emit_diagnostic(diag.set_span(sp));
749 inner.abort_if_errors_and_should_abort();
752 pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
753 self.inner.borrow_mut().emit_artifact_notification(path, artifact_type)
756 pub fn delay_as_bug(&self, diagnostic: Diagnostic) {
757 self.inner.borrow_mut().delay_as_bug(diagnostic)
762 fn must_teach(&mut self, code: &DiagnosticId) -> bool {
763 self.taught_diagnostics.insert(code.clone())
766 fn force_print_diagnostic(&mut self, db: Diagnostic) {
767 self.emitter.emit_diagnostic(&db);
770 /// Emit all stashed diagnostics.
771 fn emit_stashed_diagnostics(&mut self) {
772 let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
773 diags.iter().for_each(|diag| self.emit_diagnostic(diag));
776 fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) {
777 if diagnostic.cancelled() {
781 if diagnostic.level == Warning && !self.flags.can_emit_warnings {
785 TRACK_DIAGNOSTICS.with(|track_diagnostics| {
786 track_diagnostics.get()(diagnostic);
789 if let Some(ref code) = diagnostic.code {
790 self.emitted_diagnostic_codes.insert(code.clone());
793 let diagnostic_hash = {
795 let mut hasher = StableHasher::new();
796 diagnostic.hash(&mut hasher);
800 // Only emit the diagnostic if we haven't already emitted an equivalent
802 if self.emitted_diagnostics.insert(diagnostic_hash) {
803 self.emitter.emit_diagnostic(diagnostic);
804 if diagnostic.is_error() {
805 self.deduplicated_err_count += 1;
808 if diagnostic.is_error() {
809 self.bump_err_count();
813 fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
814 self.emitter.emit_artifact_notification(path, artifact_type);
817 fn treat_err_as_bug(&self) -> bool {
818 self.flags.treat_err_as_bug.map(|c| self.err_count() >= c).unwrap_or(false)
821 fn print_error_count(&mut self, registry: &Registry) {
822 self.emit_stashed_diagnostics();
824 let s = match self.deduplicated_err_count {
826 1 => "aborting due to previous error".to_string(),
827 count => format!("aborting due to {} previous errors", count)
829 if self.treat_err_as_bug() {
833 let _ = self.fatal(&s);
835 let can_show_explain = self.emitter.should_show_explain();
836 let are_there_diagnostics = !self.emitted_diagnostic_codes.is_empty();
837 if can_show_explain && are_there_diagnostics {
838 let mut error_codes = self
839 .emitted_diagnostic_codes
841 .filter_map(|x| match &x {
842 DiagnosticId::Error(s) if registry.find_description(s).is_some() => {
847 .collect::<Vec<_>>();
848 if !error_codes.is_empty() {
850 if error_codes.len() > 1 {
851 let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
852 self.failure(&format!("Some errors have detailed explanations: {}{}",
853 error_codes[..limit].join(", "),
854 if error_codes.len() > 9 { "..." } else { "." }));
855 self.failure(&format!("For more information about an error, try \
856 `rustc --explain {}`.",
859 self.failure(&format!("For more information about this error, try \
860 `rustc --explain {}`.",
867 fn err_count(&self) -> usize {
868 self.err_count + self.stashed_diagnostics.len()
871 fn has_errors(&self) -> bool {
874 fn has_errors_or_delayed_span_bugs(&self) -> bool {
875 self.has_errors() || !self.delayed_span_bugs.is_empty()
878 fn abort_if_errors_and_should_abort(&mut self) {
879 self.emit_stashed_diagnostics();
881 if self.has_errors() && !self.continue_after_error {
886 fn abort_if_errors(&mut self) {
887 self.emit_stashed_diagnostics();
889 if self.has_errors() {
894 fn span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) -> ! {
895 self.emit_diag_at_span(Diagnostic::new(Bug, msg), sp);
899 fn emit_diag_at_span(&mut self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
900 self.emit_diagnostic(diag.set_span(sp));
901 self.abort_if_errors_and_should_abort();
904 fn delay_span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) {
905 if self.treat_err_as_bug() {
906 // FIXME: don't abort here if report_delayed_bugs is off
907 self.span_bug(sp, msg);
909 let mut diagnostic = Diagnostic::new(Level::Bug, msg);
910 diagnostic.set_span(sp.into());
911 self.delay_as_bug(diagnostic)
914 fn failure(&mut self, msg: &str) {
915 self.emit_diagnostic(&Diagnostic::new(FailureNote, msg));
918 fn fatal(&mut self, msg: &str) -> FatalError {
919 self.emit_error(Fatal, msg);
923 fn err(&mut self, msg: &str) {
924 self.emit_error(Error, msg);
927 /// Emit an error; level should be `Error` or `Fatal`.
928 fn emit_error(&mut self, level: Level, msg: &str,) {
929 if self.treat_err_as_bug() {
932 self.emit_diagnostic(&Diagnostic::new(level, msg));
935 fn bug(&mut self, msg: &str) -> ! {
936 self.emit_diagnostic(&Diagnostic::new(Bug, msg));
940 fn delay_as_bug(&mut self, diagnostic: Diagnostic) {
941 if self.flags.report_delayed_bugs {
942 self.emit_diagnostic(&diagnostic);
944 self.delayed_span_bugs.push(diagnostic);
947 fn bump_err_count(&mut self) {
949 self.panic_if_treat_err_as_bug();
952 fn panic_if_treat_err_as_bug(&self) {
953 if self.treat_err_as_bug() {
954 let s = match (self.err_count(), self.flags.treat_err_as_bug.unwrap_or(0)) {
956 (1, 1) => "aborting due to `-Z treat-err-as-bug=1`".to_string(),
960 "aborting after {} errors due to `-Z treat-err-as-bug={}`",
971 #[derive(Copy, PartialEq, Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
983 impl fmt::Display for Level {
984 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
990 fn color(self) -> ColorSpec {
991 let mut spec = ColorSpec::new();
993 Bug | Fatal | Error => {
994 spec.set_fg(Some(Color::Red))
998 spec.set_fg(Some(Color::Yellow))
999 .set_intense(cfg!(windows));
1002 spec.set_fg(Some(Color::Green))
1006 spec.set_fg(Some(Color::Cyan))
1010 Cancelled => unreachable!(),
1015 pub fn to_str(self) -> &'static str {
1017 Bug => "error: internal compiler error",
1018 Fatal | Error => "error",
1019 Warning => "warning",
1022 FailureNote => "failure-note",
1023 Cancelled => panic!("Shouldn't call on cancelled error"),
1027 pub fn is_failure_note(&self) -> bool {
1029 FailureNote => true,
1036 macro_rules! pluralize {
1038 if $x != 1 { "s" } else { "" }