diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-11-11 16:44:02 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-11-17 10:33:14 +0100 |
| commit | 32d64edcf9d31ded609ab70b31bd3779d6f85ec1 (patch) | |
| tree | 685f3663dfc9d72589f64553192033702657051c | |
| parent | 704001b929b9e93f67d4affa9cae27bdb64d1aeb (diff) | |
| download | rust-32d64edcf9d31ded609ab70b31bd3779d6f85ec1.tar.gz rust-32d64edcf9d31ded609ab70b31bd3779d6f85ec1.zip | |
Simplfy color availability check
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 17 | ||||
| -rw-r--r-- | src/librustdoc/Cargo.toml | 3 | ||||
| -rw-r--r-- | src/librustdoc/doctest.rs | 58 | ||||
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 3 | ||||
| -rw-r--r-- | src/librustdoc/lib.rs | 4 |
6 files changed, 41 insertions, 45 deletions
diff --git a/Cargo.lock b/Cargo.lock index b7a08075080..bb33404e690 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4301,7 +4301,6 @@ dependencies = [ "serde_json", "smallvec 1.4.2", "tempfile", - "termcolor", ] [[package]] diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 302713a21db..32104e6f00d 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -200,6 +200,11 @@ pub trait Emitter { true } + /// Checks if we can use colors in the current output stream. + fn supports_color(&self) -> bool { + false + } + fn source_map(&self) -> Option<&Lrc<SourceMap>>; /// Formats the substitutions of the primary_span @@ -504,6 +509,10 @@ impl Emitter for EmitterWriter { fn should_show_explain(&self) -> bool { !self.short_message } + + fn supports_color(&self) -> bool { + self.dst.supports_color() + } } /// An emitter that does nothing when emitting a diagnostic. @@ -2057,6 +2066,14 @@ impl Destination { Destination::Raw(ref mut t, true) => WritableDst::ColoredRaw(Ansi::new(t)), } } + + 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, + } + } } impl<'a> WritableDst<'a> { diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index 085f155ec25..b0f5bac6abd 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -20,6 +20,3 @@ regex = "1" [dev-dependencies] expect-test = "1.0" - -[target.'cfg(windows)'.dependencies] -termcolor = "1.0" diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index d1219c30b89..8ac24fdc8af 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -249,7 +249,8 @@ fn run_test( outdir: DirState, path: PathBuf, ) -> Result<(), TestFailure> { - let (test, line_offset) = make_test(test, Some(cratename), as_test_harness, opts, edition); + let (test, line_offset, supports_color) = + make_test(test, Some(cratename), as_test_harness, opts, edition); let output_file = outdir.path().join("rust_out"); @@ -294,38 +295,19 @@ fn run_test( path.to_str().expect("target path must be valid unicode").to_string() } }); - match options.error_format { - ErrorOutputType::HumanReadable(kind) => { - let (_, color_config) = kind.unzip(); - match color_config { - ColorConfig::Never => { - compiler.arg("--color").arg("never"); - } - ColorConfig::Always => { - compiler.arg("--color").arg("always"); - } - ColorConfig::Auto => { - #[cfg(windows)] - { - // This specific check is because old windows consoles require a connection - // to be able to display colors (and they don't support ANSI), which we - // cannot in here, so in case this is an old windows console, we can't - // display colors. - use crate::termcolor::{ColorChoice, StandardStream, WriteColor}; - if StandardStream::stdout(ColorChoice::Auto).is_synchronous() { - compiler.arg("--color").arg("never"); - } else { - compiler.arg("--color").arg("always"); - } - } - #[cfg(not(windows))] - { - compiler.arg("--color").arg("always"); - } - } + if let ErrorOutputType::HumanReadable(kind) = options.error_format { + let (_, color_config) = kind.unzip(); + match color_config { + ColorConfig::Never => { + compiler.arg("--color").arg("never"); + } + ColorConfig::Always => { + compiler.arg("--color").arg("always"); + } + ColorConfig::Auto => { + compiler.arg("--color").arg(if supports_color { "always" } else { "never" }); } } - _ => {} } compiler.arg("-"); @@ -396,18 +378,19 @@ fn run_test( } /// Transforms a test into code that can be compiled into a Rust binary, and returns the number of -/// lines before the test code begins. +/// lines before the test code begins as well as if the output stream supports colors or not. crate fn make_test( s: &str, cratename: Option<&str>, dont_insert_main: bool, opts: &TestOptions, edition: Edition, -) -> (String, usize) { +) -> (String, usize, bool) { let (crate_attrs, everything_else, crates) = partition_source(s); let everything_else = everything_else.trim(); let mut line_offset = 0; let mut prog = String::new(); + let mut supports_color = false; if opts.attrs.is_empty() && !opts.display_warnings { // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some @@ -433,7 +416,7 @@ crate fn make_test( // crate already is included. let result = rustc_driver::catch_fatal_errors(|| { rustc_span::with_session_globals(edition, || { - use rustc_errors::emitter::EmitterWriter; + use rustc_errors::emitter::{Emitter, EmitterWriter}; use rustc_errors::Handler; use rustc_parse::maybe_new_parser_from_source_str; use rustc_session::parse::ParseSess; @@ -447,6 +430,9 @@ crate fn make_test( let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false); + + supports_color = emitter.supports_color(); + // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser let handler = Handler::with_emitter(false, None, box emitter); let sess = ParseSess::with_span_handler(handler, sm); @@ -516,7 +502,7 @@ crate fn make_test( Err(ErrorReported) => { // If the parser panicked due to a fatal error, pass the test code through unchanged. // The error will be reported during compilation. - return (s.to_owned(), 0); + return (s.to_owned(), 0, false); } }; @@ -566,7 +552,7 @@ crate fn make_test( debug!("final doctest:\n{}", prog); - (prog, line_offset) + (prog, line_offset, supports_color) } // FIXME(aburka): use a real parser to deal with multiline attributes diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index cdb9aea5ad6..880c859dd1b 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -243,7 +243,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> { .collect::<Vec<Cow<'_, str>>>() .join("\n"); let krate = krate.as_ref().map(|s| &**s); - let (test, _) = doctest::make_test(&test, krate, false, &Default::default(), edition); + let (test, _, _) = + doctest::make_test(&test, krate, false, &Default::default(), edition); let channel = if test.contains("#![feature(") { "&version=nightly" } else { "" }; let edition_string = format!("&edition={}", edition); diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 1f46ac59e6a..a88efba77b4 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -54,10 +54,6 @@ extern crate rustc_target; extern crate rustc_trait_selection; extern crate rustc_typeck; extern crate test as testing; -#[macro_use] -extern crate tracing; -#[cfg(windows)] -extern crate termcolor; use std::default::Default; use std::env; |
