Skip to content

Commit c523b65

Browse files
committed
fix(rustdoc): Color doctest errors
1 parent 2636cb4 commit c523b65

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,14 +3466,9 @@ impl Drop for Buffy {
34663466

34673467
pub fn stderr_destination(color: ColorConfig) -> Destination {
34683468
let buffer_writer = std::io::stderr();
3469-
let choice = color.to_color_choice();
34703469
// We need to resolve `ColorChoice::Auto` before `Box`ing since
34713470
// `ColorChoice::Auto` on `dyn Write` will always resolve to `Never`
3472-
let choice = if matches!(choice, ColorChoice::Auto) {
3473-
AutoStream::choice(&buffer_writer)
3474-
} else {
3475-
choice
3476-
};
3471+
let choice = get_stderr_color_choice(color, &buffer_writer);
34773472
// On Windows we'll be performing global synchronization on the entire
34783473
// system for emitting rustc errors, so there's no need to buffer
34793474
// anything.
@@ -3488,6 +3483,11 @@ pub fn stderr_destination(color: ColorConfig) -> Destination {
34883483
}
34893484
}
34903485

3486+
pub fn get_stderr_color_choice(color: ColorConfig, stderr: &std::io::Stderr) -> ColorChoice {
3487+
let choice = color.to_color_choice();
3488+
if matches!(choice, ColorChoice::Auto) { AutoStream::choice(stderr) } else { choice }
3489+
}
3490+
34913491
/// On Windows, BRIGHT_BLUE is hard to read on black. Use cyan instead.
34923492
///
34933493
/// See #36178.

src/librustdoc/doctest/make.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::sync::Arc;
88
use rustc_ast::token::{Delimiter, TokenKind};
99
use rustc_ast::tokenstream::TokenTree;
1010
use rustc_ast::{self as ast, AttrStyle, HasAttrs, StmtKind};
11-
use rustc_errors::emitter::stderr_destination;
12-
use rustc_errors::{AutoStream, ColorConfig, DiagCtxtHandle};
11+
use rustc_errors::emitter::get_stderr_color_choice;
12+
use rustc_errors::{AutoStream, ColorChoice, ColorConfig, DiagCtxtHandle};
1313
use rustc_parse::lexer::StripTokens;
1414
use rustc_parse::new_parser_from_source_str;
1515
use rustc_session::parse::ParseSess;
@@ -446,7 +446,7 @@ fn parse_source(
446446
span: Span,
447447
) -> Result<ParseSourceInfo, ()> {
448448
use rustc_errors::DiagCtxt;
449-
use rustc_errors::emitter::{Emitter, HumanEmitter};
449+
use rustc_errors::emitter::HumanEmitter;
450450
use rustc_span::source_map::FilePathMapping;
451451

452452
let mut info =
@@ -458,9 +458,12 @@ fn parse_source(
458458

459459
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
460460
let translator = rustc_driver::default_translator();
461-
info.supports_color =
462-
HumanEmitter::new(stderr_destination(ColorConfig::Auto), translator.clone())
463-
.supports_color();
461+
let supports_color = match get_stderr_color_choice(ColorConfig::Auto, &std::io::stderr()) {
462+
ColorChoice::Auto => unreachable!(),
463+
ColorChoice::AlwaysAnsi | ColorChoice::Always => true,
464+
ColorChoice::Never => false,
465+
};
466+
info.supports_color = supports_color;
464467
// Any errors in parsing should also appear when the doctest is compiled for real, so just
465468
// send all the errors that the parser emits directly into a `Sink` instead of stderr.
466469
let emitter = HumanEmitter::new(AutoStream::never(Box::new(io::sink())), translator);

0 commit comments

Comments
 (0)