From 62b4e551124ffc38196740efd6ff3b6979a504eb Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 19 Feb 2024 08:42:04 +1100 Subject: [PATCH] Avoid a `span_delayed_bug` in `compute_regions`. By storing error guarantees in `RegionErrors`. --- .../src/diagnostics/region_errors.rs | 18 +++++++++++------- compiler/rustc_borrowck/src/nll.rs | 7 ++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index e586c58781c..cd70ec580a3 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -1,7 +1,7 @@ //! Error reporting machinery for lifetime errors. use rustc_data_structures::fx::FxIndexSet; -use rustc_errors::{Applicability, DiagnosticBuilder, MultiSpan}; +use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; use rustc_hir as hir; use rustc_hir::def::Res::Def; use rustc_hir::def_id::DefId; @@ -73,7 +73,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> { /// /// Usually we expect this to either be empty or contain a small number of items, so we can avoid /// allocation most of the time. -pub(crate) struct RegionErrors<'tcx>(Vec>, TyCtxt<'tcx>); +pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>); impl<'tcx> RegionErrors<'tcx> { pub fn new(tcx: TyCtxt<'tcx>) -> Self { @@ -82,15 +82,18 @@ impl<'tcx> RegionErrors<'tcx> { #[track_caller] pub fn push(&mut self, val: impl Into>) { let val = val.into(); - self.1.sess.dcx().delayed_bug(format!("{val:?}")); - self.0.push(val); + let guar = self.1.sess.dcx().delayed_bug(format!("{val:?}")); + self.0.push((val, guar)); } pub fn is_empty(&self) -> bool { self.0.is_empty() } - pub fn into_iter(self) -> impl Iterator> { + pub fn into_iter(self) -> impl Iterator, ErrorGuaranteed)> { self.0.into_iter() } + pub fn has_errors(&self) -> Option { + self.0.get(0).map(|x| x.1) + } } impl std::fmt::Debug for RegionErrors<'_> { @@ -304,10 +307,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let mut last_unexpected_hidden_region: Option<(Span, Ty<'_>, ty::OpaqueTypeKey<'tcx>)> = None; - for nll_error in nll_errors.into_iter() { + for (nll_error, _) in nll_errors.into_iter() { match nll_error { RegionErrorKind::TypeTestError { type_test } => { - // Try to convert the lower-bound region into something named we can print for the user. + // Try to convert the lower-bound region into something named we can print for + // the user. let lower_bound_region = self.to_error_region(type_test.lower_bound); let type_test_span = type_test.span; diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index 80d79e1beb7..4aa32a61f7c 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -184,12 +184,9 @@ pub(crate) fn compute_regions<'cx, 'tcx>( let (closure_region_requirements, nll_errors) = regioncx.solve(infcx, body, polonius_output.clone()); - if !nll_errors.is_empty() { + if let Some(guar) = nll_errors.has_errors() { // Suppress unhelpful extra errors in `infer_opaque_types`. - infcx.set_tainted_by_errors(infcx.dcx().span_delayed_bug( - body.span, - "`compute_regions` tainted `infcx` with errors but did not emit any errors", - )); + infcx.set_tainted_by_errors(guar); } let remapped_opaque_tys = regioncx.infer_opaque_types(infcx, opaque_type_values);