Skip to content

Commit 2acd739

Browse files
committed
Add a safe fallback suggested in code review
1 parent be8c8ea commit 2acd739

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,30 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
323323
let mut last_unexpected_hidden_region: Option<(Span, Ty<'_>, ty::OpaqueTypeKey<'tcx>)> =
324324
None;
325325

326+
let emit_generic_does_not_live_long_enough =
327+
|cx: &mut Self, generic_kind: GenericKind<'_>, span: Span, lower_bound: RegionVid| {
328+
// FIXME. We should handle this case better. It
329+
// indicates that we have e.g., some region variable
330+
// whose value is like `'a+'b` where `'a` and `'b` are
331+
// distinct unrelated universal regions that are not
332+
// known to outlive one another. It'd be nice to have
333+
// some examples where this arises to decide how best
334+
// to report it; we could probably handle it by
335+
// iterating over the universal regions and reporting
336+
// an error that multiple bounds are required.
337+
let mut diag = cx.dcx().create_err(GenericDoesNotLiveLongEnough {
338+
kind: generic_kind.to_string(),
339+
span,
340+
});
341+
342+
// Add notes and suggestions for the case of 'static lifetime
343+
// implied but not specified when a generic associated types
344+
// are from higher-ranked trait bounds
345+
cx.suggest_static_lifetime_for_gat_from_hrtb(&mut diag, lower_bound);
346+
347+
cx.buffer_error(diag);
348+
};
349+
326350
for (nll_error, _) in nll_errors.into_iter() {
327351
match nll_error {
328352
// A type-test failed and the constraint was rewritten due
@@ -332,28 +356,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
332356
lower_bound,
333357
span,
334358
failed_due_to_placeholders: true,
335-
} => {
336-
// FIXME. We should handle this case better. It
337-
// indicates that we have e.g., some region variable
338-
// whose value is like `'a+'b` where `'a` and `'b` are
339-
// distinct unrelated universal regions that are not
340-
// known to outlive one another. It'd be nice to have
341-
// some examples where this arises to decide how best
342-
// to report it; we could probably handle it by
343-
// iterating over the universal regions and reporting
344-
// an error that multiple bounds are required.
345-
let mut diag = self.dcx().create_err(GenericDoesNotLiveLongEnough {
346-
kind: generic_kind.to_string(),
347-
span,
348-
});
359+
} => emit_generic_does_not_live_long_enough(self, generic_kind, span, lower_bound),
349360

350-
// Add notes and suggestions for the case of 'static lifetime
351-
// implied but not specified when a generic associated types
352-
// are from higher-ranked trait bounds
353-
self.suggest_static_lifetime_for_gat_from_hrtb(&mut diag, lower_bound);
354-
355-
self.buffer_error(diag);
356-
}
357361
RegionErrorKind::TypeTestError {
358362
lower_bound,
359363
span,
@@ -363,9 +367,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
363367
// Try to convert the lower-bound region into something named we can print for
364368
// the user.
365369
let Some(lower_bound_region) = self.to_error_region(lower_bound) else {
366-
unreachable!(
367-
"Found nothing good; lower bound was: {lower_bound:?} for {generic_kind:?} at {span:?}"
370+
// FIXME: this can possibly never happen along this error branch. It's possible
371+
// (and the case in the UI tests) that any lower_bound that's an internal region
372+
// and not something we want in error output and fails the type-test
373+
// evaluation would have been triggered by a higher-ranked misfire that would have
374+
// set `failed_due_to_placeholders` to `true`. The previous code structure
375+
// before elimination of placeholders in borrowck suggests this, since this
376+
// failure was used to trigger related diagnostics. If you think you can figure this out,
377+
// either add a counterexample where this branch is taken and remove this comment,
378+
// or replace this with an `unreachable!()` or similar and describe why.
379+
emit_generic_does_not_live_long_enough(
380+
self,
381+
generic_kind,
382+
span,
383+
lower_bound,
368384
);
385+
continue;
369386
};
370387
debug!(?lower_bound_region);
371388
let generic_ty =

0 commit comments

Comments
 (0)