Skip to content

Commit f4e292a

Browse files
committed
introduce fallible universal region to region inference var conversion
and use it to clean up variance extraction
1 parent 97ef35f commit f4e292a

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

compiler/rustc_borrowck/src/polonius/liveness_constraints.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,9 @@ struct VarianceExtractor<'a, 'tcx> {
231231
impl<'tcx> VarianceExtractor<'_, 'tcx> {
232232
fn record_variance(&mut self, region: ty::Region<'tcx>, variance: ty::Variance) {
233233
// We're only interested in the variance of vars and free regions.
234-
235-
if region.is_bound() || region.is_erased() {
236-
// ignore these
234+
let Some(region) = self.universal_regions.try_to_region_vid(region) else {
237235
return;
238-
}
236+
};
239237

240238
let direction = match variance {
241239
ty::Variance::Covariant => ConstraintDirection::Forward,
@@ -247,7 +245,6 @@ impl<'tcx> VarianceExtractor<'_, 'tcx> {
247245
}
248246
};
249247

250-
let region = self.universal_regions.to_region_vid(region);
251248
self.directions
252249
.entry(region)
253250
.and_modify(|entry| {

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,16 @@ impl<'tcx> UniversalRegions<'tcx> {
337337
self.indices.indices.iter().map(|(&r, &v)| (r, v))
338338
}
339339

340-
/// See `UniversalRegionIndices::to_region_vid`.
340+
/// See [UniversalRegionIndices::to_region_vid].
341341
pub(crate) fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
342342
self.indices.to_region_vid(r)
343343
}
344344

345+
/// See [UniversalRegionIndices::try_to_region_vid].
346+
pub(crate) fn try_to_region_vid(&self, r: ty::Region<'tcx>) -> Option<RegionVid> {
347+
self.indices.try_to_region_vid(r)
348+
}
349+
345350
/// As part of the NLL unit tests, you can annotate a function with
346351
/// `#[rustc_regions]`, and we will emit information about the region
347352
/// inference context and -- in particular -- the external constraints
@@ -886,19 +891,25 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
886891
/// if it is a placeholder. Handling placeholders requires access to the
887892
/// `MirTypeckRegionConstraints`.
888893
fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
894+
self.try_to_region_vid(r)
895+
.unwrap_or_else(|| bug!("cannot convert `{:?}` to a region vid", r))
896+
}
897+
898+
/// Converts `r` into a local inference variable.
899+
/// This is a fallible version of [UniversalRegionIndices::to_region_vid], see that method for
900+
/// more details.
901+
#[inline]
902+
fn try_to_region_vid(&self, r: ty::Region<'tcx>) -> Option<RegionVid> {
889903
if let ty::ReVar(..) = *r {
890-
r.as_var()
904+
Some(r.as_var())
891905
} else if let ty::ReError(guar) = *r {
892906
self.tainted_by_errors.set(Some(guar));
893907
// We use the `'static` `RegionVid` because `ReError` doesn't actually exist in the
894908
// `UniversalRegionIndices`. This is fine because 1) it is a fallback only used if
895909
// errors are being emitted and 2) it leaves the happy path unaffected.
896-
self.fr_static
910+
Some(self.fr_static)
897911
} else {
898-
*self
899-
.indices
900-
.get(&r)
901-
.unwrap_or_else(|| bug!("cannot convert `{:?}` to a region vid", r))
912+
self.indices.get(&r).copied()
902913
}
903914
}
904915

0 commit comments

Comments
 (0)