]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/session/mod.rs
The war on abort_if_errors
[rust.git] / src / librustc / session / mod.rs
index 80b4c1916a81626d9c28cc3b8b5271f29acf5328..975ec0e709b7d272605e9c31b6f9c2fe057c6511 100644 (file)
@@ -17,7 +17,8 @@
 use syntax::ast::{NodeId, NodeIdAssigner, Name};
 use syntax::codemap::Span;
 use syntax::errors::{self, DiagnosticBuilder};
-use syntax::errors::emitter::{Emitter, BasicEmitter};
+use syntax::errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
+use syntax::errors::json::JsonEmitter;
 use syntax::diagnostics;
 use syntax::feature_gate;
 use syntax::parse;
@@ -175,14 +176,15 @@ pub fn has_errors(&self) -> bool {
     pub fn abort_if_errors(&self) {
         self.diagnostic().abort_if_errors();
     }
-    pub fn abort_if_new_errors<F>(&self, mut f: F)
-        where F: FnMut()
+    pub fn abort_if_new_errors<F, T>(&self, f: F) -> T
+        where F: FnOnce() -> T
     {
         let count = self.err_count();
-        f();
+        let result = f();
         if self.err_count() > count {
             self.abort_if_errors();
         }
+        result
     }
     pub fn span_warn(&self, sp: Span, msg: &str) {
         self.diagnostic().span_warn(sp, msg)
@@ -405,12 +407,19 @@ pub fn build_session(sopts: config::Options,
     let treat_err_as_bug = sopts.treat_err_as_bug;
 
     let codemap = Rc::new(codemap::CodeMap::new());
+    let emitter: Box<Emitter> = match sopts.error_format {
+        config::ErrorOutputType::HumanReadable(color_config) => {
+            Box::new(EmitterWriter::stderr(color_config, Some(registry), codemap.clone()))
+        }
+        config::ErrorOutputType::Json => {
+            Box::new(JsonEmitter::stderr(Some(registry), codemap.clone()))
+        }
+    };
+
     let diagnostic_handler =
-        errors::Handler::new(sopts.color,
-                             Some(registry),
-                             can_print_warnings,
-                             treat_err_as_bug,
-                             codemap.clone());
+        errors::Handler::with_emitter(can_print_warnings,
+                                      treat_err_as_bug,
+                                      emitter);
 
     build_session_(sopts, local_crate_source_file, diagnostic_handler, codemap, cstore)
 }
@@ -473,13 +482,23 @@ pub fn build_session_(sopts: config::Options,
     sess
 }
 
-pub fn early_error(color: errors::ColorConfig, msg: &str) -> ! {
-    let mut emitter = BasicEmitter::stderr(color);
+pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
+    let mut emitter: Box<Emitter> = match output {
+        config::ErrorOutputType::HumanReadable(color_config) => {
+            Box::new(BasicEmitter::stderr(color_config))
+        }
+        config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
+    };
     emitter.emit(None, msg, None, errors::Level::Fatal);
     panic!(errors::FatalError);
 }
 
-pub fn early_warn(color: errors::ColorConfig, msg: &str) {
-    let mut emitter = BasicEmitter::stderr(color);
+pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
+    let mut emitter: Box<Emitter> = match output {
+        config::ErrorOutputType::HumanReadable(color_config) => {
+            Box::new(BasicEmitter::stderr(color_config))
+        }
+        config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
+    };
     emitter.emit(None, msg, None, errors::Level::Warning);
 }