Skip to content

Commit

Permalink
Avoid a span_delayed_bug in compute_regions.
Browse files Browse the repository at this point in the history
By storing error guarantees in `RegionErrors`.
  • Loading branch information
nnethercote committed Feb 20, 2024
1 parent 1f80ee5 commit 9e154ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
18 changes: 11 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<RegionErrorKind<'tcx>>, TyCtxt<'tcx>);
pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>);

impl<'tcx> RegionErrors<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
Expand All @@ -82,15 +82,18 @@ impl<'tcx> RegionErrors<'tcx> {
#[track_caller]
pub fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
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<Item = RegionErrorKind<'tcx>> {
pub fn into_iter(self) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
self.0.into_iter()
}
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
self.0.get(0).map(|x| x.1)
}
}

impl std::fmt::Debug for RegionErrors<'_> {
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9e154ea

Please sign in to comment.