Skip to content

Commit ba8bdd7

Browse files
authored
Unrolled build for #148834
Rollup merge of #148834 - Muscraft:fix-doctest-colors, r=fmease fix(rustdoc): Color doctest errors `@fmease's` [Deep analysis](#148749 (comment)) on the problem > Yeah, here's a deep analysis by me from a few weeks back of what's going on ([#148101 (comment)](#148101 (comment))): > > > […] > > However, since said PR ([#147207](#147207): migrating coloring crates), `HumanEmitter::supports_color()` unconditionally(!) returns `false` (in fact, `Emitter::supports_color` is no longer used by anyone else and should be removed), so there's no reason to keep it. Rephrased, since that PR all compiler diagnostics for doctests are uncolored. > > You could argue that I should keep it and patch `supports_color` in rustc to "work again". However, I'd rather rework our doctest coloring wholesale in a separate PR. At least before that migration PR, our setup was quite busted: > > > > 1. First of all, it didn't query+set `supports_color` for syntactically invalid doctests, so syntax errors were always shown without color (contrary to e.g., name resolution errors). > > 2. Second of all, calling `supports_color()` here was quite frankly wrong: Piping the output of `rustdoc … --test` into a file (or `| cat` or whatever) did **not** suppress colors. I'm not actually sure if we can ever address that nicely (without stripping ANSI codes after the fact) since we pass that diagnostic to `libtest`, right? I might very well be wrong here, maybe it's a non-issue. <hr> ```rust /// ``` /// foo /// ``` fn foo() {} ``` ``` rustdoc --test lib.rs ``` Stable: <img width="377" height="290" alt="stable" src="https://github.com/user-attachments/assets/cd20f947-b58d-42db-8735-797613baa9cc" /> Beta: <img width="377" height="290" alt="beta" src="https://github.com/user-attachments/assets/f02588fd-41d2-4642-b03a-5554a68671eb" /> Nightly: <img width="377" height="290" alt="nightly" src="https://github.com/user-attachments/assets/871cb417-f47e-4058-8a76-3bcd538ce141" /> After: <img width="377" height="290" alt="after" src="https://github.com/user-attachments/assets/5734c01f-3f1c-44bb-9404-628c0c33b440" /> Note: This will need to be backported to `beta` Fixes: #148749
2 parents 11339a0 + c523b65 commit ba8bdd7

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)