Skip to content

Commit eb79bc0

Browse files
Good path bugs are just a flavor of delayed bug
1 parent 3330940 commit eb79bc0

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
8585
/// Maps `Diagnostic::Level` to `snippet::AnnotationType`
8686
fn annotation_type_for_level(level: Level) -> AnnotationType {
8787
match level {
88-
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error => AnnotationType::Error,
88+
Level::Bug | Level::DelayedBug(_) | Level::Fatal | Level::Error => AnnotationType::Error,
8989
Level::ForceWarning(_) | Level::Warning => AnnotationType::Warning,
9090
Level::Note | Level::OnceNote => AnnotationType::Note,
9191
Level::Help | Level::OnceHelp => AnnotationType::Help,

compiler/rustc_errors/src/diagnostic.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::snippet::Style;
22
use crate::{
3-
CodeSuggestion, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Level, MultiSpan,
4-
SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
3+
CodeSuggestion, DelayedBugKind, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Level,
4+
MultiSpan, SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
55
};
66
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
77
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
@@ -243,12 +243,15 @@ impl Diagnostic {
243243

244244
pub fn is_error(&self) -> bool {
245245
match self.level {
246-
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error | Level::FailureNote => {
247-
true
248-
}
246+
Level::Bug
247+
| Level::DelayedBug(DelayedBugKind::Normal)
248+
| Level::Fatal
249+
| Level::Error
250+
| Level::FailureNote => true,
249251

250252
Level::ForceWarning(_)
251253
| Level::Warning
254+
| Level::DelayedBug(DelayedBugKind::GoodPath)
252255
| Level::Note
253256
| Level::OnceNote
254257
| Level::Help
@@ -318,7 +321,7 @@ impl Diagnostic {
318321
"downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
319322
self.level
320323
);
321-
self.level = Level::DelayedBug;
324+
self.level = Level::DelayedBug(DelayedBugKind::Normal);
322325
}
323326

324327
/// Appends a labeled span to the diagnostic.

compiler/rustc_errors/src/lib.rs

+34-22
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
519519
pub static TRACK_DIAGNOSTIC: AtomicRef<fn(Diagnostic, &mut dyn FnMut(Diagnostic))> =
520520
AtomicRef::new(&(default_track_diagnostic as _));
521521

522-
enum DelayedBugKind {
522+
#[derive(Copy, PartialEq, Eq, Clone, Hash, Debug, Encodable, Decodable)]
523+
pub enum DelayedBugKind {
523524
Normal,
524525
GoodPath,
525526
}
@@ -865,7 +866,8 @@ impl DiagCtxt {
865866
if treat_next_err_as_bug {
866867
self.bug(msg);
867868
}
868-
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug, msg).emit()
869+
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug(DelayedBugKind::Normal), msg)
870+
.emit()
869871
}
870872

871873
/// Like `delayed_bug`, but takes an additional span.
@@ -882,16 +884,15 @@ impl DiagCtxt {
882884
if treat_next_err_as_bug {
883885
self.span_bug(sp, msg);
884886
}
885-
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug, msg).with_span(sp).emit()
887+
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug(DelayedBugKind::Normal), msg)
888+
.with_span(sp)
889+
.emit()
886890
}
887891

888892
// FIXME(eddyb) note the comment inside `impl Drop for DiagCtxtInner`, that's
889893
// where the explanation of what "good path" is (also, it should be renamed).
890894
pub fn good_path_delayed_bug(&self, msg: impl Into<DiagnosticMessage>) {
891-
let mut inner = self.inner.borrow_mut();
892-
let diagnostic = Diagnostic::new(DelayedBug, msg);
893-
let backtrace = std::backtrace::Backtrace::capture();
894-
inner.good_path_delayed_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
895+
DiagnosticBuilder::<()>::new(self, DelayedBug(DelayedBugKind::GoodPath), msg).emit()
895896
}
896897

897898
#[track_caller]
@@ -1268,17 +1269,27 @@ impl DiagCtxtInner {
12681269
return None;
12691270
}
12701271

1271-
if diagnostic.level == DelayedBug {
1272-
// FIXME(eddyb) this should check for `has_errors` and stop pushing
1273-
// once *any* errors were emitted (and truncate `span_delayed_bugs`
1274-
// when an error is first emitted, also), but maybe there's a case
1275-
// in which that's not sound? otherwise this is really inefficient.
1276-
let backtrace = std::backtrace::Backtrace::capture();
1277-
self.span_delayed_bugs
1278-
.push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace));
1272+
// FIXME(eddyb) this should check for `has_errors` and stop pushing
1273+
// once *any* errors were emitted (and truncate `span_delayed_bugs`
1274+
// when an error is first emitted, also), but maybe there's a case
1275+
// in which that's not sound? otherwise this is really inefficient.
1276+
match diagnostic.level {
1277+
DelayedBug(DelayedBugKind::Normal) => {
1278+
let backtrace = std::backtrace::Backtrace::capture();
1279+
self.span_delayed_bugs
1280+
.push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace));
12791281

1280-
#[allow(deprecated)]
1281-
return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
1282+
#[allow(deprecated)]
1283+
return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
1284+
}
1285+
DelayedBug(DelayedBugKind::GoodPath) => {
1286+
let backtrace = std::backtrace::Backtrace::capture();
1287+
self.good_path_delayed_bugs
1288+
.push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace));
1289+
1290+
return None;
1291+
}
1292+
_ => {}
12821293
}
12831294

12841295
if diagnostic.has_future_breakage() {
@@ -1438,7 +1449,7 @@ impl DiagCtxtInner {
14381449
if backtrace || self.ice_file.is_none() { bug.decorate() } else { bug.inner };
14391450

14401451
// "Undelay" the `DelayedBug`s (into plain `Bug`s).
1441-
if bug.level != DelayedBug {
1452+
if !matches!(bug.level, DelayedBug(_)) {
14421453
// NOTE(eddyb) not panicking here because we're already producing
14431454
// an ICE, and the more information the merrier.
14441455
bug.subdiagnostic(InvalidFlushedDelayedDiagnosticLevel {
@@ -1526,8 +1537,9 @@ pub enum Level {
15261537
/// silently dropped. I.e. "expect other errors are emitted" semantics. Useful on code paths
15271538
/// that should only be reached when compiling erroneous code.
15281539
///
1529-
/// Its `EmissionGuarantee` is `ErrorGuaranteed`.
1530-
DelayedBug,
1540+
/// Its `EmissionGuarantee` is `ErrorGuaranteed` for `Normal` delayed bugs, and `()` for
1541+
/// `GoodPath` delayed bugs.
1542+
DelayedBug(DelayedBugKind),
15311543

15321544
/// An error that causes an immediate abort. Used for things like configuration errors,
15331545
/// internal overflows, some file operation errors.
@@ -1602,7 +1614,7 @@ impl Level {
16021614
fn color(self) -> ColorSpec {
16031615
let mut spec = ColorSpec::new();
16041616
match self {
1605-
Bug | DelayedBug | Fatal | Error => {
1617+
Bug | DelayedBug(_) | Fatal | Error => {
16061618
spec.set_fg(Some(Color::Red)).set_intense(true);
16071619
}
16081620
ForceWarning(_) | Warning => {
@@ -1622,7 +1634,7 @@ impl Level {
16221634

16231635
pub fn to_str(self) -> &'static str {
16241636
match self {
1625-
Bug | DelayedBug => "error: internal compiler error",
1637+
Bug | DelayedBug(_) => "error: internal compiler error",
16261638
Fatal | Error => "error",
16271639
ForceWarning(_) | Warning => "warning",
16281640
Note | OnceNote => "note",

0 commit comments

Comments
 (0)