|
34 | 34 | //!
|
35 | 35 |
|
36 | 36 | mod constraints;
|
37 |
| -pub(crate) use constraints::*; |
38 | 37 | mod dump;
|
39 |
| -pub(crate) use dump::dump_polonius_mir; |
40 | 38 | pub(crate) mod legacy;
|
41 | 39 |
|
| 40 | +use std::collections::BTreeMap; |
| 41 | + |
| 42 | +use rustc_index::bit_set::SparseBitMatrix; |
42 | 43 | use rustc_middle::mir::{Body, Location};
|
| 44 | +use rustc_middle::ty::RegionVid; |
43 | 45 | use rustc_mir_dataflow::points::PointIndex;
|
44 | 46 |
|
| 47 | +pub(crate) use self::constraints::*; |
| 48 | +pub(crate) use self::dump::dump_polonius_mir; |
45 | 49 | use crate::RegionInferenceContext;
|
46 | 50 | use crate::constraints::OutlivesConstraint;
|
47 | 51 | use crate::region_infer::values::LivenessValues;
|
48 | 52 | use crate::type_check::Locations;
|
49 | 53 | use crate::universal_regions::UniversalRegions;
|
50 | 54 |
|
51 |
| -/// Creates a constraint set for `-Zpolonius=next` by: |
52 |
| -/// - converting NLL typeck constraints to be localized |
53 |
| -/// - encoding liveness constraints |
54 |
| -pub(crate) fn create_localized_constraints<'tcx>( |
55 |
| - regioncx: &mut RegionInferenceContext<'tcx>, |
56 |
| - body: &Body<'tcx>, |
57 |
| -) -> LocalizedOutlivesConstraintSet { |
58 |
| - let mut localized_outlives_constraints = LocalizedOutlivesConstraintSet::default(); |
59 |
| - convert_typeck_constraints( |
60 |
| - body, |
61 |
| - regioncx.liveness_constraints(), |
62 |
| - regioncx.outlives_constraints(), |
63 |
| - &mut localized_outlives_constraints, |
64 |
| - ); |
65 |
| - create_liveness_constraints( |
66 |
| - body, |
67 |
| - regioncx.liveness_constraints(), |
68 |
| - regioncx.universal_regions(), |
69 |
| - &mut localized_outlives_constraints, |
70 |
| - ); |
71 |
| - |
72 |
| - // FIXME: here, we can trace loan reachability in the constraint graph and record this as loan |
73 |
| - // liveness for the next step in the chain, the NLL loan scope and active loans computations. |
74 |
| - |
75 |
| - localized_outlives_constraints |
| 55 | +/// This struct holds the data needed to create the Polonius localized constraints. |
| 56 | +pub(crate) struct PoloniusContext { |
| 57 | + /// The set of regions that are live at a given point in the CFG, used to create localized |
| 58 | + /// outlives constraints between regions that are live at connected points in the CFG. |
| 59 | + live_regions: SparseBitMatrix<PointIndex, RegionVid>, |
| 60 | + |
| 61 | + /// The expected edge direction per live region: the kind of directed edge we'll create as |
| 62 | + /// liveness constraints depends on the variance of types with respect to each contained region. |
| 63 | + live_region_variances: BTreeMap<RegionVid, ConstraintDirection>, |
| 64 | +} |
| 65 | + |
| 66 | +/// The direction a constraint can flow into. Used to create liveness constraints according to |
| 67 | +/// variance. |
| 68 | +#[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 69 | +enum ConstraintDirection { |
| 70 | + /// For covariant cases, we add a forward edge `O at P1 -> O at P2`. |
| 71 | + Forward, |
| 72 | + |
| 73 | + /// For contravariant cases, we add a backward edge `O at P2 -> O at P1` |
| 74 | + Backward, |
| 75 | + |
| 76 | + /// For invariant cases, we add both the forward and backward edges `O at P1 <-> O at P2`. |
| 77 | + Bidirectional, |
| 78 | +} |
| 79 | + |
| 80 | +impl PoloniusContext { |
| 81 | + pub(crate) fn new(num_regions: usize) -> PoloniusContext { |
| 82 | + Self { |
| 83 | + live_region_variances: BTreeMap::new(), |
| 84 | + live_regions: SparseBitMatrix::new(num_regions), |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Creates a constraint set for `-Zpolonius=next` by: |
| 89 | + /// - converting NLL typeck constraints to be localized |
| 90 | + /// - encoding liveness constraints |
| 91 | + pub(crate) fn create_localized_constraints<'tcx>( |
| 92 | + &self, |
| 93 | + regioncx: &RegionInferenceContext<'tcx>, |
| 94 | + body: &Body<'tcx>, |
| 95 | + ) -> LocalizedOutlivesConstraintSet { |
| 96 | + let mut localized_outlives_constraints = LocalizedOutlivesConstraintSet::default(); |
| 97 | + convert_typeck_constraints( |
| 98 | + body, |
| 99 | + regioncx.liveness_constraints(), |
| 100 | + regioncx.outlives_constraints(), |
| 101 | + &mut localized_outlives_constraints, |
| 102 | + ); |
| 103 | + create_liveness_constraints( |
| 104 | + body, |
| 105 | + regioncx.liveness_constraints(), |
| 106 | + regioncx.universal_regions(), |
| 107 | + &mut localized_outlives_constraints, |
| 108 | + ); |
| 109 | + |
| 110 | + // FIXME: here, we can trace loan reachability in the constraint graph and record this as loan |
| 111 | + // liveness for the next step in the chain, the NLL loan scope and active loans computations. |
| 112 | + |
| 113 | + localized_outlives_constraints |
| 114 | + } |
76 | 115 | }
|
77 | 116 |
|
78 | 117 | /// Propagate loans throughout the subset graph at a given point (with some subtleties around the
|
|
0 commit comments