Skip to content

Commit ea9c8d9

Browse files
authored
Rollup merge of #137102 - compiler-errors:name_regions2, r=oli-obk
Rework `name_regions` to not rely on reverse scc graph for non-member-constrain usages Fixes #137015 Splits the `name_regions` into two versions: One meant for member region constraint error reporting (which I've renamed to `name_regions_for_member_constraint`), and one meant *just* to replace region vids with an external region. Use the latter in the usage sites I added in #136559, since the regions returned by `name_regions_for_member_constraint` are also not *totally* accurate (which is fine for how they're used for member region constraint error reporting -- they're intentionally returning overapproximated universal regions so that we have something to name in `+ use<'a>` suggestions, because opaques can only capture universal regions and since member region constraints don't insert any edges into the region graph, the error region is probably gonna be shorter than a universal region) and because that function requires the reverse scc graph to have been computed which isn't done for our usages in #136559.
2 parents fc094a1 + 17071ff commit ea9c8d9

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ use rustc_infer::infer::{NllRegionVariableOrigin, RelateParamBound};
1414
use rustc_middle::bug;
1515
use rustc_middle::hir::place::PlaceBase;
1616
use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
17-
use rustc_middle::ty::{self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeVisitor};
17+
use rustc_middle::ty::fold::fold_regions;
18+
use rustc_middle::ty::{
19+
self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor,
20+
};
1821
use rustc_span::{Ident, Span, kw};
1922
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2023
use rustc_trait_selection::error_reporting::infer::nice_region_error::{
@@ -183,6 +186,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
183186
}
184187
}
185188

189+
/// Map the regions in the type to named regions, where possible.
190+
fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
191+
where
192+
T: TypeFoldable<TyCtxt<'tcx>>,
193+
{
194+
fold_regions(tcx, ty, |region, _| match *region {
195+
ty::ReVar(vid) => self.to_error_region(vid).unwrap_or(region),
196+
_ => region,
197+
})
198+
}
199+
186200
/// Returns `true` if a closure is inferred to be an `FnMut` closure.
187201
fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
188202
if let Some(ty::ReLateParam(late_param)) = self.to_error_region(fr).as_deref()
@@ -314,7 +328,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
314328
let type_test_span = type_test.span;
315329

316330
if let Some(lower_bound_region) = lower_bound_region {
317-
let generic_ty = self.regioncx.name_regions(
331+
let generic_ty = self.name_regions(
318332
self.infcx.tcx,
319333
type_test.generic_kind.to_ty(self.infcx.tcx),
320334
);
@@ -323,7 +337,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
323337
self.body.source.def_id().expect_local(),
324338
type_test_span,
325339
Some(origin),
326-
self.regioncx.name_regions(self.infcx.tcx, type_test.generic_kind),
340+
self.name_regions(self.infcx.tcx, type_test.generic_kind),
327341
lower_bound_region,
328342
));
329343
} else {
@@ -354,9 +368,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
354368
}
355369

356370
RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
357-
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
358-
let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
359-
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
371+
let named_ty =
372+
self.regioncx.name_regions_for_member_constraint(self.infcx.tcx, hidden_ty);
373+
let named_key =
374+
self.regioncx.name_regions_for_member_constraint(self.infcx.tcx, key);
375+
let named_region = self
376+
.regioncx
377+
.name_regions_for_member_constraint(self.infcx.tcx, member_region);
360378
let diag = unexpected_hidden_region_diagnostic(
361379
self.infcx,
362380
self.mir_def_id(),

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
204204
/// that the regions produced are in fact equal to the named region they are
205205
/// replaced with. This is fine because this function is only to improve the
206206
/// region names in error messages.
207-
pub(crate) fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
207+
///
208+
/// This differs from `MirBorrowckCtxt::name_regions` since it is particularly
209+
/// lax with mapping region vids that are *shorter* than a universal region to
210+
/// that universal region. This is useful for member region constraints since
211+
/// we want to suggest a universal region name to capture even if it's technically
212+
/// not equal to the error region.
213+
pub(crate) fn name_regions_for_member_constraint<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
208214
where
209215
T: TypeFoldable<TyCtxt<'tcx>>,
210216
{

Diff for: tests/ui/borrowck/alias-liveness/name-region.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Make sure we don't ICE when trying to name the regions that appear in the alias
2+
// of the type test error.
3+
4+
trait AnotherTrait {
5+
type Ty2<'a>;
6+
}
7+
8+
fn test_alias<T: AnotherTrait>(_: &'static T::Ty2<'_>) {
9+
let _: &'static T::Ty2<'_>;
10+
//~^ ERROR the associated type `<T as AnotherTrait>::Ty2<'_>` may not live long enough
11+
}
12+
13+
fn main() {}

Diff for: tests/ui/borrowck/alias-liveness/name-region.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0310]: the associated type `<T as AnotherTrait>::Ty2<'_>` may not live long enough
2+
--> $DIR/name-region.rs:9:12
3+
|
4+
LL | let _: &'static T::Ty2<'_>;
5+
| ^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the associated type `<T as AnotherTrait>::Ty2<'_>` must be valid for the static lifetime...
8+
| ...so that the type `<T as AnotherTrait>::Ty2<'_>` will meet its required lifetime bounds
9+
|
10+
= help: consider adding an explicit lifetime bound `<T as AnotherTrait>::Ty2<'_>: 'static`...
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0310`.

0 commit comments

Comments
 (0)