@@ -337,11 +337,16 @@ impl<'tcx> UniversalRegions<'tcx> {
337
337
self . indices . indices . iter ( ) . map ( |( & r, & v) | ( r, v) )
338
338
}
339
339
340
- /// See ` UniversalRegionIndices::to_region_vid` .
340
+ /// See [ UniversalRegionIndices::to_region_vid] .
341
341
pub ( crate ) fn to_region_vid ( & self , r : ty:: Region < ' tcx > ) -> RegionVid {
342
342
self . indices . to_region_vid ( r)
343
343
}
344
344
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
+
345
350
/// As part of the NLL unit tests, you can annotate a function with
346
351
/// `#[rustc_regions]`, and we will emit information about the region
347
352
/// inference context and -- in particular -- the external constraints
@@ -886,19 +891,25 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
886
891
/// if it is a placeholder. Handling placeholders requires access to the
887
892
/// `MirTypeckRegionConstraints`.
888
893
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 > {
889
903
if let ty:: ReVar ( ..) = * r {
890
- r. as_var ( )
904
+ Some ( r. as_var ( ) )
891
905
} else if let ty:: ReError ( guar) = * r {
892
906
self . tainted_by_errors . set ( Some ( guar) ) ;
893
907
// We use the `'static` `RegionVid` because `ReError` doesn't actually exist in the
894
908
// `UniversalRegionIndices`. This is fine because 1) it is a fallback only used if
895
909
// errors are being emitted and 2) it leaves the happy path unaffected.
896
- self . fr_static
910
+ Some ( self . fr_static )
897
911
} 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 ( )
902
913
}
903
914
}
904
915
0 commit comments