Skip to content

Commit

Permalink
Make the match in emit_diagnostic complete.
Browse files Browse the repository at this point in the history
This match is complex enough that it's a good idea to enumerate every
variant.

This also means `can_be_top_or_sub` can just be `can_be_subdiag`.
  • Loading branch information
nnethercote committed Mar 1, 2024
1 parent a7d9262 commit 7ef605b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ impl HumanEmitter {
}
if !self.short_message {
for child in children {
assert!(child.level.can_be_top_or_sub().1);
assert!(child.level.can_be_subdiag());
let span = &child.span;
if let Err(err) = self.emit_messages_default_inner(
span,
Expand Down
41 changes: 23 additions & 18 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,6 @@ impl DiagCtxtInner {

// Return value is only `Some` if the level is `Error` or `DelayedBug`.
fn emit_diagnostic(&mut self, mut diagnostic: DiagInner) -> Option<ErrorGuaranteed> {
assert!(diagnostic.level.can_be_top_or_sub().0);

if diagnostic.has_future_breakage() {
// Future breakages aren't emitted if they're Level::Allow,
// but they still need to be constructed and stashed below,
Expand All @@ -1368,9 +1366,12 @@ impl DiagCtxtInner {
// return early *and* have some kind of side-effect, except where
// noted.
match diagnostic.level {
Fatal | Error if self.treat_next_err_as_bug() => {
// `Fatal` and `Error` can be promoted to `Bug`.
diagnostic.level = Bug;
Bug => {}
Fatal | Error => {
if self.treat_next_err_as_bug() {
// `Fatal` and `Error` can be promoted to `Bug`.
diagnostic.level = Bug;
}
}
DelayedBug => {
// Note that because we check these conditions first,
Expand Down Expand Up @@ -1405,14 +1406,21 @@ impl DiagCtxtInner {
};
}
}
Warning if !self.flags.can_emit_warnings => {
if diagnostic.has_future_breakage() {
// The side-effect is at the top of this method.
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
ForceWarning(None) => {} // `ForceWarning(Some(...))` is below, with `Expect`
Warning => {
if !self.flags.can_emit_warnings {
// We are not emitting warnings.
if diagnostic.has_future_breakage() {
// The side-effect is at the top of this method.
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
}
return None;
}
return None;
}
Note | Help | FailureNote => {}
OnceNote | OnceHelp => panic!("bad level: {:?}", diagnostic.level),
Allow => {
// Nothing emitted for allowed lints.
if diagnostic.has_future_breakage() {
// The side-effect is at the top of this method.
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
Expand All @@ -1434,12 +1442,12 @@ impl DiagCtxtInner {
}
self.fulfilled_expectations.insert(expect_id.normalize());
if let Expect(_) = diagnostic.level {
// Nothing emitted here for expected lints.
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
self.suppressed_expected_diag = true;
return None;
}
}
_ => {}
}

TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| {
Expand Down Expand Up @@ -1816,16 +1824,13 @@ impl Level {
matches!(*self, FailureNote)
}

// Can this level be used in a top-level diagnostic message and/or a
// subdiagnostic message?
fn can_be_top_or_sub(&self) -> (bool, bool) {
// Can this level be used in a subdiagnostic message?
fn can_be_subdiag(&self) -> bool {
match self {
Bug | DelayedBug | Fatal | Error | ForceWarning(_) | FailureNote | Allow
| Expect(_) => (true, false),

Warning | Note | Help => (true, true),
| Expect(_) => false,

OnceNote | OnceHelp => (false, true),
Warning | Note | Help | OnceNote | OnceHelp => true,
}
}
}
Expand Down

0 comments on commit 7ef605b

Please sign in to comment.