Skip to content

Commit caefec9

Browse files
committed
Do not abort compilation when failing to normalize opaque types.
1 parent 7919ef0 commit caefec9

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ pub trait InferCtxtExt<'tcx> {
101101
}
102102

103103
pub trait TypeErrCtxtExt<'tcx> {
104+
fn build_overflow_error<T>(
105+
&self,
106+
predicate: &T,
107+
span: Span,
108+
suggest_increasing_limit: bool,
109+
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>
110+
where
111+
T: fmt::Display
112+
+ TypeFoldable<'tcx>
113+
+ Print<'tcx, FmtPrinter<'tcx, 'tcx>, Output = FmtPrinter<'tcx, 'tcx>>,
114+
<T as Print<'tcx, FmtPrinter<'tcx, 'tcx>>>::Error: std::fmt::Debug;
115+
104116
fn report_overflow_error<T>(
105117
&self,
106118
predicate: &T,
@@ -478,6 +490,26 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
478490
suggest_increasing_limit: bool,
479491
mutate: impl FnOnce(&mut Diagnostic),
480492
) -> !
493+
where
494+
T: fmt::Display
495+
+ TypeFoldable<'tcx>
496+
+ Print<'tcx, FmtPrinter<'tcx, 'tcx>, Output = FmtPrinter<'tcx, 'tcx>>,
497+
<T as Print<'tcx, FmtPrinter<'tcx, 'tcx>>>::Error: std::fmt::Debug,
498+
{
499+
let mut err = self.build_overflow_error(predicate, span, suggest_increasing_limit);
500+
mutate(&mut err);
501+
err.emit();
502+
503+
self.tcx.sess.abort_if_errors();
504+
bug!();
505+
}
506+
507+
fn build_overflow_error<T>(
508+
&self,
509+
predicate: &T,
510+
span: Span,
511+
suggest_increasing_limit: bool,
512+
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>
481513
where
482514
T: fmt::Display
483515
+ TypeFoldable<'tcx>
@@ -511,11 +543,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
511543
self.suggest_new_overflow_limit(&mut err);
512544
}
513545

514-
mutate(&mut err);
515-
516-
err.emit();
517-
self.tcx.sess.abort_if_errors();
518-
bug!();
546+
err
519547
}
520548

521549
/// Reports that an overflow has occurred and halts compilation. We

compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,16 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
216216
let substs = substs.try_fold_with(self)?;
217217
let recursion_limit = self.tcx().recursion_limit();
218218
if !recursion_limit.value_within_limit(self.anon_depth) {
219-
self.infcx.err_ctxt().report_overflow_error(
220-
&ty,
221-
self.cause.span,
222-
true,
223-
|_| {},
224-
);
219+
// A closure or generator may have itself as in its upvars. This
220+
// should be checked handled by the recursion check for opaque types,
221+
// but we may end up here before that check can happen. In that case,
222+
// we delay a bug to mark the trip, and continue without revealing the
223+
// opaque.
224+
self.infcx
225+
.err_ctxt()
226+
.build_overflow_error(&ty, self.cause.span, true)
227+
.delay_as_bug();
228+
return ty.try_super_fold_with(self);
225229
}
226230

227231
let generic_ty = self.tcx().bound_type_of(def_id);

0 commit comments

Comments
 (0)