Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitter cleanups #121783

Merged
merged 14 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::io::prelude::*;
use std::io::{self, IsTerminal};
use std::iter;
use std::path::Path;
use termcolor::{Ansi, Buffer, BufferWriter, ColorChoice, ColorSpec, StandardStream};
use termcolor::{Buffer, BufferWriter, ColorChoice, ColorSpec, StandardStream};
use termcolor::{Color, WriteColor};

/// Default column width, used in tests and when terminal dimensions cannot be determined.
Expand All @@ -58,18 +58,6 @@ impl HumanReadableErrorType {
HumanReadableErrorType::AnnotateSnippet(cc) => (false, cc),
}
}
pub fn new_emitter(
self,
mut dst: Destination,
fallback_bundle: LazyFallbackBundle,
) -> HumanEmitter {
let (short, color_config) = self.unzip();
let color = color_config.suggests_using_colors();
if !dst.supports_color() && color {
dst = Box::new(Ansi::new(dst));
}
HumanEmitter::new(dst, fallback_bundle).short_message(short)
}
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -628,12 +616,6 @@ impl ColorConfig {
ColorConfig::Auto => ColorChoice::Never,
}
}
fn suggests_using_colors(self) -> bool {
match self {
ColorConfig::Always | ColorConfig::Auto => true,
ColorConfig::Never => false,
}
}
}

/// Handles the writing of `HumanReadableErrorType::Default` and `HumanReadableErrorType::Short`
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
use rustc_span::source_map::{FilePathMapping, SourceMap};
use termcolor::{ColorSpec, WriteColor};

use crate::emitter::{should_show_source_code, Emitter, HumanReadableErrorType};
use crate::emitter::{
should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter,
HumanReadableErrorType,
};
use crate::registry::Registry;
use crate::translation::{to_fluent_args, Translate};
use crate::{
Expand Down Expand Up @@ -405,8 +408,17 @@ impl Diagnostic {
.collect();

let buf = BufWriter::default();
je.json_rendered
.new_emitter(Box::new(buf.clone()), je.fallback_bundle.clone())
let mut dst: Destination = Box::new(buf.clone());
let (short, color_config) = je.json_rendered.unzip();
let color = match color_config {
ColorConfig::Always | ColorConfig::Auto => true,
ColorConfig::Never => false,
};
if color {
dst = Box::new(termcolor::Ansi::new(dst));
}
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
HumanEmitter::new(dst, je.fallback_bundle.clone())
.short_message(short)
.sm(Some(je.sm.clone()))
.fluent_bundle(je.fluent_bundle.clone())
.diagnostic_width(je.diagnostic_width)
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_errors/src/json/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::*;

use crate::emitter::ColorConfig;
use crate::DiagCtxt;
use rustc_span::BytePos;

Expand Down