@@ -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