about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-07-26 13:58:50 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-07-31 09:34:36 +0000
commit51c22154f56d54d512b0b14aebf9eb18278963a1 (patch)
tree0778ba91c5f56e4f4675dbca0c88b37f9609a5b3 /compiler/rustc_errors/src
parent0e7ec9683dae0a0bc66797b1351059ed642f4e2d (diff)
downloadrust-51c22154f56d54d512b0b14aebf9eb18278963a1.tar.gz
rust-51c22154f56d54d512b0b14aebf9eb18278963a1.zip
Remove a `bool` for color in favor of the `WriteColor` trait wrapping colored and uncolored printing
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/emitter.rs31
-rw-r--r--compiler/rustc_errors/src/json.rs14
2 files changed, 27 insertions, 18 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index 41fb1cafe25..87b3a25ed44 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -61,12 +61,15 @@ impl HumanReadableErrorType {
     }
     pub fn new_emitter(
         self,
-        dst: Box<dyn Write + Send>,
+        mut dst: Box<dyn WriteColor + Send>,
         fallback_bundle: LazyFallbackBundle,
     ) -> EmitterWriter {
         let (short, color_config) = self.unzip();
         let color = color_config.suggests_using_colors();
-        EmitterWriter::new(dst, fallback_bundle, color).short_message(short)
+        if !dst.supports_color() && color {
+            dst = Box::new(Ansi::new(dst));
+        }
+        EmitterWriter::new(dst, fallback_bundle).short_message(short)
     }
 }
 
@@ -669,11 +672,10 @@ impl EmitterWriter {
     }
 
     pub fn new(
-        dst: Box<dyn Write + Send>,
+        dst: Box<dyn WriteColor + Send>,
         fallback_bundle: LazyFallbackBundle,
-        colored: bool,
     ) -> EmitterWriter {
-        Self::create(Raw(dst, colored), fallback_bundle)
+        Self::create(Raw(dst), fallback_bundle)
     }
 
     fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> {
@@ -2603,15 +2605,13 @@ fn emit_to_destination(
 pub enum Destination {
     Terminal(StandardStream),
     Buffered(BufferWriter),
-    // The bool denotes whether we should be emitting ansi color codes or not
-    Raw(Box<(dyn Write + Send)>, bool),
+    Raw(Box<(dyn WriteColor + Send)>),
 }
 
 pub enum WritableDst<'a> {
     Terminal(&'a mut StandardStream),
     Buffered(&'a mut BufferWriter, Buffer),
-    Raw(&'a mut (dyn Write + Send)),
-    ColoredRaw(Ansi<&'a mut (dyn Write + Send)>),
+    Raw(&'a mut (dyn WriteColor + Send)),
 }
 
 impl Destination {
@@ -2637,8 +2637,7 @@ impl Destination {
                 let buf = t.buffer();
                 WritableDst::Buffered(t, buf)
             }
-            Destination::Raw(ref mut t, false) => WritableDst::Raw(t),
-            Destination::Raw(ref mut t, true) => WritableDst::ColoredRaw(Ansi::new(t)),
+            Destination::Raw(ref mut t) => WritableDst::Raw(t),
         }
     }
 
@@ -2646,7 +2645,7 @@ impl Destination {
         match *self {
             Self::Terminal(ref stream) => stream.supports_color(),
             Self::Buffered(ref buffer) => buffer.buffer().supports_color(),
-            Self::Raw(_, supports_color) => supports_color,
+            Self::Raw(ref writer) => writer.supports_color(),
         }
     }
 }
@@ -2706,8 +2705,7 @@ impl<'a> WritableDst<'a> {
         match *self {
             WritableDst::Terminal(ref mut t) => t.set_color(color),
             WritableDst::Buffered(_, ref mut t) => t.set_color(color),
-            WritableDst::ColoredRaw(ref mut t) => t.set_color(color),
-            WritableDst::Raw(_) => Ok(()),
+            WritableDst::Raw(ref mut t) => t.set_color(color),
         }
     }
 
@@ -2715,8 +2713,7 @@ impl<'a> WritableDst<'a> {
         match *self {
             WritableDst::Terminal(ref mut t) => t.reset(),
             WritableDst::Buffered(_, ref mut t) => t.reset(),
-            WritableDst::ColoredRaw(ref mut t) => t.reset(),
-            WritableDst::Raw(_) => Ok(()),
+            WritableDst::Raw(ref mut t) => t.reset(),
         }
     }
 }
@@ -2727,7 +2724,6 @@ impl<'a> Write for WritableDst<'a> {
             WritableDst::Terminal(ref mut t) => t.write(bytes),
             WritableDst::Buffered(_, ref mut buf) => buf.write(bytes),
             WritableDst::Raw(ref mut w) => w.write(bytes),
-            WritableDst::ColoredRaw(ref mut t) => t.write(bytes),
         }
     }
 
@@ -2736,7 +2732,6 @@ impl<'a> Write for WritableDst<'a> {
             WritableDst::Terminal(ref mut t) => t.flush(),
             WritableDst::Buffered(_, ref mut buf) => buf.flush(),
             WritableDst::Raw(ref mut w) => w.flush(),
-            WritableDst::ColoredRaw(ref mut w) => w.flush(),
         }
     }
 }
diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs
index e199244b1e4..b8f58e3057c 100644
--- a/compiler/rustc_errors/src/json.rs
+++ b/compiler/rustc_errors/src/json.rs
@@ -10,6 +10,7 @@
 // FIXME: spec the JSON output properly.
 
 use rustc_span::source_map::{FilePathMapping, SourceMap};
+use termcolor::{ColorSpec, WriteColor};
 
 use crate::emitter::{Emitter, HumanReadableErrorType};
 use crate::registry::Registry;
@@ -356,6 +357,19 @@ impl Diagnostic {
                 self.0.lock().unwrap().flush()
             }
         }
+        impl WriteColor for BufWriter {
+            fn supports_color(&self) -> bool {
+                false
+            }
+
+            fn set_color(&mut self, _spec: &ColorSpec) -> io::Result<()> {
+                Ok(())
+            }
+
+            fn reset(&mut self) -> io::Result<()> {
+                Ok(())
+            }
+        }
         let buf = BufWriter::default();
         let output = buf.clone();
         je.json_rendered