Skip to content

Commit

Permalink
Unrolled build for rust-lang#121783
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#121783 - nnethercote:emitter-cleanups, r=oli-obk

Emitter cleanups

Some cleanups I made when reading emitter code. In particular, `HumanEmitter` and `JsonEmitter` have gone from three constructors to one.

r? `@oli-obk`
  • Loading branch information
rust-timer authored Feb 29, 2024
2 parents 384d26f + 607bf65 commit 74932d3
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 219 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {

impl<B: WriteBackendMethods> CodegenContext<B> {
pub fn create_dcx(&self) -> DiagCtxt {
DiagCtxt::with_emitter(Box::new(self.diag_emitter.clone()))
DiagCtxt::new(Box::new(self.diag_emitter.clone()))
}

pub fn config(&self, kind: ModuleKind) -> &ModuleConfig {
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
use rustc_data_structures::profiling::{
get_resident_set_size, print_time_passes_entry, TimePassesFormat,
};
use rustc_errors::emitter::stderr_destination;
use rustc_errors::registry::Registry;
use rustc_errors::{
markdown, ColorConfig, DiagCtxt, ErrCode, ErrorGuaranteed, FatalError, PResult,
Expand Down Expand Up @@ -1384,11 +1385,11 @@ fn report_ice(
) {
let fallback_bundle =
rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
let emitter = Box::new(rustc_errors::emitter::HumanEmitter::stderr(
rustc_errors::ColorConfig::Auto,
let emitter = Box::new(rustc_errors::emitter::HumanEmitter::new(
stderr_destination(rustc_errors::ColorConfig::Auto),
fallback_bundle,
));
let dcx = rustc_errors::DiagCtxt::with_emitter(emitter);
let dcx = rustc_errors::DiagCtxt::new(emitter);

// a .span_bug or .bug call has already printed what
// it wants to print.
Expand Down
61 changes: 16 additions & 45 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ use crate::{
FluentBundle, LazyFallbackBundle, Level, MultiSpan, Subdiag, SubstitutionHighlight,
SuggestionStyle, TerminalUrl,
};
use rustc_lint_defs::pluralize;

use derive_setters::Setters;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
use rustc_error_messages::{FluentArgs, SpanLabel};
use rustc_lint_defs::pluralize;
use rustc_span::hygiene::{ExpnKind, MacroKind};
use std::borrow::Cow;
use std::cmp::{max, min, Reverse};
Expand All @@ -35,7 +34,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 +57,6 @@ impl HumanReadableErrorType {
HumanReadableErrorType::AnnotateSnippet(cc) => (false, cc),
}
}
pub fn new_emitter(
self,
mut dst: Box<dyn WriteColor + Send>,
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 @@ -130,8 +117,8 @@ impl Margin {
fn was_cut_right(&self, line_len: usize) -> bool {
let right =
if self.computed_right == self.span_right || self.computed_right == self.label_right {
// Account for the "..." padding given above. Otherwise we end up with code lines that
// do fit but end in "..." as if they were trimmed.
// Account for the "..." padding given above. Otherwise we end up with code lines
// that do fit but end in "..." as if they were trimmed.
self.computed_right - 6
} else {
self.computed_right
Expand Down Expand Up @@ -628,12 +615,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 All @@ -657,19 +638,14 @@ pub struct HumanEmitter {
}

#[derive(Debug)]
pub struct FileWithAnnotatedLines {
pub file: Lrc<SourceFile>,
pub lines: Vec<Line>,
pub(crate) struct FileWithAnnotatedLines {
pub(crate) file: Lrc<SourceFile>,
pub(crate) lines: Vec<Line>,
multiline_depth: usize,
}

impl HumanEmitter {
pub fn stderr(color_config: ColorConfig, fallback_bundle: LazyFallbackBundle) -> HumanEmitter {
let dst = from_stderr(color_config);
Self::create(dst, fallback_bundle)
}

fn create(dst: Destination, fallback_bundle: LazyFallbackBundle) -> HumanEmitter {
pub fn new(dst: Destination, fallback_bundle: LazyFallbackBundle) -> HumanEmitter {
HumanEmitter {
dst: IntoDynSyncSend(dst),
sm: None,
Expand All @@ -686,13 +662,6 @@ impl HumanEmitter {
}
}

pub fn new(
dst: Box<dyn WriteColor + Send>,
fallback_bundle: LazyFallbackBundle,
) -> HumanEmitter {
Self::create(dst, fallback_bundle)
}

fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> {
if self.ui_testing {
Cow::Borrowed(ANONYMIZED_LINE_NUM)
Expand Down Expand Up @@ -724,8 +693,9 @@ impl HumanEmitter {
.skip(left)
.take_while(|ch| {
// Make sure that the trimming on the right will fall within the terminal width.
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is.
// For now, just accept that sometimes the code line will be longer than desired.
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char`
// is. For now, just accept that sometimes the code line will be longer than
// desired.
let next = unicode_width::UnicodeWidthChar::width(*ch).unwrap_or(1);
if taken + next > right - left {
return false;
Expand Down Expand Up @@ -2228,8 +2198,8 @@ impl HumanEmitter {
buffer.puts(*row_num - 1, max_line_num_len + 3, &line, Style::NoStyle);
*row_num += 1;
}
// If the last line is exactly equal to the line we need to add, we can skip both of them.
// This allows us to avoid output like the following:
// If the last line is exactly equal to the line we need to add, we can skip both of
// them. This allows us to avoid output like the following:
// 2 - &
// 2 + if true { true } else { false }
// 3 - if true { true } else { false }
Expand Down Expand Up @@ -2586,6 +2556,7 @@ fn num_overlap(
let extra = if inclusive { 1 } else { 0 };
(b_start..b_end + extra).contains(&a_start) || (a_start..a_end + extra).contains(&b_start)
}

fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
num_overlap(
a1.start_col.display,
Expand Down Expand Up @@ -2632,7 +2603,7 @@ fn emit_to_destination(
Ok(())
}

pub type Destination = Box<(dyn WriteColor + Send)>;
pub type Destination = Box<dyn WriteColor + Send>;

struct Buffy {
buffer_writer: BufferWriter,
Expand Down Expand Up @@ -2674,7 +2645,7 @@ impl WriteColor for Buffy {
}
}

fn from_stderr(color: ColorConfig) -> Destination {
pub fn stderr_destination(color: ColorConfig) -> Destination {
let choice = color.to_color_choice();
// On Windows we'll be performing global synchronization on the entire
// system for emitting rustc errors, so there's no need to buffer
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_errors/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ impl<'args> TranslateError<'args> {
pub fn message(id: &'args Cow<'args, str>, args: &'args FluentArgs<'args>) -> Self {
Self::One { id, args, kind: TranslateErrorKind::MessageMissing }
}

pub fn primary(id: &'args Cow<'args, str>, args: &'args FluentArgs<'args>) -> Self {
Self::One { id, args, kind: TranslateErrorKind::PrimaryBundleMissing }
}

pub fn attribute(
id: &'args Cow<'args, str>,
args: &'args FluentArgs<'args>,
attr: &'args str,
) -> Self {
Self::One { id, args, kind: TranslateErrorKind::AttributeMissing { attr } }
}

pub fn value(id: &'args Cow<'args, str>, args: &'args FluentArgs<'args>) -> Self {
Self::One { id, args, kind: TranslateErrorKind::ValueMissing }
}
Expand Down
Loading

0 comments on commit 74932d3

Please sign in to comment.