]> git.lizzy.rs Git - rust.git/blob - src/librustc_errors/lib.rs
concerning well-formed suggestions for unused shorthand field patterns
[rust.git] / src / librustc_errors / lib.rs
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.
4 //
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.
10
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/")]
14 #![deny(warnings)]
15
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)]
23
24 extern crate term;
25 #[cfg(unix)]
26 extern crate libc;
27 extern crate rustc_data_structures;
28 extern crate serialize as rustc_serialize;
29 extern crate syntax_pos;
30 extern crate unicode_width;
31
32 pub use emitter::ColorConfig;
33
34 use self::Level::*;
35
36 use emitter::{Emitter, EmitterWriter};
37
38 use rustc_data_structures::fx::FxHashSet;
39 use rustc_data_structures::stable_hasher::StableHasher;
40
41 use std::borrow::Cow;
42 use std::cell::{RefCell, Cell};
43 use std::mem;
44 use std::rc::Rc;
45 use std::{error, fmt};
46 use std::sync::atomic::AtomicUsize;
47 use std::sync::atomic::Ordering::SeqCst;
48 use std::panic;
49
50 mod diagnostic;
51 mod diagnostic_builder;
52 pub mod emitter;
53 mod snippet;
54 pub mod registry;
55 mod styled_buffer;
56 mod lock;
57
58 use syntax_pos::{BytePos, Loc, FileLinesResult, FileMap, FileName, MultiSpan, Span, NO_EXPANSION};
59
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
64     ///
65     /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
66     /// `foo` and `bar` on their own:
67     ///
68     /// ```
69     /// vec![
70     ///     Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
71     ///     Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
72     /// ]
73     /// ```
74     ///
75     /// or by replacing the entire span:
76     ///
77     /// ```
78     /// vec![
79     ///     Substitution { parts: vec![(0..7, "a.b")] },
80     ///     Substitution { parts: vec![(0..7, "x.y")] },
81     /// ]
82     /// ```
83     pub substitutions: Vec<Substitution>,
84     pub msg: String,
85     pub show_code_when_inline: bool,
86 }
87
88 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
89 /// See the docs on `CodeSuggestion::substitutions`
90 pub struct Substitution {
91     pub parts: Vec<SubstitutionPart>,
92 }
93
94 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
95 pub struct SubstitutionPart {
96     pub span: Span,
97     pub snippet: String,
98 }
99
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;
109 }
110
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};
115
116         fn push_trailing(buf: &mut String,
117                          line_opt: Option<&Cow<str>>,
118                          lo: &Loc,
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],
126                         None => &line[lo..],
127                     });
128                 }
129                 if let None = hi_opt {
130                     buf.push('\n');
131                 }
132             }
133         }
134
135         assert!(!self.substitutions.is_empty());
136
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());
141
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());
148
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
156             //
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);
161
162             let mut prev_line = fm.get_line(lines.lines[0].line_index);
163             let mut buf = String::new();
164
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));
169                 } else {
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());
175                             buf.push('\n');
176                         }
177                     }
178                     if let Some(cur_line) = fm.get_line(cur_lo.line - 1) {
179                         buf.push_str(&cur_line[..cur_lo.col.to_usize()]);
180                     }
181                 }
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);
185             }
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);
189             }
190             // remove trailing newlines
191             while buf.ends_with('\n') {
192                 buf.pop();
193             }
194             (buf, substitution.parts)
195         }).collect()
196     }
197 }
198
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
201 /// not be true.)
202 #[derive(Copy, Clone, Debug)]
203 #[must_use]
204 pub struct FatalError;
205
206 pub struct FatalErrorMarker;
207
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 {}
211
212 impl FatalError {
213     pub fn raise(self) -> ! {
214         panic::resume_unwind(Box::new(FatalErrorMarker))
215     }
216 }
217
218 impl fmt::Display for FatalError {
219     fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
220         write!(f, "parser fatal error")
221     }
222 }
223
224 impl error::Error for FatalError {
225     fn description(&self) -> &str {
226         "The parser has encountered a fatal error"
227     }
228 }
229
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;
234
235 impl fmt::Display for ExplicitBug {
236     fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
237         write!(f, "parser internal bug")
238     }
239 }
240
241 impl error::Error for ExplicitBug {
242     fn description(&self) -> &str {
243         "The parser has encountered an internal bug"
244     }
245 }
246
247 pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
248 pub use diagnostic_builder::DiagnosticBuilder;
249
250 /// A handler deals with errors; certain errors
251 /// (fatal, bug, unimpl) may cause immediate exit,
252 /// others log errors for later reporting.
253 pub struct Handler {
254     pub flags: HandlerFlags,
255
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>>>,
261
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>>,
266
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
269     // twice.
270     emitted_diagnostics: RefCell<FxHashSet<u128>>,
271 }
272
273 #[derive(Default)]
274 pub struct HandlerFlags {
275     pub can_emit_warnings: bool,
276     pub treat_err_as_bug: bool,
277     pub external_macro_backtrace: bool,
278 }
279
280 impl Handler {
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>>)
285                             -> Handler {
286         Handler::with_tty_emitter_and_flags(
287             color_config,
288             cm,
289             HandlerFlags {
290                 can_emit_warnings,
291                 treat_err_as_bug,
292                 .. Default::default()
293             })
294     }
295
296     pub fn with_tty_emitter_and_flags(color_config: ColorConfig,
297                                       cm: Option<Rc<CodeMapper>>,
298                                       flags: HandlerFlags)
299                                       -> Handler {
300         let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false));
301         Handler::with_emitter_and_flags(emitter, flags)
302     }
303
304     pub fn with_emitter(can_emit_warnings: bool,
305                         treat_err_as_bug: bool,
306                         e: Box<Emitter>)
307                         -> Handler {
308         Handler::with_emitter_and_flags(
309             e,
310             HandlerFlags {
311                 can_emit_warnings,
312                 treat_err_as_bug,
313                 .. Default::default()
314             })
315     }
316
317     pub fn with_emitter_and_flags(e: Box<Emitter>, flags: HandlerFlags) -> Handler {
318         Handler {
319             flags,
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()),
327         }
328     }
329
330     pub fn set_continue_after_error(&self, continue_after_error: bool) {
331         self.continue_after_error.set(continue_after_error);
332     }
333
334     /// Resets the diagnostic error count as well as the cached emitted diagnostics.
335     ///
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);
342     }
343
344     pub fn struct_dummy<'a>(&'a self) -> DiagnosticBuilder<'a> {
345         DiagnosticBuilder::new(self, Level::Cancelled, "")
346     }
347
348     pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
349                                                     sp: S,
350                                                     msg: &str)
351                                                     -> DiagnosticBuilder<'a> {
352         let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
353         result.set_span(sp);
354         if !self.flags.can_emit_warnings {
355             result.cancel();
356         }
357         result
358     }
359     pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
360                                                               sp: S,
361                                                               msg: &str,
362                                                               code: DiagnosticId)
363                                                               -> DiagnosticBuilder<'a> {
364         let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
365         result.set_span(sp);
366         result.code(code);
367         if !self.flags.can_emit_warnings {
368             result.cancel();
369         }
370         result
371     }
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 {
375             result.cancel();
376         }
377         result
378     }
379     pub fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
380                                                    sp: S,
381                                                    msg: &str)
382                                                    -> DiagnosticBuilder<'a> {
383         let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
384         result.set_span(sp);
385         result
386     }
387     pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
388                                                              sp: S,
389                                                              msg: &str,
390                                                              code: DiagnosticId)
391                                                              -> DiagnosticBuilder<'a> {
392         let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
393         result.set_span(sp);
394         result.code(code);
395         result
396     }
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)
400     }
401     pub fn struct_err_with_code<'a>(
402         &'a self,
403         msg: &str,
404         code: DiagnosticId,
405     ) -> DiagnosticBuilder<'a> {
406         let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
407         result.code(code);
408         result
409     }
410     pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
411                                                      sp: S,
412                                                      msg: &str)
413                                                      -> DiagnosticBuilder<'a> {
414         let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
415         result.set_span(sp);
416         result
417     }
418     pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
419                                                                sp: S,
420                                                                msg: &str,
421                                                                code: DiagnosticId)
422                                                                -> DiagnosticBuilder<'a> {
423         let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
424         result.set_span(sp);
425         result.code(code);
426         result
427     }
428     pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
429         DiagnosticBuilder::new(self, Level::Fatal, msg)
430     }
431
432     pub fn cancel(&self, err: &mut DiagnosticBuilder) {
433         err.cancel();
434     }
435
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");
439         }
440     }
441
442     pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> FatalError {
443         self.emit(&sp.into(), msg, Fatal);
444         FatalError
445     }
446     pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
447                                                     sp: S,
448                                                     msg: &str,
449                                                     code: DiagnosticId)
450                                                     -> FatalError {
451         self.emit_with_code(&sp.into(), msg, code, Fatal);
452         FatalError
453     }
454     pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
455         self.emit(&sp.into(), msg, Error);
456     }
457     pub fn mut_span_err<'a, S: Into<MultiSpan>>(&'a self,
458                                                 sp: S,
459                                                 msg: &str)
460                                                 -> DiagnosticBuilder<'a> {
461         let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
462         result.set_span(sp);
463         result
464     }
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);
467     }
468     pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
469         self.emit(&sp.into(), msg, Warning);
470     }
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);
473     }
474     pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
475         self.emit(&sp.into(), msg, Bug);
476         panic!(ExplicitBug);
477     }
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);
481         }
482         let mut diagnostic = Diagnostic::new(Level::Bug, msg);
483         diagnostic.set_span(sp.into());
484         *self.delayed_span_bug.borrow_mut() = Some(diagnostic);
485     }
486     pub fn span_bug_no_panic<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
487         self.emit(&sp.into(), msg, Bug);
488     }
489     pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
490         self.emit(&sp.into(), msg, Note);
491     }
492     pub fn span_note_diag<'a>(&'a self,
493                               sp: Span,
494                               msg: &str)
495                               -> DiagnosticBuilder<'a> {
496         let mut db = DiagnosticBuilder::new(self, Note, msg);
497         db.set_span(sp);
498         db
499     }
500     pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
501         self.span_bug(sp, &format!("unimplemented {}", msg));
502     }
503     pub fn fatal(&self, msg: &str) -> FatalError {
504         if self.flags.treat_err_as_bug {
505             self.bug(msg);
506         }
507         let mut db = DiagnosticBuilder::new(self, Fatal, msg);
508         db.emit();
509         FatalError
510     }
511     pub fn err(&self, msg: &str) {
512         if self.flags.treat_err_as_bug {
513             self.bug(msg);
514         }
515         let mut db = DiagnosticBuilder::new(self, Error, msg);
516         db.emit();
517     }
518     pub fn warn(&self, msg: &str) {
519         let mut db = DiagnosticBuilder::new(self, Warning, msg);
520         db.emit();
521     }
522     pub fn note_without_error(&self, msg: &str) {
523         let mut db = DiagnosticBuilder::new(self, Note, msg);
524         db.emit();
525     }
526     pub fn bug(&self, msg: &str) -> ! {
527         let mut db = DiagnosticBuilder::new(self, Bug, msg);
528         db.emit();
529         panic!(ExplicitBug);
530     }
531     pub fn unimpl(&self, msg: &str) -> ! {
532         self.bug(&format!("unimplemented {}", msg));
533     }
534
535     fn bump_err_count(&self) {
536         self.panic_if_treat_err_as_bug();
537         self.err_count.fetch_add(1, SeqCst);
538     }
539
540     pub fn err_count(&self) -> usize {
541         self.err_count.load(SeqCst)
542     }
543
544     pub fn has_errors(&self) -> bool {
545         self.err_count() > 0
546     }
547     pub fn abort_if_errors(&self) {
548         let s;
549         match self.err_count() {
550             0 => {
551                 if let Some(bug) = self.delayed_span_bug.borrow_mut().take() {
552                     DiagnosticBuilder::new_diagnostic(self, bug).emit();
553                 }
554                 return;
555             }
556             1 => s = "aborting due to previous error".to_string(),
557             _ => {
558                 s = format!("aborting due to {} previous errors", self.err_count());
559             }
560         }
561
562         self.fatal(&s).raise();
563     }
564     pub fn emit(&self, msp: &MultiSpan, msg: &str, lvl: Level) {
565         if lvl == Warning && !self.flags.can_emit_warnings {
566             return;
567         }
568         let mut db = DiagnosticBuilder::new(self, lvl, msg);
569         db.set_span(msp.clone());
570         db.emit();
571         if !self.continue_after_error.get() {
572             self.abort_if_errors();
573         }
574     }
575     pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: DiagnosticId, lvl: Level) {
576         if lvl == Warning && !self.flags.can_emit_warnings {
577             return;
578         }
579         let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code), msg);
580         db.set_span(msp.clone());
581         db.emit();
582         if !self.continue_after_error.get() {
583             self.abort_if_errors();
584         }
585     }
586
587     pub fn track_diagnostics<F, R>(&self, f: F) -> (R, Vec<Diagnostic>)
588         where F: FnOnce() -> R
589     {
590         let prev = mem::replace(&mut *self.tracked_diagnostics.borrow_mut(),
591                                 Some(Vec::new()));
592         let ret = f();
593         let diagnostics = mem::replace(&mut *self.tracked_diagnostics.borrow_mut(), prev)
594             .unwrap();
595         (ret, diagnostics)
596     }
597
598     /// `true` if a diagnostic with this code has already been emitted in this handler.
599     ///
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)
604     }
605
606     fn emit_db(&self, db: &DiagnosticBuilder) {
607         let diagnostic = &**db;
608
609         if let Some(ref mut list) = *self.tracked_diagnostics.borrow_mut() {
610             list.push(diagnostic.clone());
611         }
612
613         if let Some(ref code) = diagnostic.code {
614             self.tracked_diagnostic_codes.borrow_mut().insert(code.clone());
615         }
616
617         let diagnostic_hash = {
618             use std::hash::Hash;
619             let mut hasher = StableHasher::new();
620             diagnostic.hash(&mut hasher);
621             hasher.finish()
622         };
623
624         // Only emit the diagnostic if we haven't already emitted an equivalent
625         // one:
626         if self.emitted_diagnostics.borrow_mut().insert(diagnostic_hash) {
627             self.emitter.borrow_mut().emit(db);
628             if db.is_error() {
629                 self.bump_err_count();
630             }
631         }
632     }
633 }
634
635
636 #[derive(Copy, PartialEq, Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
637 pub enum Level {
638     Bug,
639     Fatal,
640     // An error which while not immediately fatal, should stop the compiler
641     // progressing beyond the current phase.
642     PhaseFatal,
643     Error,
644     Warning,
645     Note,
646     Help,
647     Cancelled,
648 }
649
650 impl fmt::Display for Level {
651     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
652         self.to_str().fmt(f)
653     }
654 }
655
656 impl Level {
657     fn color(self) -> term::color::Color {
658         match self {
659             Bug | Fatal | PhaseFatal | Error => term::color::BRIGHT_RED,
660             Warning => {
661                 if cfg!(windows) {
662                     term::color::BRIGHT_YELLOW
663                 } else {
664                     term::color::YELLOW
665                 }
666             }
667             Note => term::color::BRIGHT_GREEN,
668             Help => term::color::BRIGHT_CYAN,
669             Cancelled => unreachable!(),
670         }
671     }
672
673     pub fn to_str(self) -> &'static str {
674         match self {
675             Bug => "error: internal compiler error",
676             Fatal | PhaseFatal | Error => "error",
677             Warning => "warning",
678             Note => "note",
679             Help => "help",
680             Cancelled => panic!("Shouldn't call on cancelled error"),
681         }
682     }
683 }