Skip to content

Commit 51c2215

Browse files
committed
Remove a bool for color in favor of the WriteColor trait wrapping colored and uncolored printing
1 parent 0e7ec96 commit 51c2215

File tree

7 files changed

+49
-25
lines changed

7 files changed

+49
-25
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,7 @@ dependencies = [
36193619
"rustc_session",
36203620
"rustc_span",
36213621
"smallvec",
3622+
"termcolor",
36223623
"thin-vec",
36233624
"tracing",
36243625
]

compiler/rustc_errors/src/emitter.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ impl HumanReadableErrorType {
6161
}
6262
pub fn new_emitter(
6363
self,
64-
dst: Box<dyn Write + Send>,
64+
mut dst: Box<dyn WriteColor + Send>,
6565
fallback_bundle: LazyFallbackBundle,
6666
) -> EmitterWriter {
6767
let (short, color_config) = self.unzip();
6868
let color = color_config.suggests_using_colors();
69-
EmitterWriter::new(dst, fallback_bundle, color).short_message(short)
69+
if !dst.supports_color() && color {
70+
dst = Box::new(Ansi::new(dst));
71+
}
72+
EmitterWriter::new(dst, fallback_bundle).short_message(short)
7073
}
7174
}
7275

@@ -669,11 +672,10 @@ impl EmitterWriter {
669672
}
670673

671674
pub fn new(
672-
dst: Box<dyn Write + Send>,
675+
dst: Box<dyn WriteColor + Send>,
673676
fallback_bundle: LazyFallbackBundle,
674-
colored: bool,
675677
) -> EmitterWriter {
676-
Self::create(Raw(dst, colored), fallback_bundle)
678+
Self::create(Raw(dst), fallback_bundle)
677679
}
678680

679681
fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> {
@@ -2603,15 +2605,13 @@ fn emit_to_destination(
26032605
pub enum Destination {
26042606
Terminal(StandardStream),
26052607
Buffered(BufferWriter),
2606-
// The bool denotes whether we should be emitting ansi color codes or not
2607-
Raw(Box<(dyn Write + Send)>, bool),
2608+
Raw(Box<(dyn WriteColor + Send)>),
26082609
}
26092610

26102611
pub enum WritableDst<'a> {
26112612
Terminal(&'a mut StandardStream),
26122613
Buffered(&'a mut BufferWriter, Buffer),
2613-
Raw(&'a mut (dyn Write + Send)),
2614-
ColoredRaw(Ansi<&'a mut (dyn Write + Send)>),
2614+
Raw(&'a mut (dyn WriteColor + Send)),
26152615
}
26162616

26172617
impl Destination {
@@ -2637,16 +2637,15 @@ impl Destination {
26372637
let buf = t.buffer();
26382638
WritableDst::Buffered(t, buf)
26392639
}
2640-
Destination::Raw(ref mut t, false) => WritableDst::Raw(t),
2641-
Destination::Raw(ref mut t, true) => WritableDst::ColoredRaw(Ansi::new(t)),
2640+
Destination::Raw(ref mut t) => WritableDst::Raw(t),
26422641
}
26432642
}
26442643

26452644
fn supports_color(&self) -> bool {
26462645
match *self {
26472646
Self::Terminal(ref stream) => stream.supports_color(),
26482647
Self::Buffered(ref buffer) => buffer.buffer().supports_color(),
2649-
Self::Raw(_, supports_color) => supports_color,
2648+
Self::Raw(ref writer) => writer.supports_color(),
26502649
}
26512650
}
26522651
}
@@ -2706,17 +2705,15 @@ impl<'a> WritableDst<'a> {
27062705
match *self {
27072706
WritableDst::Terminal(ref mut t) => t.set_color(color),
27082707
WritableDst::Buffered(_, ref mut t) => t.set_color(color),
2709-
WritableDst::ColoredRaw(ref mut t) => t.set_color(color),
2710-
WritableDst::Raw(_) => Ok(()),
2708+
WritableDst::Raw(ref mut t) => t.set_color(color),
27112709
}
27122710
}
27132711

27142712
fn reset(&mut self) -> io::Result<()> {
27152713
match *self {
27162714
WritableDst::Terminal(ref mut t) => t.reset(),
27172715
WritableDst::Buffered(_, ref mut t) => t.reset(),
2718-
WritableDst::ColoredRaw(ref mut t) => t.reset(),
2719-
WritableDst::Raw(_) => Ok(()),
2716+
WritableDst::Raw(ref mut t) => t.reset(),
27202717
}
27212718
}
27222719
}
@@ -2727,7 +2724,6 @@ impl<'a> Write for WritableDst<'a> {
27272724
WritableDst::Terminal(ref mut t) => t.write(bytes),
27282725
WritableDst::Buffered(_, ref mut buf) => buf.write(bytes),
27292726
WritableDst::Raw(ref mut w) => w.write(bytes),
2730-
WritableDst::ColoredRaw(ref mut t) => t.write(bytes),
27312727
}
27322728
}
27332729

@@ -2736,7 +2732,6 @@ impl<'a> Write for WritableDst<'a> {
27362732
WritableDst::Terminal(ref mut t) => t.flush(),
27372733
WritableDst::Buffered(_, ref mut buf) => buf.flush(),
27382734
WritableDst::Raw(ref mut w) => w.flush(),
2739-
WritableDst::ColoredRaw(ref mut w) => w.flush(),
27402735
}
27412736
}
27422737
}

compiler/rustc_errors/src/json.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// FIXME: spec the JSON output properly.
1111

1212
use rustc_span::source_map::{FilePathMapping, SourceMap};
13+
use termcolor::{ColorSpec, WriteColor};
1314

1415
use crate::emitter::{Emitter, HumanReadableErrorType};
1516
use crate::registry::Registry;
@@ -356,6 +357,19 @@ impl Diagnostic {
356357
self.0.lock().unwrap().flush()
357358
}
358359
}
360+
impl WriteColor for BufWriter {
361+
fn supports_color(&self) -> bool {
362+
false
363+
}
364+
365+
fn set_color(&mut self, _spec: &ColorSpec) -> io::Result<()> {
366+
Ok(())
367+
}
368+
369+
fn reset(&mut self) -> io::Result<()> {
370+
Ok(())
371+
}
372+
}
359373
let buf = BufWriter::default();
360374
let output = buf.clone();
361375
je.json_rendered

compiler/rustc_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ rustc_span = { path = "../rustc_span" }
2727
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
2828
thin-vec = "0.2.12"
2929
tracing = "0.1"
30+
termcolor = "1.2"

compiler/rustc_expand/src/tests.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_span::{BytePos, Span};
99
use rustc_data_structures::sync::Lrc;
1010
use rustc_errors::emitter::EmitterWriter;
1111
use rustc_errors::{Handler, MultiSpan, PResult};
12+
use termcolor::WriteColor;
1213

1314
use std::io;
1415
use std::io::prelude::*;
@@ -29,10 +30,9 @@ fn create_test_handler() -> (Handler, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
2930
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
3031
false,
3132
);
32-
let emitter =
33-
EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle, false)
34-
.sm(Some(source_map.clone()))
35-
.diagnostic_width(Some(140));
33+
let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle)
34+
.sm(Some(source_map.clone()))
35+
.diagnostic_width(Some(140));
3636
let handler = Handler::with_emitter(Box::new(emitter));
3737
(handler, source_map, output)
3838
}
@@ -156,6 +156,20 @@ pub(crate) struct Shared<T: Write> {
156156
pub data: Arc<Mutex<T>>,
157157
}
158158

159+
impl<T: Write> WriteColor for Shared<T> {
160+
fn supports_color(&self) -> bool {
161+
false
162+
}
163+
164+
fn set_color(&mut self, _spec: &termcolor::ColorSpec) -> io::Result<()> {
165+
Ok(())
166+
}
167+
168+
fn reset(&mut self) -> io::Result<()> {
169+
Ok(())
170+
}
171+
}
172+
159173
impl<T: Write> Write for Shared<T> {
160174
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
161175
self.data.lock().unwrap().write(buf)

src/librustdoc/doctest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ pub(crate) fn make_test(
562562
.diagnostic_width(Some(80))
563563
.supports_color();
564564

565-
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false);
565+
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
566566

567567
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
568568
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 {
738738
false,
739739
);
740740

741-
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false);
741+
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
742742

743743
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
744744
let sess = ParseSess::with_span_handler(handler, sm);

src/tools/clippy/clippy_lints/src/doc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,6 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
719719
let emitter = EmitterWriter::new(
720720
Box::new(io::sink()),
721721
fallback_bundle,
722-
false,
723722
);
724723
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
725724
let sess = ParseSess::with_span_handler(handler, sm);

0 commit comments

Comments
 (0)