Remove a bool for color in favor of the WriteColor trait wrapping colored and uncolored printing

This commit is contained in:
Oli Scherer 2023-07-26 13:58:50 +00:00
parent 0e7ec9683d
commit 51c22154f5
7 changed files with 49 additions and 25 deletions

View file

@ -3619,6 +3619,7 @@ dependencies = [
"rustc_session",
"rustc_span",
"smallvec",
"termcolor",
"thin-vec",
"tracing",
]

View file

@ -61,12 +61,15 @@ pub fn unzip(self) -> (bool, ColorConfig) {
}
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 @@ fn create(dst: Destination, fallback_bundle: LazyFallbackBundle) -> EmitterWrite
}
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 @@ fn writable(&mut self) -> WritableDst<'_> {
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 @@ fn supports_color(&self) -> bool {
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 @@ fn set_color(&mut self, color: &ColorSpec) -> io::Result<()> {
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 @@ fn reset(&mut self) -> io::Result<()> {
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 @@ fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
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 @@ fn flush(&mut self) -> io::Result<()> {
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(),
}
}
}

View file

@ -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 @@ fn flush(&mut self) -> io::Result<()> {
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

View file

@ -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"

View file

@ -9,6 +9,7 @@
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)

View file

@ -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);

View file

@ -719,7 +719,6 @@ fn has_needless_main(code: String, edition: Edition) -> bool {
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);