From 5ff06fb77f906b65b6a5301e84acc82a246af2b0 Mon Sep 17 00:00:00 2001
From: Hans Kratz <hans@appfour.com>
Date: Wed, 4 Aug 2021 14:25:45 +0000
Subject: [PATCH] Fix overflow in rustc happening if the `err_count()` is
 reduced in a stage.

This can happen if stashed diagnostics are removed or replaced with fewer errors. The semantics stay the same if built without overflow. Fixes #84219.
---
 compiler/rustc_session/src/session.rs | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs
index 9ab6dbb1ea906..ea2067aaa83f1 100644
--- a/compiler/rustc_session/src/session.rs
+++ b/compiler/rustc_session/src/session.rs
@@ -450,8 +450,7 @@ impl Session {
     {
         let old_count = self.err_count();
         let result = f();
-        let errors = self.err_count() - old_count;
-        if errors == 0 { Ok(result) } else { Err(ErrorReported) }
+        if self.err_count() == old_count { Ok(result) } else { Err(ErrorReported) }
     }
     pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
         self.diagnostic().span_warn(sp, msg)