1 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
2 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
3 html_root_url = "https://doc.rust-lang.org/nightly/")]
5 #![feature(custom_attribute)]
6 #![allow(unused_attributes)]
7 #![feature(range_contains)]
8 #![cfg_attr(unix, feature(libc))]
10 #![feature(optin_builtin_traits)]
13 extern crate termcolor;
18 extern crate rustc_data_structures;
19 extern crate serialize as rustc_serialize;
20 extern crate syntax_pos;
21 extern crate unicode_width;
23 pub use emitter::ColorConfig;
27 use emitter::{Emitter, EmitterWriter};
29 use rustc_data_structures::sync::{self, Lrc, Lock, AtomicUsize, AtomicBool, SeqCst};
30 use rustc_data_structures::fx::FxHashSet;
31 use rustc_data_structures::stable_hasher::StableHasher;
35 use std::{error, fmt};
38 use termcolor::{ColorSpec, Color};
41 mod diagnostic_builder;
48 use syntax_pos::{BytePos,
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(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
82 pub struct CodeSuggestion {
83 /// Each substitute can have multiple variants due to multiple
84 /// applicable suggestions
86 /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
87 /// `foo` and `bar` on their own:
91 /// Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
92 /// Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
96 /// or by replacing the entire span:
100 /// Substitution { parts: vec![(0..7, "a.b")] },
101 /// Substitution { parts: vec![(0..7, "x.y")] },
104 pub substitutions: Vec<Substitution>,
106 pub show_code_when_inline: bool,
107 /// Whether or not the suggestion is approximate
109 /// Sometimes we may show suggestions with placeholders,
110 /// which are useful for users but not useful for
111 /// tools like rustfix
112 pub applicability: Applicability,
115 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
116 /// See the docs on `CodeSuggestion::substitutions`
117 pub struct Substitution {
118 pub parts: Vec<SubstitutionPart>,
121 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
122 pub struct SubstitutionPart {
127 pub type SourceMapperDyn = dyn SourceMapper + sync::Send + sync::Sync;
129 pub trait SourceMapper {
130 fn lookup_char_pos(&self, pos: BytePos) -> Loc;
131 fn span_to_lines(&self, sp: Span) -> FileLinesResult;
132 fn span_to_string(&self, sp: Span) -> String;
133 fn span_to_filename(&self, sp: Span) -> FileName;
134 fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
135 fn call_span_if_macro(&self, sp: Span) -> Span;
136 fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool;
137 fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize;
140 impl CodeSuggestion {
141 /// Returns the assembled code suggestions and whether they should be shown with an underline.
142 pub fn splice_lines(&self, cm: &SourceMapperDyn)
143 -> Vec<(String, Vec<SubstitutionPart>)> {
144 use syntax_pos::{CharPos, Loc, Pos};
146 fn push_trailing(buf: &mut String,
147 line_opt: Option<&Cow<str>>,
149 hi_opt: Option<&Loc>) {
150 let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
151 if let Some(line) = line_opt {
152 if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
153 let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
155 Some(hi) if hi > lo => buf.push_str(&line[lo..hi]),
157 None => buf.push_str(&line[lo..]),
160 if let None = hi_opt {
166 assert!(!self.substitutions.is_empty());
168 self.substitutions.iter().cloned().map(|mut substitution| {
169 // Assumption: all spans are in the same file, and all spans
170 // are disjoint. Sort in ascending order.
171 substitution.parts.sort_by_key(|part| part.span.lo());
173 // Find the bounding span.
174 let lo = substitution.parts.iter().map(|part| part.span.lo()).min().unwrap();
175 let hi = substitution.parts.iter().map(|part| part.span.hi()).min().unwrap();
176 let bounding_span = Span::new(lo, hi, NO_EXPANSION);
177 let lines = cm.span_to_lines(bounding_span).unwrap();
178 assert!(!lines.lines.is_empty());
180 // To build up the result, we do this for each span:
181 // - push the line segment trailing the previous span
182 // (at the beginning a "phantom" span pointing at the start of the line)
183 // - push lines between the previous and current span (if any)
184 // - if the previous and current span are not on the same line
185 // push the line segment leading up to the current span
186 // - splice in the span substitution
188 // Finally push the trailing line segment of the last span
189 let fm = &lines.file;
190 let mut prev_hi = cm.lookup_char_pos(bounding_span.lo());
191 prev_hi.col = CharPos::from_usize(0);
193 let mut prev_line = fm.get_line(lines.lines[0].line_index);
194 let mut buf = String::new();
196 for part in &substitution.parts {
197 let cur_lo = cm.lookup_char_pos(part.span.lo());
198 if prev_hi.line == cur_lo.line {
199 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
201 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
202 // push lines between the previous and current span (if any)
203 for idx in prev_hi.line..(cur_lo.line - 1) {
204 if let Some(line) = fm.get_line(idx) {
205 buf.push_str(line.as_ref());
209 if let Some(cur_line) = fm.get_line(cur_lo.line - 1) {
210 buf.push_str(&cur_line[..cur_lo.col.to_usize()]);
213 buf.push_str(&part.snippet);
214 prev_hi = cm.lookup_char_pos(part.span.hi());
215 prev_line = fm.get_line(prev_hi.line - 1);
217 // if the replacement already ends with a newline, don't print the next line
218 if !buf.ends_with('\n') {
219 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
221 // remove trailing newlines
222 while buf.ends_with('\n') {
225 (buf, substitution.parts)
230 /// Used as a return value to signify a fatal error occurred. (It is also
231 /// used as the argument to panic at the moment, but that will eventually
233 #[derive(Copy, Clone, Debug)]
235 pub struct FatalError;
237 pub struct FatalErrorMarker;
239 // Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
240 // We don't want to invoke the panic handler and print a backtrace for fatal errors.
241 impl !Send for FatalError {}
244 pub fn raise(self) -> ! {
245 panic::resume_unwind(Box::new(FatalErrorMarker))
249 impl fmt::Display for FatalError {
250 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
251 write!(f, "parser fatal error")
255 impl error::Error for FatalError {
256 fn description(&self) -> &str {
257 "The parser has encountered a fatal error"
261 /// Signifies that the compiler died with an explicit call to `.bug`
262 /// or `.span_bug` rather than a failed assertion, etc.
263 #[derive(Copy, Clone, Debug)]
264 pub struct ExplicitBug;
266 impl fmt::Display for ExplicitBug {
267 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
268 write!(f, "parser internal bug")
272 impl error::Error for ExplicitBug {
273 fn description(&self) -> &str {
274 "The parser has encountered an internal bug"
278 pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
279 pub use diagnostic_builder::DiagnosticBuilder;
281 /// A handler deals with errors; certain errors
282 /// (fatal, bug, unimpl) may cause immediate exit,
283 /// others log errors for later reporting.
285 pub flags: HandlerFlags,
287 err_count: AtomicUsize,
288 emitter: Lock<Box<dyn Emitter + sync::Send>>,
289 continue_after_error: AtomicBool,
290 delayed_span_bugs: Lock<Vec<Diagnostic>>,
292 // This set contains the `DiagnosticId` of all emitted diagnostics to avoid
293 // emitting the same diagnostic with extended help (`--teach`) twice, which
294 // would be uneccessary repetition.
295 taught_diagnostics: Lock<FxHashSet<DiagnosticId>>,
297 /// Used to suggest rustc --explain <error code>
298 emitted_diagnostic_codes: Lock<FxHashSet<DiagnosticId>>,
300 // This set contains a hash of every diagnostic that has been emitted by
301 // this handler. These hashes is used to avoid emitting the same error
303 emitted_diagnostics: Lock<FxHashSet<u128>>,
306 fn default_track_diagnostic(_: &Diagnostic) {}
308 thread_local!(pub static TRACK_DIAGNOSTICS: Cell<fn(&Diagnostic)> =
309 Cell::new(default_track_diagnostic));
312 pub struct HandlerFlags {
313 /// If false, warning-level lints are suppressed.
314 /// (rustc: see `--allow warnings` and `--cap-lints`)
315 pub can_emit_warnings: bool,
316 /// If true, error-level diagnostics are upgraded to bug-level.
317 /// (rustc: see `-Z treat-err-as-bug`)
318 pub treat_err_as_bug: bool,
319 /// If true, immediately emit diagnostics that would otherwise be buffered.
320 /// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
321 pub dont_buffer_diagnostics: bool,
322 /// If true, immediately print bugs registered with `delay_span_bug`.
323 /// (rustc: see `-Z report-delayed-bugs`)
324 pub report_delayed_bugs: bool,
325 /// show macro backtraces even for non-local macros.
326 /// (rustc: see `-Z external-macro-backtrace`)
327 pub external_macro_backtrace: bool,
330 impl Drop for Handler {
332 if self.err_count() == 0 {
333 let mut bugs = self.delayed_span_bugs.borrow_mut();
334 let has_bugs = !bugs.is_empty();
335 for bug in bugs.drain(..) {
336 DiagnosticBuilder::new_diagnostic(self, bug).emit();
339 panic!("no errors encountered even though `delay_span_bug` issued");
346 pub fn with_tty_emitter(color_config: ColorConfig,
347 can_emit_warnings: bool,
348 treat_err_as_bug: bool,
349 cm: Option<Lrc<SourceMapperDyn>>)
351 Handler::with_tty_emitter_and_flags(
357 .. Default::default()
361 pub fn with_tty_emitter_and_flags(color_config: ColorConfig,
362 cm: Option<Lrc<SourceMapperDyn>>,
365 let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false));
366 Handler::with_emitter_and_flags(emitter, flags)
369 pub fn with_emitter(can_emit_warnings: bool,
370 treat_err_as_bug: bool,
371 e: Box<dyn Emitter + sync::Send>)
373 Handler::with_emitter_and_flags(
378 .. Default::default()
382 pub fn with_emitter_and_flags(e: Box<dyn Emitter + sync::Send>, flags: HandlerFlags) -> Handler
386 err_count: AtomicUsize::new(0),
387 emitter: Lock::new(e),
388 continue_after_error: AtomicBool::new(true),
389 delayed_span_bugs: Lock::new(Vec::new()),
390 taught_diagnostics: Default::default(),
391 emitted_diagnostic_codes: Default::default(),
392 emitted_diagnostics: Default::default(),
396 pub fn set_continue_after_error(&self, continue_after_error: bool) {
397 self.continue_after_error.store(continue_after_error, SeqCst);
400 /// Resets the diagnostic error count as well as the cached emitted diagnostics.
402 /// NOTE: DO NOT call this function from rustc. It is only meant to be called from external
403 /// tools that want to reuse a `Parser` cleaning the previously emitted diagnostics as well as
404 /// the overall count of emitted error diagnostics.
405 pub fn reset_err_count(&self) {
406 // actually frees the underlying memory (which `clear` would not do)
407 *self.emitted_diagnostics.borrow_mut() = Default::default();
408 self.err_count.store(0, SeqCst);
411 pub fn struct_dummy<'a>(&'a self) -> DiagnosticBuilder<'a> {
412 DiagnosticBuilder::new(self, Level::Cancelled, "")
415 pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
418 -> DiagnosticBuilder<'a> {
419 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
421 if !self.flags.can_emit_warnings {
426 pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
430 -> DiagnosticBuilder<'a> {
431 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
434 if !self.flags.can_emit_warnings {
439 pub fn struct_warn<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
440 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
441 if !self.flags.can_emit_warnings {
446 pub fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
449 -> DiagnosticBuilder<'a> {
450 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
454 pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
458 -> DiagnosticBuilder<'a> {
459 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
464 // FIXME: This method should be removed (every error should have an associated error code).
465 pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
466 DiagnosticBuilder::new(self, Level::Error, msg)
468 pub fn struct_err_with_code<'a>(
472 ) -> DiagnosticBuilder<'a> {
473 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
477 pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
480 -> DiagnosticBuilder<'a> {
481 let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
485 pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
489 -> DiagnosticBuilder<'a> {
490 let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
495 pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
496 DiagnosticBuilder::new(self, Level::Fatal, msg)
499 pub fn cancel(&self, err: &mut DiagnosticBuilder) {
503 fn panic_if_treat_err_as_bug(&self) {
504 if self.flags.treat_err_as_bug {
505 panic!("encountered error with `-Z treat_err_as_bug");
509 pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> FatalError {
510 self.emit(&sp.into(), msg, Fatal);
513 pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
518 self.emit_with_code(&sp.into(), msg, code, Fatal);
521 pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
522 self.emit(&sp.into(), msg, Error);
524 pub fn mut_span_err<'a, S: Into<MultiSpan>>(&'a self,
527 -> DiagnosticBuilder<'a> {
528 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
532 pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
533 self.emit_with_code(&sp.into(), msg, code, Error);
535 pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
536 self.emit(&sp.into(), msg, Warning);
538 pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
539 self.emit_with_code(&sp.into(), msg, code, Warning);
541 pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
542 self.emit(&sp.into(), msg, Bug);
545 pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
546 if self.flags.treat_err_as_bug {
547 // FIXME: don't abort here if report_delayed_bugs is off
548 self.span_bug(sp, msg);
550 let mut diagnostic = Diagnostic::new(Level::Bug, msg);
551 diagnostic.set_span(sp.into());
552 self.delay_as_bug(diagnostic);
554 fn delay_as_bug(&self, diagnostic: Diagnostic) {
555 if self.flags.report_delayed_bugs {
556 DiagnosticBuilder::new_diagnostic(self, diagnostic.clone()).emit();
558 self.delayed_span_bugs.borrow_mut().push(diagnostic);
560 pub fn span_bug_no_panic<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
561 self.emit(&sp.into(), msg, Bug);
563 pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
564 self.emit(&sp.into(), msg, Note);
566 pub fn span_note_diag<'a>(&'a self,
569 -> DiagnosticBuilder<'a> {
570 let mut db = DiagnosticBuilder::new(self, Note, msg);
574 pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
575 self.span_bug(sp, &format!("unimplemented {}", msg));
577 pub fn failure(&self, msg: &str) {
578 DiagnosticBuilder::new(self, FailureNote, msg).emit()
580 pub fn fatal(&self, msg: &str) -> FatalError {
581 if self.flags.treat_err_as_bug {
584 DiagnosticBuilder::new(self, Fatal, msg).emit();
587 pub fn err(&self, msg: &str) {
588 if self.flags.treat_err_as_bug {
591 let mut db = DiagnosticBuilder::new(self, Error, msg);
594 pub fn warn(&self, msg: &str) {
595 let mut db = DiagnosticBuilder::new(self, Warning, msg);
598 pub fn note_without_error(&self, msg: &str) {
599 let mut db = DiagnosticBuilder::new(self, Note, msg);
602 pub fn bug(&self, msg: &str) -> ! {
603 let mut db = DiagnosticBuilder::new(self, Bug, msg);
607 pub fn unimpl(&self, msg: &str) -> ! {
608 self.bug(&format!("unimplemented {}", msg));
611 fn bump_err_count(&self) {
612 self.panic_if_treat_err_as_bug();
613 self.err_count.fetch_add(1, SeqCst);
616 pub fn err_count(&self) -> usize {
617 self.err_count.load(SeqCst)
620 pub fn has_errors(&self) -> bool {
624 pub fn print_error_count(&self) {
625 let s = match self.err_count() {
627 1 => "aborting due to previous error".to_string(),
628 _ => format!("aborting due to {} previous errors", self.err_count())
631 let _ = self.fatal(&s);
633 let can_show_explain = self.emitter.borrow().should_show_explain();
634 let are_there_diagnostics = !self.emitted_diagnostic_codes.borrow().is_empty();
635 if can_show_explain && are_there_diagnostics {
636 let mut error_codes =
637 self.emitted_diagnostic_codes.borrow()
639 .filter_map(|x| match *x {
640 DiagnosticId::Error(ref s) => Some(s.clone()),
643 .collect::<Vec<_>>();
644 if !error_codes.is_empty() {
646 if error_codes.len() > 1 {
647 let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
648 self.failure(&format!("Some errors occurred: {}{}",
649 error_codes[..limit].join(", "),
650 if error_codes.len() > 9 { "..." } else { "." }));
651 self.failure(&format!("For more information about an error, try \
652 `rustc --explain {}`.",
655 self.failure(&format!("For more information about this error, try \
656 `rustc --explain {}`.",
663 pub fn abort_if_errors(&self) {
664 if self.err_count() == 0 {
669 pub fn emit(&self, msp: &MultiSpan, msg: &str, lvl: Level) {
670 if lvl == Warning && !self.flags.can_emit_warnings {
673 let mut db = DiagnosticBuilder::new(self, lvl, msg);
674 db.set_span(msp.clone());
676 if !self.continue_after_error.load(SeqCst) {
677 self.abort_if_errors();
680 pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: DiagnosticId, lvl: Level) {
681 if lvl == Warning && !self.flags.can_emit_warnings {
684 let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code), msg);
685 db.set_span(msp.clone());
687 if !self.continue_after_error.load(SeqCst) {
688 self.abort_if_errors();
692 /// `true` if we haven't taught a diagnostic with this code already.
693 /// The caller must then teach the user about such a diagnostic.
695 /// Used to suppress emitting the same error multiple times with extended explanation when
696 /// calling `-Zteach`.
697 pub fn must_teach(&self, code: &DiagnosticId) -> bool {
698 self.taught_diagnostics.borrow_mut().insert(code.clone())
701 pub fn force_print_db(&self, mut db: DiagnosticBuilder) {
702 self.emitter.borrow_mut().emit(&db);
706 fn emit_db(&self, db: &DiagnosticBuilder) {
707 let diagnostic = &**db;
709 TRACK_DIAGNOSTICS.with(|track_diagnostics| {
710 track_diagnostics.get()(diagnostic);
713 if let Some(ref code) = diagnostic.code {
714 self.emitted_diagnostic_codes.borrow_mut().insert(code.clone());
717 let diagnostic_hash = {
719 let mut hasher = StableHasher::new();
720 diagnostic.hash(&mut hasher);
724 // Only emit the diagnostic if we haven't already emitted an equivalent
726 if self.emitted_diagnostics.borrow_mut().insert(diagnostic_hash) {
727 self.emitter.borrow_mut().emit(db);
729 self.bump_err_count();
736 #[derive(Copy, PartialEq, Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
740 // An error which while not immediately fatal, should stop the compiler
741 // progressing beyond the current phase.
751 impl fmt::Display for Level {
752 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
758 fn color(self) -> ColorSpec {
759 let mut spec = ColorSpec::new();
761 Bug | Fatal | PhaseFatal | Error => {
762 spec.set_fg(Some(Color::Red))
766 spec.set_fg(Some(Color::Yellow))
767 .set_intense(cfg!(windows));
770 spec.set_fg(Some(Color::Green))
774 spec.set_fg(Some(Color::Cyan))
778 Cancelled => unreachable!(),
783 pub fn to_str(self) -> &'static str {
785 Bug => "error: internal compiler error",
786 Fatal | PhaseFatal | Error => "error",
787 Warning => "warning",
791 Cancelled => panic!("Shouldn't call on cancelled error"),
795 pub fn is_failure_note(&self) -> bool {