]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/liveness/mod.rs
Remove in_band_lifetimes from borrowck
[rust.git] / compiler / rustc_borrowck / src / type_check / liveness / mod.rs
1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_middle::mir::{Body, Local};
3 use rustc_middle::ty::{RegionVid, TyCtxt};
4 use std::rc::Rc;
5
6 use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
7 use rustc_mir_dataflow::move_paths::MoveData;
8 use rustc_mir_dataflow::ResultsCursor;
9
10 use crate::{
11     constraints::OutlivesConstraintSet,
12     facts::{AllFacts, AllFactsExt},
13     location::LocationTable,
14     nll::ToRegionVid,
15     region_infer::values::RegionValueElements,
16     universal_regions::UniversalRegions,
17 };
18
19 use super::TypeChecker;
20
21 mod local_use_map;
22 mod polonius;
23 mod trace;
24
25 /// Combines liveness analysis with initialization analysis to
26 /// determine which variables are live at which points, both due to
27 /// ordinary uses and drops. Returns a set of (ty, location) pairs
28 /// that indicate which types must be live at which point in the CFG.
29 /// This vector is consumed by `constraint_generation`.
30 ///
31 /// N.B., this computation requires normalization; therefore, it must be
32 /// performed before
33 pub(super) fn generate<'mir, 'tcx>(
34     typeck: &mut TypeChecker<'_, 'tcx>,
35     body: &Body<'tcx>,
36     elements: &Rc<RegionValueElements>,
37     flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
38     move_data: &MoveData<'tcx>,
39     location_table: &LocationTable,
40 ) {
41     debug!("liveness::generate");
42
43     let free_regions = regions_that_outlive_free_regions(
44         typeck.infcx.num_region_vars(),
45         &typeck.borrowck_context.universal_regions,
46         &typeck.borrowck_context.constraints.outlives_constraints,
47     );
48     let live_locals = compute_live_locals(typeck.tcx(), &free_regions, &body);
49     let facts_enabled = AllFacts::enabled(typeck.tcx());
50
51     let polonius_drop_used = if facts_enabled {
52         let mut drop_used = Vec::new();
53         polonius::populate_access_facts(typeck, body, location_table, move_data, &mut drop_used);
54         Some(drop_used)
55     } else {
56         None
57     };
58
59     if !live_locals.is_empty() || facts_enabled {
60         trace::trace(
61             typeck,
62             body,
63             elements,
64             flow_inits,
65             move_data,
66             live_locals,
67             polonius_drop_used,
68         );
69     }
70 }
71
72 // The purpose of `compute_live_locals` is to define the subset of `Local`
73 // variables for which we need to do a liveness computation. We only need
74 // to compute whether a variable `X` is live if that variable contains
75 // some region `R` in its type where `R` is not known to outlive a free
76 // region (i.e., where `R` may be valid for just a subset of the fn body).
77 fn compute_live_locals<'tcx>(
78     tcx: TyCtxt<'tcx>,
79     free_regions: &FxHashSet<RegionVid>,
80     body: &Body<'tcx>,
81 ) -> Vec<Local> {
82     let live_locals: Vec<Local> = body
83         .local_decls
84         .iter_enumerated()
85         .filter_map(|(local, local_decl)| {
86             if tcx.all_free_regions_meet(&local_decl.ty, |r| {
87                 free_regions.contains(&r.to_region_vid())
88             }) {
89                 None
90             } else {
91                 Some(local)
92             }
93         })
94         .collect();
95
96     debug!("{} total variables", body.local_decls.len());
97     debug!("{} variables need liveness", live_locals.len());
98     debug!("{} regions outlive free regions", free_regions.len());
99
100     live_locals
101 }
102
103 /// Computes all regions that are (currently) known to outlive free
104 /// regions. For these regions, we do not need to compute
105 /// liveness, since the outlives constraints will ensure that they
106 /// are live over the whole fn body anyhow.
107 fn regions_that_outlive_free_regions<'tcx>(
108     num_region_vars: usize,
109     universal_regions: &UniversalRegions<'tcx>,
110     constraint_set: &OutlivesConstraintSet<'tcx>,
111 ) -> FxHashSet<RegionVid> {
112     // Build a graph of the outlives constraints thus far. This is
113     // a reverse graph, so for each constraint `R1: R2` we have an
114     // edge `R2 -> R1`. Therefore, if we find all regions
115     // reachable from each free region, we will have all the
116     // regions that are forced to outlive some free region.
117     let rev_constraint_graph = constraint_set.reverse_graph(num_region_vars);
118     let fr_static = universal_regions.fr_static;
119     let rev_region_graph = rev_constraint_graph.region_graph(constraint_set, fr_static);
120
121     // Stack for the depth-first search. Start out with all the free regions.
122     let mut stack: Vec<_> = universal_regions.universal_regions().collect();
123
124     // Set of all free regions, plus anything that outlives them. Initially
125     // just contains the free regions.
126     let mut outlives_free_region: FxHashSet<_> = stack.iter().cloned().collect();
127
128     // Do the DFS -- for each thing in the stack, find all things
129     // that outlive it and add them to the set. If they are not,
130     // push them onto the stack for later.
131     while let Some(sub_region) = stack.pop() {
132         stack.extend(
133             rev_region_graph
134                 .outgoing_regions(sub_region)
135                 .filter(|&r| outlives_free_region.insert(r)),
136         );
137     }
138
139     // Return the final set of things we visited.
140     outlives_free_region
141 }