diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2023-07-26 13:58:50 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2023-07-31 09:34:36 +0000 |
| commit | 51c22154f56d54d512b0b14aebf9eb18278963a1 (patch) | |
| tree | 0778ba91c5f56e4f4675dbca0c88b37f9609a5b3 | |
| parent | 0e7ec9683dae0a0bc66797b1351059ed642f4e2d (diff) | |
| download | rust-51c22154f56d54d512b0b14aebf9eb18278963a1.tar.gz rust-51c22154f56d54d512b0b14aebf9eb18278963a1.zip | |
Remove a `bool` for color in favor of the `WriteColor` trait wrapping colored and uncolored printing
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 31 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/json.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_expand/Cargo.toml | 1 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/tests.rs | 22 | ||||
| -rw-r--r-- | src/librustdoc/doctest.rs | 4 | ||||
| -rw-r--r-- | src/tools/clippy/clippy_lints/src/doc.rs | 1 |
7 files changed, 49 insertions, 25 deletions
diff --git a/Cargo.lock b/Cargo.lock index dd8ce11ac12..7b7c0fd60d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3619,6 +3619,7 @@ dependencies = [ "rustc_session", "rustc_span", "smallvec", + "termcolor", "thin-vec", "tracing", ] 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 diff --git a/compiler/rustc_expand/Cargo.toml b/compiler/rustc_expand/Cargo.toml index 2dae0e3f53c..02da5b5dc53 100644 --- a/compiler/rustc_expand/Cargo.toml +++ b/compiler/rustc_expand/Cargo.toml @@ -27,3 +27,4 @@ rustc_span = { path = "../rustc_span" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } thin-vec = "0.2.12" tracing = "0.1" +termcolor = "1.2" diff --git a/compiler/rustc_expand/src/tests.rs b/compiler/rustc_expand/src/tests.rs index 4d1d834ed2b..8e3219c138c 100644 --- a/compiler/rustc_expand/src/tests.rs +++ b/compiler/rustc_expand/src/tests.rs @@ -9,6 +9,7 @@ use rustc_span::{BytePos, Span}; use rustc_data_structures::sync::Lrc; use rustc_errors::emitter::EmitterWriter; use rustc_errors::{Handler, MultiSpan, PResult}; +use termcolor::WriteColor; use std::io; use std::io::prelude::*; @@ -29,10 +30,9 @@ fn create_test_handler() -> (Handler, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) { vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE], false, ); - let emitter = - EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle, false) - .sm(Some(source_map.clone())) - .diagnostic_width(Some(140)); + let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle) + .sm(Some(source_map.clone())) + .diagnostic_width(Some(140)); let handler = Handler::with_emitter(Box::new(emitter)); (handler, source_map, output) } @@ -156,6 +156,20 @@ pub(crate) struct Shared<T: Write> { pub data: Arc<Mutex<T>>, } +impl<T: Write> WriteColor for Shared<T> { + fn supports_color(&self) -> bool { + false + } + + fn set_color(&mut self, _spec: &termcolor::ColorSpec) -> io::Result<()> { + Ok(()) + } + + fn reset(&mut self) -> io::Result<()> { + Ok(()) + } +} + impl<T: Write> Write for Shared<T> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.data.lock().unwrap().write(buf) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 1afac214da3..3315ccad4d3 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -562,7 +562,7 @@ pub(crate) fn make_test( .diagnostic_width(Some(80)) .supports_color(); - let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false); + let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle); // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings(); @@ -738,7 +738,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool { false, ); - let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false); + let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle); let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings(); let sess = ParseSess::with_span_handler(handler, sm); diff --git a/src/tools/clippy/clippy_lints/src/doc.rs b/src/tools/clippy/clippy_lints/src/doc.rs index 573838ce63e..2c4d93e33ba 100644 --- a/src/tools/clippy/clippy_lints/src/doc.rs +++ b/src/tools/clippy/clippy_lints/src/doc.rs @@ -719,7 +719,6 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) { let emitter = EmitterWriter::new( Box::new(io::sink()), fallback_bundle, - false, ); let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings(); let sess = ParseSess::with_span_handler(handler, sm); |
