Skip to content

Commit 437325b

Browse files
committed
Inline and remove HumanReadableErrorType::new_emitter.
And likewise with `ColorConfig::suggests_using_colors`. They both have a single call site. And note that `BufWriter::supports_color()` always returns false, which enables a small bit of constant folding along the way.
1 parent d372741 commit 437325b

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::io::prelude::*;
3535
use std::io::{self, IsTerminal};
3636
use std::iter;
3737
use std::path::Path;
38-
use termcolor::{Ansi, Buffer, BufferWriter, ColorChoice, ColorSpec, StandardStream};
38+
use termcolor::{Buffer, BufferWriter, ColorChoice, ColorSpec, StandardStream};
3939
use termcolor::{Color, WriteColor};
4040

4141
/// Default column width, used in tests and when terminal dimensions cannot be determined.
@@ -58,18 +58,6 @@ impl HumanReadableErrorType {
5858
HumanReadableErrorType::AnnotateSnippet(cc) => (false, cc),
5959
}
6060
}
61-
pub fn new_emitter(
62-
self,
63-
mut dst: Destination,
64-
fallback_bundle: LazyFallbackBundle,
65-
) -> HumanEmitter {
66-
let (short, color_config) = self.unzip();
67-
let color = color_config.suggests_using_colors();
68-
if !dst.supports_color() && color {
69-
dst = Box::new(Ansi::new(dst));
70-
}
71-
HumanEmitter::new(dst, fallback_bundle).short_message(short)
72-
}
7361
}
7462

7563
#[derive(Clone, Copy, Debug)]
@@ -628,12 +616,6 @@ impl ColorConfig {
628616
ColorConfig::Auto => ColorChoice::Never,
629617
}
630618
}
631-
fn suggests_using_colors(self) -> bool {
632-
match self {
633-
ColorConfig::Always | ColorConfig::Auto => true,
634-
ColorConfig::Never => false,
635-
}
636-
}
637619
}
638620

639621
/// Handles the writing of `HumanReadableErrorType::Default` and `HumanReadableErrorType::Short`

compiler/rustc_errors/src/json.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
use rustc_span::source_map::{FilePathMapping, SourceMap};
1313
use termcolor::{ColorSpec, WriteColor};
1414

15-
use crate::emitter::{should_show_source_code, Emitter, HumanReadableErrorType};
15+
use crate::emitter::{
16+
should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter,
17+
HumanReadableErrorType,
18+
};
1619
use crate::registry::Registry;
1720
use crate::translation::{to_fluent_args, Translate};
1821
use crate::{
@@ -405,8 +408,17 @@ impl Diagnostic {
405408
.collect();
406409

407410
let buf = BufWriter::default();
408-
je.json_rendered
409-
.new_emitter(Box::new(buf.clone()), je.fallback_bundle.clone())
411+
let mut dst: Destination = Box::new(buf.clone());
412+
let (short, color_config) = je.json_rendered.unzip();
413+
let color = match color_config {
414+
ColorConfig::Always | ColorConfig::Auto => true,
415+
ColorConfig::Never => false,
416+
};
417+
if color {
418+
dst = Box::new(termcolor::Ansi::new(dst));
419+
}
420+
HumanEmitter::new(dst, je.fallback_bundle.clone())
421+
.short_message(short)
410422
.sm(Some(je.sm.clone()))
411423
.fluent_bundle(je.fluent_bundle.clone())
412424
.diagnostic_width(je.diagnostic_width)

compiler/rustc_errors/src/json/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
22

3-
use crate::emitter::ColorConfig;
43
use crate::DiagCtxt;
54
use rustc_span::BytePos;
65

0 commit comments

Comments
 (0)