Skip to content

Commit 62b4e55

Browse files
committed
Avoid a span_delayed_bug in compute_regions.
By storing error guarantees in `RegionErrors`.
1 parent 9f82563 commit 62b4e55

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Error reporting machinery for lifetime errors.
22
33
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_errors::{Applicability, DiagnosticBuilder, MultiSpan};
4+
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
55
use rustc_hir as hir;
66
use rustc_hir::def::Res::Def;
77
use rustc_hir::def_id::DefId;
@@ -73,7 +73,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
7373
///
7474
/// Usually we expect this to either be empty or contain a small number of items, so we can avoid
7575
/// allocation most of the time.
76-
pub(crate) struct RegionErrors<'tcx>(Vec<RegionErrorKind<'tcx>>, TyCtxt<'tcx>);
76+
pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>);
7777

7878
impl<'tcx> RegionErrors<'tcx> {
7979
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
@@ -82,15 +82,18 @@ impl<'tcx> RegionErrors<'tcx> {
8282
#[track_caller]
8383
pub fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
8484
let val = val.into();
85-
self.1.sess.dcx().delayed_bug(format!("{val:?}"));
86-
self.0.push(val);
85+
let guar = self.1.sess.dcx().delayed_bug(format!("{val:?}"));
86+
self.0.push((val, guar));
8787
}
8888
pub fn is_empty(&self) -> bool {
8989
self.0.is_empty()
9090
}
91-
pub fn into_iter(self) -> impl Iterator<Item = RegionErrorKind<'tcx>> {
91+
pub fn into_iter(self) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
9292
self.0.into_iter()
9393
}
94+
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
95+
self.0.get(0).map(|x| x.1)
96+
}
9497
}
9598

9699
impl std::fmt::Debug for RegionErrors<'_> {
@@ -304,10 +307,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
304307
let mut last_unexpected_hidden_region: Option<(Span, Ty<'_>, ty::OpaqueTypeKey<'tcx>)> =
305308
None;
306309

307-
for nll_error in nll_errors.into_iter() {
310+
for (nll_error, _) in nll_errors.into_iter() {
308311
match nll_error {
309312
RegionErrorKind::TypeTestError { type_test } => {
310-
// Try to convert the lower-bound region into something named we can print for the user.
313+
// Try to convert the lower-bound region into something named we can print for
314+
// the user.
311315
let lower_bound_region = self.to_error_region(type_test.lower_bound);
312316

313317
let type_test_span = type_test.span;

compiler/rustc_borrowck/src/nll.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,9 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
184184
let (closure_region_requirements, nll_errors) =
185185
regioncx.solve(infcx, body, polonius_output.clone());
186186

187-
if !nll_errors.is_empty() {
187+
if let Some(guar) = nll_errors.has_errors() {
188188
// Suppress unhelpful extra errors in `infer_opaque_types`.
189-
infcx.set_tainted_by_errors(infcx.dcx().span_delayed_bug(
190-
body.span,
191-
"`compute_regions` tainted `infcx` with errors but did not emit any errors",
192-
));
189+
infcx.set_tainted_by_errors(guar);
193190
}
194191

195192
let remapped_opaque_tys = regioncx.infer_opaque_types(infcx, opaque_type_values);

0 commit comments

Comments
 (0)