Skip to content

Commit c367386

Browse files
committed
Refactor emit_diagnostic.
- Combine two different blocks involving `diagnostic.level.get_expectation_id()` into one. - Combine several `if`s involving `diagnostic.level` into a single `match`. This requires reordering some of the operations, but this has no functional effect.
1 parent 5dd0431 commit c367386

File tree

1 file changed

+39
-43
lines changed
  • compiler/rustc_errors/src

1 file changed

+39
-43
lines changed

compiler/rustc_errors/src/lib.rs

+39-43
Original file line numberDiff line numberDiff line change
@@ -1247,24 +1247,41 @@ impl DiagCtxtInner {
12471247
}
12481248

12491249
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
1250-
// The `LintExpectationId` can be stable or unstable depending on when it was created.
1251-
// Diagnostics created before the definition of `HirId`s are unstable and can not yet
1252-
// be stored. Instead, they are buffered until the `LintExpectationId` is replaced by
1253-
// a stable one by the `LintLevelsBuilder`.
1254-
if let Some(LintExpectationId::Unstable { .. }) = diagnostic.level.get_expectation_id() {
1255-
self.unstable_expect_diagnostics.push(diagnostic);
1256-
return None;
1250+
if let Some(expectation_id) = diagnostic.level.get_expectation_id() {
1251+
// The `LintExpectationId` can be stable or unstable depending on when it was created.
1252+
// Diagnostics created before the definition of `HirId`s are unstable and can not yet
1253+
// be stored. Instead, they are buffered until the `LintExpectationId` is replaced by
1254+
// a stable one by the `LintLevelsBuilder`.
1255+
if let LintExpectationId::Unstable { .. } = expectation_id {
1256+
self.unstable_expect_diagnostics.push(diagnostic);
1257+
return None;
1258+
}
1259+
self.suppressed_expected_diag = true;
1260+
self.fulfilled_expectations.insert(expectation_id.normalize());
1261+
}
1262+
1263+
if diagnostic.has_future_breakage() {
1264+
// Future breakages aren't emitted if they're Level::Allow,
1265+
// but they still need to be constructed and stashed below,
1266+
// so they'll trigger the good-path bug check.
1267+
self.suppressed_expected_diag = true;
1268+
self.future_breakage_diagnostics.push(diagnostic.clone());
1269+
}
1270+
1271+
if matches!(diagnostic.level, DelayedBug(_)) && self.flags.eagerly_emit_delayed_bugs {
1272+
diagnostic.level = Error;
12571273
}
12581274

1259-
// FIXME(eddyb) this should check for `has_errors` and stop pushing
1260-
// once *any* errors were emitted (and truncate `span_delayed_bugs`
1261-
// when an error is first emitted, also), but maybe there's a case
1262-
// in which that's not sound? otherwise this is really inefficient.
12631275
match diagnostic.level {
1264-
DelayedBug(_) if self.flags.eagerly_emit_delayed_bugs => {
1265-
diagnostic.level = Error;
1276+
// This must come after the possible promotion of `DelayedBug` to `Error` above.
1277+
Fatal | Error if self.treat_next_err_as_bug() => {
1278+
diagnostic.level = Bug;
12661279
}
12671280
DelayedBug(DelayedBugKind::Normal) => {
1281+
// FIXME(eddyb) this should check for `has_errors` and stop pushing
1282+
// once *any* errors were emitted (and truncate `span_delayed_bugs`
1283+
// when an error is first emitted, also), but maybe there's a case
1284+
// in which that's not sound? otherwise this is really inefficient.
12681285
let backtrace = std::backtrace::Backtrace::capture();
12691286
self.span_delayed_bugs
12701287
.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
@@ -1277,38 +1294,17 @@ impl DiagCtxtInner {
12771294
.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
12781295
return None;
12791296
}
1280-
_ => {}
1281-
}
1282-
1283-
// This must come after the possible promotion of `DelayedBug` to
1284-
// `Error` above.
1285-
if matches!(diagnostic.level, Error | Fatal) && self.treat_next_err_as_bug() {
1286-
diagnostic.level = Bug;
1287-
}
1288-
1289-
if diagnostic.has_future_breakage() {
1290-
// Future breakages aren't emitted if they're Level::Allow,
1291-
// but they still need to be constructed and stashed below,
1292-
// so they'll trigger the good-path bug check.
1293-
self.suppressed_expected_diag = true;
1294-
self.future_breakage_diagnostics.push(diagnostic.clone());
1295-
}
1296-
1297-
if let Some(expectation_id) = diagnostic.level.get_expectation_id() {
1298-
self.suppressed_expected_diag = true;
1299-
self.fulfilled_expectations.insert(expectation_id.normalize());
1300-
}
1301-
1302-
if diagnostic.level == Warning && !self.flags.can_emit_warnings {
1303-
if diagnostic.has_future_breakage() {
1297+
Warning if !self.flags.can_emit_warnings => {
1298+
if diagnostic.has_future_breakage() {
1299+
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1300+
}
1301+
return None;
1302+
}
1303+
Allow | Expect(_) => {
13041304
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1305+
return None;
13051306
}
1306-
return None;
1307-
}
1308-
1309-
if matches!(diagnostic.level, Expect(_) | Allow) {
1310-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1311-
return None;
1307+
_ => {}
13121308
}
13131309

13141310
let mut guaranteed = None;

0 commit comments

Comments
 (0)