diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index f20239b6fcb12..4cab7e407eec2 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -3466,14 +3466,9 @@ impl Drop for Buffy { pub fn stderr_destination(color: ColorConfig) -> Destination { let buffer_writer = std::io::stderr(); - let choice = color.to_color_choice(); // We need to resolve `ColorChoice::Auto` before `Box`ing since // `ColorChoice::Auto` on `dyn Write` will always resolve to `Never` - let choice = if matches!(choice, ColorChoice::Auto) { - AutoStream::choice(&buffer_writer) - } else { - choice - }; + let choice = get_stderr_color_choice(color, &buffer_writer); // On Windows we'll be performing global synchronization on the entire // system for emitting rustc errors, so there's no need to buffer // anything. @@ -3488,6 +3483,11 @@ pub fn stderr_destination(color: ColorConfig) -> Destination { } } +pub fn get_stderr_color_choice(color: ColorConfig, stderr: &std::io::Stderr) -> ColorChoice { + let choice = color.to_color_choice(); + if matches!(choice, ColorChoice::Auto) { AutoStream::choice(stderr) } else { choice } +} + /// On Windows, BRIGHT_BLUE is hard to read on black. Use cyan instead. /// /// See #36178. diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs index 6c177f942fb05..e9f5024e494d1 100644 --- a/src/librustdoc/doctest/make.rs +++ b/src/librustdoc/doctest/make.rs @@ -8,8 +8,8 @@ use std::sync::Arc; use rustc_ast::token::{Delimiter, TokenKind}; use rustc_ast::tokenstream::TokenTree; use rustc_ast::{self as ast, AttrStyle, HasAttrs, StmtKind}; -use rustc_errors::emitter::stderr_destination; -use rustc_errors::{AutoStream, ColorConfig, DiagCtxtHandle}; +use rustc_errors::emitter::get_stderr_color_choice; +use rustc_errors::{AutoStream, ColorChoice, ColorConfig, DiagCtxtHandle}; use rustc_parse::lexer::StripTokens; use rustc_parse::new_parser_from_source_str; use rustc_session::parse::ParseSess; @@ -446,7 +446,7 @@ fn parse_source( span: Span, ) -> Result { use rustc_errors::DiagCtxt; - use rustc_errors::emitter::{Emitter, HumanEmitter}; + use rustc_errors::emitter::HumanEmitter; use rustc_span::source_map::FilePathMapping; let mut info = @@ -458,9 +458,12 @@ fn parse_source( let sm = Arc::new(SourceMap::new(FilePathMapping::empty())); let translator = rustc_driver::default_translator(); - info.supports_color = - HumanEmitter::new(stderr_destination(ColorConfig::Auto), translator.clone()) - .supports_color(); + let supports_color = match get_stderr_color_choice(ColorConfig::Auto, &std::io::stderr()) { + ColorChoice::Auto => unreachable!(), + ColorChoice::AlwaysAnsi | ColorChoice::Always => true, + ColorChoice::Never => false, + }; + info.supports_color = supports_color; // Any errors in parsing should also appear when the doctest is compiled for real, so just // send all the errors that the parser emits directly into a `Sink` instead of stderr. let emitter = HumanEmitter::new(AutoStream::never(Box::new(io::sink())), translator);