Skip to content

lazify backtrace formatting for delayed diagnostics #84965

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

Merged
merged 1 commit into from
May 6, 2021
Merged
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
27 changes: 22 additions & 5 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl error::Error for ExplicitBug {}

pub use diagnostic::{Diagnostic, DiagnosticId, DiagnosticStyledString, SubDiagnostic};
pub use diagnostic_builder::DiagnosticBuilder;
use std::backtrace::Backtrace;

/// A handler deals with errors and other compiler output.
/// Certain errors (fatal, bug, unimpl) may cause immediate exit,
Expand All @@ -317,7 +318,7 @@ struct HandlerInner {
deduplicated_err_count: usize,
emitter: Box<dyn Emitter + sync::Send>,
delayed_span_bugs: Vec<Diagnostic>,
delayed_good_path_bugs: Vec<Diagnostic>,
delayed_good_path_bugs: Vec<DelayedDiagnostic>,

/// This set contains the `DiagnosticId` of all emitted diagnostics to avoid
/// emitting the same diagnostic with extended help (`--teach`) twice, which
Expand Down Expand Up @@ -388,7 +389,7 @@ impl Drop for HandlerInner {
if !self.has_any_message() {
let bugs = std::mem::replace(&mut self.delayed_good_path_bugs, Vec::new());
self.flush_delayed(
bugs,
bugs.into_iter().map(DelayedDiagnostic::decorate).collect(),
"no warnings or errors encountered even though `delayed_good_path_bugs` issued",
);
}
Expand Down Expand Up @@ -968,12 +969,12 @@ impl HandlerInner {
}

fn delay_good_path_bug(&mut self, msg: &str) {
let mut diagnostic = Diagnostic::new(Level::Bug, msg);
let diagnostic = Diagnostic::new(Level::Bug, msg);
if self.flags.report_delayed_bugs {
self.emit_diagnostic(&diagnostic);
}
diagnostic.note(&format!("delayed at {}", std::backtrace::Backtrace::force_capture()));
self.delayed_good_path_bugs.push(diagnostic);
let backtrace = std::backtrace::Backtrace::force_capture();
self.delayed_good_path_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
}

fn failure(&mut self, msg: &str) {
Expand Down Expand Up @@ -1042,6 +1043,22 @@ impl HandlerInner {
}
}

struct DelayedDiagnostic {
inner: Diagnostic,
note: Backtrace,
}

impl DelayedDiagnostic {
fn with_backtrace(diagnostic: Diagnostic, backtrace: Backtrace) -> Self {
DelayedDiagnostic { inner: diagnostic, note: backtrace }
}

fn decorate(mut self) -> Diagnostic {
self.inner.note(&format!("delayed at {}", self.note));
self.inner
}
}

#[derive(Copy, PartialEq, Clone, Hash, Debug, Encodable, Decodable)]
pub enum Level {
Bug,
Expand Down