11use rustc_index:: IndexVec ;
22use rustc_index:: interval:: { IntervalSet , SparseIntervalMatrix } ;
33use rustc_middle:: mir:: Local ;
4- use rustc_middle:: ty:: { GenericArg , RegionVid , Ty } ;
4+ use rustc_middle:: ty:: { GenericArg , RegionVid , Ty , TyCtxt } ;
55use rustc_mir_dataflow:: points:: PointIndex ;
66
7- use crate :: BorrowckInferCtxt ;
87use crate :: polonius:: { ConstraintDirection , LiveRegionVariances } ;
98use crate :: region_infer:: values:: LivenessValues ;
109use crate :: type_check:: liveness:: LivenessComputation ;
1110use crate :: universal_regions:: UniversalRegions ;
1211
12+ /// The source of liveness information for a given region.
13+ pub ( super ) trait LivenessSource {
14+ fn liveness_for_region ( & mut self , region : RegionVid ) -> RegionLiveness < ' _ > ;
15+ }
16+
17+ /// For a given region, the relevant liveness and variance information.
18+ pub ( super ) struct RegionLiveness < ' a > {
19+ pub ( super ) direction : ConstraintDirection ,
20+ live_points : Option < & ' a IntervalSet < PointIndex > > ,
21+ }
22+
23+ impl < ' a > RegionLiveness < ' a > {
24+ #[ inline]
25+ pub ( super ) fn new < ' tcx > (
26+ region : RegionVid ,
27+ live_region_variances : & LiveRegionVariances ,
28+ universal_regions : & UniversalRegions < ' tcx > ,
29+ live_points : & ' a SparseIntervalMatrix < RegionVid , PointIndex > ,
30+ ) -> Self {
31+ // Universal regions propagate loans along the CFG, i.e. forwards only.
32+ let is_universal_region = universal_regions. is_universal_region ( region) ;
33+
34+ // Note: there currently are cases related to promoted and const generics, where we don't yet
35+ // have variance information (possibly about temporary regions created when typeck sanitizes the
36+ // promoteds). Until that is done, we conservatively fallback to maximizing reachability by
37+ // adding a bidirectional edge here. This will not limit traversal whatsoever, and thus
38+ // propagate liveness when needed.
39+ //
40+ // FIXME: add the missing variance information and remove this fallback bidirectional edge.
41+ let direction = if is_universal_region {
42+ ConstraintDirection :: Forward
43+ } else {
44+ live_region_variances
45+ . get ( region)
46+ . copied ( )
47+ . flatten ( )
48+ . unwrap_or ( ConstraintDirection :: Bidirectional )
49+ } ;
50+ let live_points = live_points. row ( region) ;
51+ Self { direction, live_points }
52+ }
53+
54+ pub ( super ) fn is_live_at ( & self , point : PointIndex ) -> bool {
55+ self . live_points . map_or ( false , |points| points. contains ( point) )
56+ }
57+ }
58+
59+ /// The data needed to compute region liveness on-demand while traversing the localized outlives
60+ /// constraint graph to compute loan liveness.
1361#[ derive( Default ) ]
1462pub ( crate ) struct DeferredLocals < ' tcx > {
1563 /// For each region, the local whose liveness is deferred.
@@ -24,14 +72,12 @@ pub(crate) struct DeferredLocals<'tcx> {
2472impl < ' tcx > DeferredLocals < ' tcx > {
2573 pub ( crate ) fn defer_local (
2674 & mut self ,
27- infcx : & BorrowckInferCtxt < ' tcx > ,
75+ tcx : TyCtxt < ' tcx > ,
2876 universal_regions : & UniversalRegions < ' tcx > ,
2977 local : Local ,
3078 local_ty : Ty < ' tcx > ,
3179 dropck_kinds : & [ GenericArg < ' tcx > ] ,
3280 ) {
33- let tcx = infcx. tcx ;
34-
3581 // We already have drop data for this local, because we need to register
3682 // region constraints eagerly. So, we'll store this so we don't need to
3783 // recompute.
@@ -42,7 +88,7 @@ impl<'tcx> DeferredLocals<'tcx> {
4288 // we can trace liveness for the local containing it.
4389 let by_region = & mut self . by_region ;
4490 tcx. for_each_free_region ( & local_ty, |region| {
45- // See note in [ `VarianceExtractor::record_variance`] .
91+ // See note in `VarianceExtractor::record_variance`.
4692 if region. is_bound ( ) || region. is_erased ( ) {
4793 return ;
4894 }
@@ -58,7 +104,7 @@ impl<'tcx> DeferredLocals<'tcx> {
58104 } ) ;
59105 }
60106
61- /// For a given region, compute the liveness for the local containing it, if if is deferred.
107+ /// For a given region, compute the liveness for the local containing it, if it is deferred.
62108 #[ inline]
63109 pub ( crate ) fn compute_deferred_local (
64110 & mut self ,
@@ -80,45 +126,3 @@ impl<'tcx> DeferredLocals<'tcx> {
80126 } ) ;
81127 }
82128}
83-
84- /// For a given region, the relevant liveness and variance information.
85- pub ( super ) struct RegionLiveness < ' a > {
86- pub ( super ) direction : ConstraintDirection ,
87- live_points : Option < & ' a IntervalSet < PointIndex > > ,
88- }
89-
90- impl < ' a > RegionLiveness < ' a > {
91- #[ inline]
92- pub ( super ) fn new < ' tcx > (
93- region : RegionVid ,
94- live_region_variances : & LiveRegionVariances ,
95- universal_regions : & UniversalRegions < ' tcx > ,
96- live_points : & ' a SparseIntervalMatrix < RegionVid , PointIndex > ,
97- ) -> Self {
98- // Universal regions propagate loans along the CFG, i.e. forwards only.
99- let is_universal_region = universal_regions. is_universal_region ( region) ;
100-
101- // Note: there currently are cases related to promoted and const generics, where we don't yet
102- // have variance information (possibly about temporary regions created when typeck sanitizes the
103- // promoteds). Until that is done, we conservatively fallback to maximizing reachability by
104- // adding a bidirectional edge here. This will not limit traversal whatsoever, and thus
105- // propagate liveness when needed.
106- //
107- // FIXME: add the missing variance information and remove this fallback bidirectional edge.
108- let direction = if is_universal_region {
109- ConstraintDirection :: Forward
110- } else {
111- live_region_variances
112- . get ( region)
113- . copied ( )
114- . flatten ( )
115- . unwrap_or ( ConstraintDirection :: Bidirectional )
116- } ;
117- let live_points = live_points. row ( region) ;
118- Self { direction, live_points }
119- }
120-
121- pub ( super ) fn is_live_at ( & self , point : PointIndex ) -> bool {
122- self . live_points . map_or ( false , |points| points. contains ( point) )
123- }
124- }
0 commit comments