Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
use rustc_errors::emitter::Emitter;
use rustc_errors::{
Diag, DiagArgMap, DiagCtxt, DiagCtxtHandle, DiagMessage, ErrCode, FatalError, FatalErrorMarker,
Level, MultiSpan, Style, Suggestions, catch_fatal_errors,
Level, MultiSpan, Style, Sublevel, Suggestions, catch_fatal_errors,
};
use rustc_fs_util::link_or_copy;
use rustc_incremental::{copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir_sess};
Expand Down Expand Up @@ -1217,7 +1217,7 @@ struct Diagnostic {
// missing the following fields from `rustc_errors::Subdiag`.
// - `span`: it doesn't impl `Send`.
struct Subdiagnostic {
level: Level,
level: Sublevel,
messages: Vec<(DiagMessage, Style)>,
}

Expand Down
42 changes: 20 additions & 22 deletions compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//!
//! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/

use std::borrow::Cow;
use std::fmt::Debug;
use std::io;
use std::io::Write;
Expand All @@ -29,7 +28,7 @@ use crate::emitter::{
use crate::formatting::{format_diag_message, format_diag_messages};
use crate::{
CodeSuggestion, DiagInner, DiagMessage, Emitter, ErrCode, Level, MultiSpan, Style, Subdiag,
SuggestionStyle, TerminalUrl,
Sublevel, SuggestionStyle, TerminalUrl,
};

/// Generates diagnostics using annotate-snippet
Expand Down Expand Up @@ -126,14 +125,23 @@ fn annotation_level_for_level(level: Level) -> annotate_snippets::level::Level<'
}
Level::Fatal | Level::Error => annotate_snippets::level::ERROR,
Level::ForceWarning | Level::Warning => annotate_snippets::Level::WARNING,
Level::Note | Level::OnceNote => annotate_snippets::Level::NOTE,
Level::Help | Level::OnceHelp => annotate_snippets::Level::HELP,
Level::Note => annotate_snippets::Level::NOTE,
Level::Help => annotate_snippets::Level::HELP,
Level::FailureNote => annotate_snippets::Level::NOTE.no_name(),
Level::Allow => panic!("Should not call with Allow"),
Level::Expect => panic!("Should not call with Expect"),
}
}

fn annotation_level_for_sublevel(level: Sublevel) -> annotate_snippets::level::Level<'static> {
match level {
Sublevel::Error => annotate_snippets::Level::ERROR,
Sublevel::Warning => annotate_snippets::Level::WARNING,
Sublevel::Note | Sublevel::OnceNote => annotate_snippets::Level::NOTE,
Sublevel::Help | Sublevel::OnceHelp => annotate_snippets::Level::HELP,
}
}

impl AnnotateSnippetEmitter {
pub fn new(dst: Destination) -> Self {
Self {
Expand Down Expand Up @@ -166,9 +174,7 @@ impl AnnotateSnippetEmitter {
// If at least one portion of the message is styled, we need to
// "pre-style" the message
let mut title = if msgs.iter().any(|(_, style)| style != &crate::Style::NoStyle) {
annotation_level
.clone()
.secondary_title(Cow::Owned(self.pre_style_msgs(msgs, *level, args)))
annotation_level.clone().secondary_title(self.pre_style_msgs(msgs, args))
} else {
annotation_level.clone().primary_title(format_diag_messages(msgs, args))
};
Expand All @@ -186,8 +192,8 @@ impl AnnotateSnippetEmitter {
// If we don't have span information, emit and exit
let Some(sm) = self.sm.as_ref() else {
group = group.elements(children.iter().map(|c| {
let msg = format_diag_messages(&c.messages, args).to_string();
let level = annotation_level_for_level(c.level);
let msg = format_diag_messages(&c.messages, args);
let level = annotation_level_for_sublevel(c.level);
level.message(msg)
}));

Expand Down Expand Up @@ -250,12 +256,12 @@ impl AnnotateSnippetEmitter {
}

for c in children {
let level = annotation_level_for_level(c.level);
let level = annotation_level_for_sublevel(c.level);

// If at least one portion of the message is styled, we need to
// "pre-style" the message
let msg = if c.messages.iter().any(|(_, style)| style != &crate::Style::NoStyle) {
Cow::Owned(self.pre_style_msgs(&c.messages, c.level, args))
self.pre_style_msgs(&c.messages, args)
} else {
format_diag_messages(&c.messages, args)
};
Expand Down Expand Up @@ -309,10 +315,7 @@ impl AnnotateSnippetEmitter {
// do not display this suggestion, it is meant only for tools
}
SuggestionStyle::HideCodeAlways => {
let msg = format_diag_messages(
&[(suggestion.msg.to_owned(), Style::HeaderMsg)],
args,
);
let msg = format_diag_message(&suggestion.msg, args).into_owned();
group = group.element(annotate_snippets::Level::HELP.message(msg));
}
SuggestionStyle::HideCodeInline
Expand Down Expand Up @@ -544,16 +547,11 @@ impl AnnotateSnippetEmitter {
.short_message(self.short_message)
}

fn pre_style_msgs(
&self,
msgs: &[(DiagMessage, Style)],
level: Level,
args: &DiagArgMap,
) -> String {
fn pre_style_msgs(&self, msgs: &[(DiagMessage, Style)], args: &DiagArgMap) -> String {
msgs.iter()
.filter_map(|(m, style)| {
let text = format_diag_message(m, args);
let style = style.anstyle(level);
let style = style.anstyle();
if text.is_empty() { None } else { Some(format!("{style}{text}{style:#}")) }
})
.collect()
Expand Down
46 changes: 25 additions & 21 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use tracing::debug;

use crate::{
CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
MultiSpan, StashKey, Style, Substitution, SubstitutionPart, SuggestionStyle, Suggestions,
MultiSpan, StashKey, Style, Sublevel, Substitution, SubstitutionPart, SuggestionStyle,
Suggestions,
};

/// Trait for types that `Diag::emit` can return as a "guarantee" (or "proof")
Expand Down Expand Up @@ -314,9 +315,7 @@ impl DiagInner {
Level::ForceWarning
| Level::Warning
| Level::Note
| Level::OnceNote
| Level::Help
| Level::OnceHelp
| Level::FailureNote
| Level::Allow
| Level::Expect => false,
Expand All @@ -343,7 +342,12 @@ impl DiagInner {
}
}

pub(crate) fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
pub(crate) fn sub(
&mut self,
level: Sublevel,
message: impl Into<DiagMessage>,
span: MultiSpan,
) {
let sub = Subdiag { level, messages: vec![(message.into(), Style::NoStyle)], span };
self.children.push(sub);
}
Expand All @@ -367,7 +371,7 @@ impl DiagInner {
pub fn emitted_at_sub_diag(&self) -> Subdiag {
let track = format!("-Ztrack-diagnostics: created at {}", self.emitted_at);
Subdiag {
level: crate::Level::Note,
level: crate::Sublevel::Note,
messages: vec![(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)],
span: MultiSpan::new(),
}
Expand Down Expand Up @@ -420,7 +424,7 @@ impl PartialEq for DiagInner {
/// For example, a note attached to an error.
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
pub struct Subdiag {
pub level: Level,
pub level: Sublevel,
pub messages: Vec<(DiagMessage, Style)>,
pub span: MultiSpan,
}
Expand Down Expand Up @@ -701,12 +705,12 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
with_fn! { with_note,
/// Add a note attached to this diagnostic.
pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Note, msg, MultiSpan::new());
self.sub(Sublevel::Note, msg, MultiSpan::new());
self
} }

pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
self.sub_with_highlights(Sublevel::Note, msg, MultiSpan::new());
self
}

Expand All @@ -715,13 +719,13 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
span: impl Into<MultiSpan>,
msg: Vec<StringPart>,
) -> &mut Self {
self.sub_with_highlights(Level::Note, msg, span.into());
self.sub_with_highlights(Sublevel::Note, msg, span.into());
self
}

/// This is like [`Diag::note()`], but it's only printed once.
pub fn note_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::OnceNote, msg, MultiSpan::new());
self.sub(Sublevel::OnceNote, msg, MultiSpan::new());
self
}

Expand All @@ -733,7 +737,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>,
) -> &mut Self {
self.sub(Level::Note, msg, sp.into());
self.sub(Sublevel::Note, msg, sp.into());
self
} }

Expand All @@ -744,14 +748,14 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
sp: S,
msg: impl Into<DiagMessage>,
) -> &mut Self {
self.sub(Level::OnceNote, msg, sp.into());
self.sub(Sublevel::OnceNote, msg, sp.into());
self
}

with_fn! { with_warn,
/// Add a warning attached to this diagnostic.
pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Warning, msg, MultiSpan::new());
self.sub(Sublevel::Warning, msg, MultiSpan::new());
self
} }

Expand All @@ -762,26 +766,26 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
sp: S,
msg: impl Into<DiagMessage>,
) -> &mut Self {
self.sub(Level::Warning, msg, sp.into());
self.sub(Sublevel::Warning, msg, sp.into());
self
}

with_fn! { with_help,
/// Add a help message attached to this diagnostic.
pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::Help, msg, MultiSpan::new());
self.sub(Sublevel::Help, msg, MultiSpan::new());
self
} }

/// This is like [`Diag::help()`], but it's only printed once.
pub fn help_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::OnceHelp, msg, MultiSpan::new());
self.sub(Sublevel::OnceHelp, msg, MultiSpan::new());
self
}

/// Add a help message attached to this diagnostic with a customizable highlighted message.
pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
self.sub_with_highlights(Sublevel::Help, msg, MultiSpan::new());
self
}

Expand All @@ -791,7 +795,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
span: impl Into<MultiSpan>,
msg: Vec<StringPart>,
) -> &mut Self {
self.sub_with_highlights(Level::Help, msg, span.into());
self.sub_with_highlights(Sublevel::Help, msg, span.into());
self
}

Expand All @@ -803,7 +807,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>,
) -> &mut Self {
self.sub(Level::Help, msg, sp.into());
self.sub(Sublevel::Help, msg, sp.into());
self
} }

Expand Down Expand Up @@ -1226,13 +1230,13 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
/// public methods above.
///
/// Used by `proc_macro_server` for implementing `server::Diagnostic`.
pub fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
pub fn sub(&mut self, level: Sublevel, message: impl Into<DiagMessage>, span: MultiSpan) {
self.deref_mut().sub(level, message, span);
}

/// Convenience function for internal use, clients should use one of the
/// public methods above.
fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
fn sub_with_highlights(&mut self, level: Sublevel, messages: Vec<StringPart>, span: MultiSpan) {
let messages = messages.into_iter().map(|m| (m.content.into(), m.style)).collect();
let sub = Subdiag { level, messages, span };
self.children.push(sub);
Expand Down
34 changes: 6 additions & 28 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use tracing::{debug, warn};
use crate::formatting::format_diag_message;
use crate::timings::TimingRecord;
use crate::{
CodeSuggestion, DiagInner, DiagMessage, Level, MultiSpan, Style, Subdiag, SuggestionStyle,
CodeSuggestion, DiagInner, DiagMessage, Level, MultiSpan, Style, Subdiag, Sublevel,
SuggestionStyle,
};

/// Describes the way the content of the `rendered` field of the json output is generated
Expand Down Expand Up @@ -209,7 +210,7 @@ pub trait Emitter {
);

children.push(Subdiag {
level: Level::Note,
level: Sublevel::Note,
messages: vec![(DiagMessage::from(msg), Style::NoStyle)],
span: MultiSpan::new(),
});
Expand Down Expand Up @@ -379,7 +380,7 @@ impl Emitter for EmitterWithNote {
}

fn emit_diagnostic(&mut self, mut diag: DiagInner) {
diag.sub(Level::Note, self.note.clone(), MultiSpan::new());
diag.sub(Sublevel::Note, self.note.clone(), MultiSpan::new());
self.emitter.emit_diagnostic(diag);
}
}
Expand Down Expand Up @@ -555,33 +556,10 @@ pub fn get_stderr_color_choice(color: ColorConfig, stderr: &std::io::Stderr) ->
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.
const BRIGHT_BLUE: anstyle::Style = if cfg!(windows) {
AnsiColor::BrightCyan.on_default()
} else {
AnsiColor::BrightBlue.on_default()
};

impl Style {
pub(crate) fn anstyle(&self, lvl: Level) -> anstyle::Style {
pub(crate) fn anstyle(&self) -> anstyle::Style {
match self {
Style::Addition => AnsiColor::BrightGreen.on_default(),
Style::Removal => AnsiColor::BrightRed.on_default(),
Style::LineAndColumn => anstyle::Style::new(),
Style::LineNumber => BRIGHT_BLUE.effects(Effects::BOLD),
Style::Quotation => anstyle::Style::new(),
Style::MainHeaderMsg => if cfg!(windows) {
AnsiColor::BrightWhite.on_default()
} else {
anstyle::Style::new()
}
.effects(Effects::BOLD),
Style::UnderlinePrimary | Style::LabelPrimary => lvl.color().effects(Effects::BOLD),
Style::UnderlineSecondary | Style::LabelSecondary => BRIGHT_BLUE.effects(Effects::BOLD),
Style::HeaderMsg | Style::NoStyle => anstyle::Style::new(),
Style::Level(lvl) => lvl.color().effects(Effects::BOLD),
Style::NoStyle => anstyle::Style::new(),
Style::Highlight => AnsiColor::Magenta.on_default().effects(Effects::BOLD),
}
}
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_errors/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ fn to_fluent_args<'iter>(iter: impl Iterator<Item = DiagArg<'iter>>) -> FluentAr
}

/// Convert `DiagMessage`s to a string
pub fn format_diag_messages(
messages: &[(DiagMessage, Style)],
args: &DiagArgMap,
) -> Cow<'static, str> {
Cow::Owned(messages.iter().map(|(m, _)| format_diag_message(m, args)).collect::<String>())
pub fn format_diag_messages(messages: &[(DiagMessage, Style)], args: &DiagArgMap) -> String {
messages.iter().map(|(m, _)| format_diag_message(m, args)).collect::<String>()
}

/// Convert a `DiagMessage` to a string
Expand Down
Loading
Loading