Skip to content
Closed
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
28 changes: 11 additions & 17 deletions compiler/rustc_codegen_ssa/src/back/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,10 @@ pub(super) fn search_for_section<'a>(
fn add_gnu_property_note(
file: &mut write::Object<'static>,
architecture: Architecture,
binary_format: BinaryFormat,
endianness: Endianness,
) {
// check bti protection
if binary_format != BinaryFormat::Elf
|| !matches!(architecture, Architecture::X86_64 | Architecture::Aarch64)
{
// Only X86_64 and Aarch64 require a GNU property note.
if !matches!(architecture, Architecture::X86_64 | Architecture::Aarch64) {
return;
}

Expand Down Expand Up @@ -253,12 +250,14 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static

file.set_mangling(original_mangling);
}
let e_flags = elf_e_flags(architecture, sess);
// adapted from LLVM's `MCELFObjectTargetWriter::getOSABI`
let os_abi = elf_os_abi(sess);
let abi_version = 0;
add_gnu_property_note(&mut file, architecture, binary_format, endianness);
file.flags = FileFlags::Elf { os_abi, abi_version, e_flags };
if binary_format == BinaryFormat::Elf {
let e_flags = elf_e_flags(architecture, sess);
// adapted from LLVM's `MCELFObjectTargetWriter::getOSABI`
let os_abi = elf_os_abi(sess);
let abi_version = 0;
add_gnu_property_note(&mut file, architecture, endianness);
file.flags = FileFlags::Elf { os_abi, abi_version, e_flags };
}
Some(file)
}

Expand Down Expand Up @@ -382,7 +381,6 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
}
}
Architecture::PowerPc64 => {
const EF_PPC64_ABI_UNKNOWN: u32 = 0;
const EF_PPC64_ABI_ELF_V1: u32 = 1;
const EF_PPC64_ABI_ELF_V2: u32 = 2;

Expand All @@ -392,11 +390,7 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
// which leads to broken binaries if ELFv1 is used for the object files.
LlvmAbi::ElfV1 => EF_PPC64_ABI_ELF_V1,
LlvmAbi::ElfV2 => EF_PPC64_ABI_ELF_V2,
_ if sess.target.options.binary_format.to_object() == BinaryFormat::Elf => {
bug!("invalid ABI specified for this PPC64 ELF target");
}
// Fall back
_ => EF_PPC64_ABI_UNKNOWN,
_ => bug!("invalid ABI specified for this PPC64 ELF target"),
}
}
Architecture::Sparc32Plus => elf::EF_SPARC_32PLUS,
Expand Down
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 @@ -321,9 +322,7 @@ impl DiagInner {
Level::ForceWarning
| Level::Warning
| Level::Note
| Level::OnceNote
| Level::Help
| Level::OnceHelp
| Level::FailureNote
| Level::Allow
| Level::Expect => false,
Expand All @@ -350,7 +349,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 @@ -374,7 +378,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 @@ -427,7 +431,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 @@ -708,12 +712,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 @@ -722,13 +726,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 @@ -740,7 +744,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 @@ -751,14 +755,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 @@ -769,26 +773,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 @@ -798,7 +802,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 @@ -810,7 +814,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 @@ -1233,13 +1237,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
Loading
Loading