1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
12 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
13 html_root_url = "https://doc.rust-lang.org/nightly/")]
16 #![feature(custom_attribute)]
17 #![allow(unused_attributes)]
18 #![feature(range_contains)]
19 #![cfg_attr(unix, feature(libc))]
20 #![feature(conservative_impl_trait)]
21 #![feature(i128_type)]
22 #![feature(optin_builtin_traits)]
27 extern crate rustc_data_structures;
28 extern crate serialize as rustc_serialize;
29 extern crate syntax_pos;
30 extern crate unicode_width;
32 pub use emitter::ColorConfig;
36 use emitter::{Emitter, EmitterWriter};
38 use rustc_data_structures::fx::FxHashSet;
39 use rustc_data_structures::stable_hasher::StableHasher;
42 use std::cell::{RefCell, Cell};
45 use std::{error, fmt};
46 use std::sync::atomic::AtomicUsize;
47 use std::sync::atomic::Ordering::SeqCst;
51 mod diagnostic_builder;
58 use syntax_pos::{BytePos, Loc, FileLinesResult, FileMap, FileName, MultiSpan, Span, NO_EXPANSION};
60 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
61 pub struct CodeSuggestion {
62 /// Each substitute can have multiple variants due to multiple
63 /// applicable suggestions
65 /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
66 /// `foo` and `bar` on their own:
70 /// Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
71 /// Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
75 /// or by replacing the entire span:
79 /// Substitution { parts: vec![(0..7, "a.b")] },
80 /// Substitution { parts: vec![(0..7, "x.y")] },
83 pub substitutions: Vec<Substitution>,
85 pub show_code_when_inline: bool,
88 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
89 /// See the docs on `CodeSuggestion::substitutions`
90 pub struct Substitution {
91 pub parts: Vec<SubstitutionPart>,
94 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
95 pub struct SubstitutionPart {
100 pub trait CodeMapper {
101 fn lookup_char_pos(&self, pos: BytePos) -> Loc;
102 fn span_to_lines(&self, sp: Span) -> FileLinesResult;
103 fn span_to_string(&self, sp: Span) -> String;
104 fn span_to_filename(&self, sp: Span) -> FileName;
105 fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
106 fn call_span_if_macro(&self, sp: Span) -> Span;
107 fn ensure_filemap_source_present(&self, file_map: Rc<FileMap>) -> bool;
108 fn doctest_offset_line(&self, line: usize) -> usize;
111 impl CodeSuggestion {
112 /// Returns the assembled code suggestions and whether they should be shown with an underline.
113 pub fn splice_lines(&self, cm: &CodeMapper) -> Vec<(String, Vec<SubstitutionPart>)> {
114 use syntax_pos::{CharPos, Loc, Pos};
116 fn push_trailing(buf: &mut String,
117 line_opt: Option<&Cow<str>>,
119 hi_opt: Option<&Loc>) {
120 let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
121 if let Some(line) = line_opt {
122 if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
123 let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
124 buf.push_str(match hi_opt {
125 Some(hi) => &line[lo..hi],
129 if let None = hi_opt {
135 assert!(!self.substitutions.is_empty());
137 self.substitutions.iter().cloned().map(|mut substitution| {
138 // Assumption: all spans are in the same file, and all spans
139 // are disjoint. Sort in ascending order.
140 substitution.parts.sort_by_key(|part| part.span.lo());
142 // Find the bounding span.
143 let lo = substitution.parts.iter().map(|part| part.span.lo()).min().unwrap();
144 let hi = substitution.parts.iter().map(|part| part.span.hi()).min().unwrap();
145 let bounding_span = Span::new(lo, hi, NO_EXPANSION);
146 let lines = cm.span_to_lines(bounding_span).unwrap();
147 assert!(!lines.lines.is_empty());
149 // To build up the result, we do this for each span:
150 // - push the line segment trailing the previous span
151 // (at the beginning a "phantom" span pointing at the start of the line)
152 // - push lines between the previous and current span (if any)
153 // - if the previous and current span are not on the same line
154 // push the line segment leading up to the current span
155 // - splice in the span substitution
157 // Finally push the trailing line segment of the last span
158 let fm = &lines.file;
159 let mut prev_hi = cm.lookup_char_pos(bounding_span.lo());
160 prev_hi.col = CharPos::from_usize(0);
162 let mut prev_line = fm.get_line(lines.lines[0].line_index);
163 let mut buf = String::new();
165 for part in &substitution.parts {
166 let cur_lo = cm.lookup_char_pos(part.span.lo());
167 if prev_hi.line == cur_lo.line {
168 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
170 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
171 // push lines between the previous and current span (if any)
172 for idx in prev_hi.line..(cur_lo.line - 1) {
173 if let Some(line) = fm.get_line(idx) {
174 buf.push_str(line.as_ref());
178 if let Some(cur_line) = fm.get_line(cur_lo.line - 1) {
179 buf.push_str(&cur_line[..cur_lo.col.to_usize()]);
182 buf.push_str(&part.snippet);
183 prev_hi = cm.lookup_char_pos(part.span.hi());
184 prev_line = fm.get_line(prev_hi.line - 1);
186 // if the replacement already ends with a newline, don't print the next line
187 if !buf.ends_with('\n') {
188 push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
190 // remove trailing newlines
191 while buf.ends_with('\n') {
194 (buf, substitution.parts)
199 /// Used as a return value to signify a fatal error occurred. (It is also
200 /// used as the argument to panic at the moment, but that will eventually
202 #[derive(Copy, Clone, Debug)]
204 pub struct FatalError;
206 pub struct FatalErrorMarker;
208 // Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
209 // We don't want to invoke the panic handler and print a backtrace for fatal errors.
210 impl !Send for FatalError {}
213 pub fn raise(self) -> ! {
214 panic::resume_unwind(Box::new(FatalErrorMarker))
218 impl fmt::Display for FatalError {
219 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
220 write!(f, "parser fatal error")
224 impl error::Error for FatalError {
225 fn description(&self) -> &str {
226 "The parser has encountered a fatal error"
230 /// Signifies that the compiler died with an explicit call to `.bug`
231 /// or `.span_bug` rather than a failed assertion, etc.
232 #[derive(Copy, Clone, Debug)]
233 pub struct ExplicitBug;
235 impl fmt::Display for ExplicitBug {
236 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
237 write!(f, "parser internal bug")
241 impl error::Error for ExplicitBug {
242 fn description(&self) -> &str {
243 "The parser has encountered an internal bug"
247 pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
248 pub use diagnostic_builder::DiagnosticBuilder;
250 /// A handler deals with errors; certain errors
251 /// (fatal, bug, unimpl) may cause immediate exit,
252 /// others log errors for later reporting.
254 pub flags: HandlerFlags,
256 err_count: AtomicUsize,
257 emitter: RefCell<Box<Emitter>>,
258 continue_after_error: Cell<bool>,
259 delayed_span_bug: RefCell<Option<Diagnostic>>,
260 tracked_diagnostics: RefCell<Option<Vec<Diagnostic>>>,
262 // This set contains the `DiagnosticId` of all emitted diagnostics to avoid
263 // emitting the same diagnostic with extended help (`--teach`) twice, which
264 // would be uneccessary repetition.
265 tracked_diagnostic_codes: RefCell<FxHashSet<DiagnosticId>>,
267 // This set contains a hash of every diagnostic that has been emitted by
268 // this handler. These hashes is used to avoid emitting the same error
270 emitted_diagnostics: RefCell<FxHashSet<u128>>,
274 pub struct HandlerFlags {
275 pub can_emit_warnings: bool,
276 pub treat_err_as_bug: bool,
277 pub external_macro_backtrace: bool,
281 pub fn with_tty_emitter(color_config: ColorConfig,
282 can_emit_warnings: bool,
283 treat_err_as_bug: bool,
284 cm: Option<Rc<CodeMapper>>)
286 Handler::with_tty_emitter_and_flags(
292 .. Default::default()
296 pub fn with_tty_emitter_and_flags(color_config: ColorConfig,
297 cm: Option<Rc<CodeMapper>>,
300 let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false));
301 Handler::with_emitter_and_flags(emitter, flags)
304 pub fn with_emitter(can_emit_warnings: bool,
305 treat_err_as_bug: bool,
308 Handler::with_emitter_and_flags(
313 .. Default::default()
317 pub fn with_emitter_and_flags(e: Box<Emitter>, flags: HandlerFlags) -> Handler {
320 err_count: AtomicUsize::new(0),
321 emitter: RefCell::new(e),
322 continue_after_error: Cell::new(true),
323 delayed_span_bug: RefCell::new(None),
324 tracked_diagnostics: RefCell::new(None),
325 tracked_diagnostic_codes: RefCell::new(FxHashSet()),
326 emitted_diagnostics: RefCell::new(FxHashSet()),
330 pub fn set_continue_after_error(&self, continue_after_error: bool) {
331 self.continue_after_error.set(continue_after_error);
334 /// Resets the diagnostic error count as well as the cached emitted diagnostics.
336 /// NOTE: DO NOT call this function from rustc. It is only meant to be called from external
337 /// tools that want to reuse a `Parser` cleaning the previously emitted diagnostics as well as
338 /// the overall count of emitted error diagnostics.
339 pub fn reset_err_count(&self) {
340 self.emitted_diagnostics.replace(FxHashSet());
341 self.err_count.store(0, SeqCst);
344 pub fn struct_dummy<'a>(&'a self) -> DiagnosticBuilder<'a> {
345 DiagnosticBuilder::new(self, Level::Cancelled, "")
348 pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
351 -> DiagnosticBuilder<'a> {
352 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
354 if !self.flags.can_emit_warnings {
359 pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
363 -> DiagnosticBuilder<'a> {
364 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
367 if !self.flags.can_emit_warnings {
372 pub fn struct_warn<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
373 let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
374 if !self.flags.can_emit_warnings {
379 pub fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
382 -> DiagnosticBuilder<'a> {
383 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
387 pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
391 -> DiagnosticBuilder<'a> {
392 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
397 // FIXME: This method should be removed (every error should have an associated error code).
398 pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
399 DiagnosticBuilder::new(self, Level::Error, msg)
401 pub fn struct_err_with_code<'a>(
405 ) -> DiagnosticBuilder<'a> {
406 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
410 pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
413 -> DiagnosticBuilder<'a> {
414 let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
418 pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
422 -> DiagnosticBuilder<'a> {
423 let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
428 pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
429 DiagnosticBuilder::new(self, Level::Fatal, msg)
432 pub fn cancel(&self, err: &mut DiagnosticBuilder) {
436 fn panic_if_treat_err_as_bug(&self) {
437 if self.flags.treat_err_as_bug {
438 panic!("encountered error with `-Z treat_err_as_bug");
442 pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> FatalError {
443 self.emit(&sp.into(), msg, Fatal);
446 pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
451 self.emit_with_code(&sp.into(), msg, code, Fatal);
454 pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
455 self.emit(&sp.into(), msg, Error);
457 pub fn mut_span_err<'a, S: Into<MultiSpan>>(&'a self,
460 -> DiagnosticBuilder<'a> {
461 let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
465 pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
466 self.emit_with_code(&sp.into(), msg, code, Error);
468 pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
469 self.emit(&sp.into(), msg, Warning);
471 pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
472 self.emit_with_code(&sp.into(), msg, code, Warning);
474 pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
475 self.emit(&sp.into(), msg, Bug);
478 pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
479 if self.flags.treat_err_as_bug {
480 self.span_bug(sp, msg);
482 let mut diagnostic = Diagnostic::new(Level::Bug, msg);
483 diagnostic.set_span(sp.into());
484 *self.delayed_span_bug.borrow_mut() = Some(diagnostic);
486 pub fn span_bug_no_panic<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
487 self.emit(&sp.into(), msg, Bug);
489 pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
490 self.emit(&sp.into(), msg, Note);
492 pub fn span_note_diag<'a>(&'a self,
495 -> DiagnosticBuilder<'a> {
496 let mut db = DiagnosticBuilder::new(self, Note, msg);
500 pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
501 self.span_bug(sp, &format!("unimplemented {}", msg));
503 pub fn fatal(&self, msg: &str) -> FatalError {
504 if self.flags.treat_err_as_bug {
507 let mut db = DiagnosticBuilder::new(self, Fatal, msg);
511 pub fn err(&self, msg: &str) {
512 if self.flags.treat_err_as_bug {
515 let mut db = DiagnosticBuilder::new(self, Error, msg);
518 pub fn warn(&self, msg: &str) {
519 let mut db = DiagnosticBuilder::new(self, Warning, msg);
522 pub fn note_without_error(&self, msg: &str) {
523 let mut db = DiagnosticBuilder::new(self, Note, msg);
526 pub fn bug(&self, msg: &str) -> ! {
527 let mut db = DiagnosticBuilder::new(self, Bug, msg);
531 pub fn unimpl(&self, msg: &str) -> ! {
532 self.bug(&format!("unimplemented {}", msg));
535 fn bump_err_count(&self) {
536 self.panic_if_treat_err_as_bug();
537 self.err_count.fetch_add(1, SeqCst);
540 pub fn err_count(&self) -> usize {
541 self.err_count.load(SeqCst)
544 pub fn has_errors(&self) -> bool {
547 pub fn abort_if_errors(&self) {
549 match self.err_count() {
551 if let Some(bug) = self.delayed_span_bug.borrow_mut().take() {
552 DiagnosticBuilder::new_diagnostic(self, bug).emit();
556 1 => s = "aborting due to previous error".to_string(),
558 s = format!("aborting due to {} previous errors", self.err_count());
562 self.fatal(&s).raise();
564 pub fn emit(&self, msp: &MultiSpan, msg: &str, lvl: Level) {
565 if lvl == Warning && !self.flags.can_emit_warnings {
568 let mut db = DiagnosticBuilder::new(self, lvl, msg);
569 db.set_span(msp.clone());
571 if !self.continue_after_error.get() {
572 self.abort_if_errors();
575 pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: DiagnosticId, lvl: Level) {
576 if lvl == Warning && !self.flags.can_emit_warnings {
579 let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code), msg);
580 db.set_span(msp.clone());
582 if !self.continue_after_error.get() {
583 self.abort_if_errors();
587 pub fn track_diagnostics<F, R>(&self, f: F) -> (R, Vec<Diagnostic>)
588 where F: FnOnce() -> R
590 let prev = mem::replace(&mut *self.tracked_diagnostics.borrow_mut(),
593 let diagnostics = mem::replace(&mut *self.tracked_diagnostics.borrow_mut(), prev)
598 /// `true` if a diagnostic with this code has already been emitted in this handler.
600 /// Used to suppress emitting the same error multiple times with extended explanation when
601 /// calling `-Zteach`.
602 pub fn code_emitted(&self, code: &DiagnosticId) -> bool {
603 self.tracked_diagnostic_codes.borrow().contains(code)
606 fn emit_db(&self, db: &DiagnosticBuilder) {
607 let diagnostic = &**db;
609 if let Some(ref mut list) = *self.tracked_diagnostics.borrow_mut() {
610 list.push(diagnostic.clone());
613 if let Some(ref code) = diagnostic.code {
614 self.tracked_diagnostic_codes.borrow_mut().insert(code.clone());
617 let diagnostic_hash = {
619 let mut hasher = StableHasher::new();
620 diagnostic.hash(&mut hasher);
624 // Only emit the diagnostic if we haven't already emitted an equivalent
626 if self.emitted_diagnostics.borrow_mut().insert(diagnostic_hash) {
627 self.emitter.borrow_mut().emit(db);
629 self.bump_err_count();
636 #[derive(Copy, PartialEq, Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
640 // An error which while not immediately fatal, should stop the compiler
641 // progressing beyond the current phase.
650 impl fmt::Display for Level {
651 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
657 fn color(self) -> term::color::Color {
659 Bug | Fatal | PhaseFatal | Error => term::color::BRIGHT_RED,
662 term::color::BRIGHT_YELLOW
667 Note => term::color::BRIGHT_GREEN,
668 Help => term::color::BRIGHT_CYAN,
669 Cancelled => unreachable!(),
673 pub fn to_str(self) -> &'static str {
675 Bug => "error: internal compiler error",
676 Fatal | PhaseFatal | Error => "error",
677 Warning => "warning",
680 Cancelled => panic!("Shouldn't call on cancelled error"),