Skip to content

Commit b38c36b

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 6a48407 commit b38c36b

File tree

1 file changed

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

1 file changed

+39
-44
lines changed

compiler/rustc_errors/src/lib.rs

+39-44
Original file line numberDiff line numberDiff line change
@@ -1248,24 +1248,36 @@ impl DiagCtxtInner {
12481248
}
12491249

12501250
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
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 Some(LintExpectationId::Unstable { .. }) = diagnostic.level.get_expectation_id() {
1256-
self.unstable_expect_diagnostics.push(diagnostic.clone());
1257-
return None;
1251+
if let Some(expectation_id) = diagnostic.level.get_expectation_id() {
1252+
// The `LintExpectationId` can be stable or unstable depending on when it was created.
1253+
// Diagnostics created before the definition of `HirId`s are unstable and can not yet
1254+
// be stored. Instead, they are buffered until the `LintExpectationId` is replaced by
1255+
// a stable one by the `LintLevelsBuilder`.
1256+
if let LintExpectationId::Unstable { .. } = expectation_id {
1257+
self.unstable_expect_diagnostics.push(diagnostic.clone());
1258+
return None;
1259+
}
1260+
self.fulfilled_expectations.insert(expectation_id.normalize());
1261+
}
1262+
1263+
if diagnostic.has_future_breakage() {
1264+
self.future_breakage_diagnostics.push(diagnostic.clone());
1265+
}
1266+
1267+
if matches!(diagnostic.level, DelayedBug(_)) && self.flags.eagerly_emit_delayed_bugs {
1268+
diagnostic.level = Error;
12581269
}
12591270

1260-
// FIXME(eddyb) this should check for `has_errors` and stop pushing
1261-
// once *any* errors were emitted (and truncate `span_delayed_bugs`
1262-
// when an error is first emitted, also), but maybe there's a case
1263-
// in which that's not sound? otherwise this is really inefficient.
12641271
match diagnostic.level {
1265-
DelayedBug(_) if self.flags.eagerly_emit_delayed_bugs => {
1266-
diagnostic.level = Error;
1272+
// This must come after the possible promotion of `DelayedBug` to `Error` above.
1273+
Fatal | Error if self.treat_next_err_as_bug() => {
1274+
diagnostic.level = Bug;
12671275
}
12681276
DelayedBug(DelayedBugKind::Normal) => {
1277+
// FIXME(eddyb) this should check for `has_errors` and stop pushing
1278+
// once *any* errors were emitted (and truncate `span_delayed_bugs`
1279+
// when an error is first emitted, also), but maybe there's a case
1280+
// in which that's not sound? otherwise this is really inefficient.
12691281
let backtrace = std::backtrace::Backtrace::capture();
12701282
self.span_delayed_bugs
12711283
.push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace));
@@ -1280,39 +1292,22 @@ impl DiagCtxtInner {
12801292

12811293
return None;
12821294
}
1283-
_ => {}
1284-
}
1285-
1286-
// This must come after the possible promotion of `DelayedBug` to
1287-
// `Error` above.
1288-
if matches!(diagnostic.level, Error | Fatal) && self.treat_next_err_as_bug() {
1289-
diagnostic.level = Bug;
1290-
}
1291-
1292-
if diagnostic.has_future_breakage() {
1293-
self.future_breakage_diagnostics.push(diagnostic.clone());
1294-
}
1295-
1296-
if let Some(expectation_id) = diagnostic.level.get_expectation_id() {
1297-
self.fulfilled_expectations.insert(expectation_id.normalize());
1298-
}
1299-
1300-
if diagnostic.level == Warning && !self.flags.can_emit_warnings {
1301-
if diagnostic.has_future_breakage() {
1295+
Warning if !self.flags.can_emit_warnings => {
1296+
if diagnostic.has_future_breakage() {
1297+
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1298+
}
1299+
return None;
1300+
}
1301+
Allow => {
13021302
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1303+
return None;
13031304
}
1304-
return None;
1305-
}
1306-
1307-
if matches!(diagnostic.level, Expect(_)) {
1308-
self.suppressed_expected_diag = true;
1309-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1310-
return None;
1311-
}
1312-
1313-
if matches!(diagnostic.level, Allow) {
1314-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1315-
return None;
1305+
Expect(_) => {
1306+
self.suppressed_expected_diag = true;
1307+
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1308+
return None;
1309+
}
1310+
_ => {}
13161311
}
13171312

13181313
let mut guaranteed = None;

0 commit comments

Comments
 (0)