22
33use std:: collections:: VecDeque ;
44
5+ use rustc:: infer:: NLLRegionVariableOrigin ;
56use rustc:: mir:: {
67 Body , CastKind , ConstraintCategory , FakeReadCause , Local , Location , Operand , Place , Rvalue ,
78 Statement , StatementKind , TerminatorKind ,
89} ;
910use rustc:: ty:: adjustment:: PointerCast ;
10- use rustc:: ty:: { self , TyCtxt } ;
11+ use rustc:: ty:: { self , RegionVid , TyCtxt } ;
1112use rustc_data_structures:: fx:: FxHashSet ;
1213use rustc_errors:: { Applicability , DiagnosticBuilder } ;
1314use rustc_index:: vec:: IndexVec ;
1415use rustc_span:: symbol:: Symbol ;
1516use rustc_span:: Span ;
1617
1718use crate :: borrow_check:: {
18- borrow_set:: BorrowData , nll :: ConstraintDescription , region_infer :: Cause , MirBorrowckCtxt ,
19- WriteKind ,
19+ borrow_set:: BorrowData , diagnostics :: RegionErrorNamingCtx , nll :: ConstraintDescription ,
20+ region_infer :: Cause , MirBorrowckCtxt , WriteKind ,
2021} ;
2122
2223use super :: { find_use, RegionName , UseSpans } ;
@@ -254,6 +255,32 @@ impl BorrowExplanation {
254255}
255256
256257impl < ' cx , ' tcx > MirBorrowckCtxt < ' cx , ' tcx > {
258+ fn free_region_constraint_info (
259+ & self ,
260+ borrow_region : RegionVid ,
261+ outlived_region : RegionVid ,
262+ ) -> ( ConstraintCategory , bool , Span , Option < RegionName > ) {
263+ let ( category, from_closure, span) = self . nonlexical_regioncx . best_blame_constraint (
264+ & self . body ,
265+ borrow_region,
266+ NLLRegionVariableOrigin :: FreeRegion ,
267+ |r| {
268+ self . nonlexical_regioncx . provides_universal_region (
269+ r,
270+ borrow_region,
271+ outlived_region,
272+ )
273+ } ,
274+ ) ;
275+
276+ let mut renctx = RegionErrorNamingCtx :: new ( ) ;
277+ let outlived_fr_name =
278+ self . nonlexical_regioncx . give_region_a_name ( self , & mut renctx, outlived_region) ;
279+ // TODO(mark-i-m): just return the region and let the caller name it
280+
281+ ( category, from_closure, span, outlived_fr_name)
282+ }
283+
257284 /// Returns structured explanation for *why* the borrow contains the
258285 /// point from `location`. This is key for the "3-point errors"
259286 /// [described in the NLL RFC][d].
@@ -285,7 +312,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
285312 let borrow_region_vid = borrow. region ;
286313 debug ! ( "explain_why_borrow_contains_point: borrow_region_vid={:?}" , borrow_region_vid) ;
287314
288- let region_sub = regioncx. find_sub_region_live_at ( borrow_region_vid, location) ;
315+ let region_sub =
316+ self . nonlexical_regioncx . find_sub_region_live_at ( borrow_region_vid, location) ;
289317 debug ! ( "explain_why_borrow_contains_point: region_sub={:?}" , region_sub) ;
290318
291319 match find_use:: find ( body, regioncx, tcx, region_sub, location) {
@@ -330,9 +358,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
330358
331359 None => {
332360 if let Some ( region) = regioncx. to_error_region_vid ( borrow_region_vid) {
333- let ( category, from_closure, span, region_name) = self
334- . nonlexical_regioncx
335- . free_region_constraint_info ( self , borrow_region_vid, region) ;
361+ let ( category, from_closure, span, region_name) =
362+ self . free_region_constraint_info ( borrow_region_vid, region) ;
336363 if let Some ( region_name) = region_name {
337364 let opt_place_desc = self . describe_place ( borrow. borrowed_place . as_ref ( ) ) ;
338365 BorrowExplanation :: MustBeValidFor {
@@ -345,14 +372,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
345372 } else {
346373 debug ! (
347374 "explain_why_borrow_contains_point: \
348- Could not generate a region name"
375+ Could not generate a region name"
349376 ) ;
350377 BorrowExplanation :: Unexplained
351378 }
352379 } else {
353380 debug ! (
354381 "explain_why_borrow_contains_point: \
355- Could not generate an error region vid"
382+ Could not generate an error region vid"
356383 ) ;
357384 BorrowExplanation :: Unexplained
358385 }
0 commit comments