1 //! Drop range analysis finds the portions of the tree where a value is guaranteed to be dropped
2 //! (i.e. moved, uninitialized, etc.). This is used to exclude the types of those values from the
3 //! generator type. See `InteriorVisitor::record` for where the results of this analysis are used.
5 //! There are three phases to this analysis:
6 //! 1. Use `ExprUseVisitor` to identify the interesting values that are consumed and borrowed.
7 //! 2. Use `DropRangeVisitor` to find where the interesting values are dropped or reinitialized,
8 //! and also build a control flow graph.
9 //! 3. Use `DropRanges::propagate_to_fixpoint` to flow the dropped/reinitialized information through
10 //! the CFG and find the exact points where we know a value is definitely dropped.
12 //! The end result is a data structure that maps the post-order index of each node in the HIR tree
13 //! to a set of values that are known to be dropped at that location.
15 use self::cfg_build::build_control_flow_graph;
16 use self::record_consumed_borrow::find_consumed_and_borrowed;
17 use crate::check::FnCtxt;
18 use hir::def_id::DefId;
19 use hir::{Body, HirId, HirIdMap, Node};
20 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
22 use rustc_index::bit_set::BitSet;
23 use rustc_index::vec::IndexVec;
24 use rustc_middle::hir::map::Map;
25 use rustc_middle::hir::place::{PlaceBase, PlaceWithHirId};
27 use std::collections::BTreeMap;
33 mod record_consumed_borrow;
35 pub fn compute_drop_ranges<'a, 'tcx>(
36 fcx: &'a FnCtxt<'a, 'tcx>,
38 body: &'tcx Body<'tcx>,
40 if fcx.sess().opts.unstable_opts.drop_tracking {
41 let consumed_borrowed_places = find_consumed_and_borrowed(fcx, def_id, body);
43 let typeck_results = &fcx.typeck_results.borrow();
44 let num_exprs = fcx.tcx.region_scope_tree(def_id).body_expr_count(body.id()).unwrap_or(0);
45 let (mut drop_ranges, borrowed_temporaries) = build_control_flow_graph(
49 consumed_borrowed_places,
54 drop_ranges.propagate_to_fixpoint();
56 debug!("borrowed_temporaries = {borrowed_temporaries:?}");
58 tracked_value_map: drop_ranges.tracked_value_map,
59 nodes: drop_ranges.nodes,
60 borrowed_temporaries: Some(borrowed_temporaries),
63 // If drop range tracking is not enabled, skip all the analysis and produce an
64 // empty set of DropRanges.
66 tracked_value_map: FxHashMap::default(),
67 nodes: IndexVec::new(),
68 borrowed_temporaries: None,
73 /// Applies `f` to consumable node in the HIR subtree pointed to by `place`.
75 /// This includes the place itself, and if the place is a reference to a local
76 /// variable then `f` is also called on the HIR node for that variable as well.
78 /// For example, if `place` points to `foo()`, then `f` is called once for the
79 /// result of `foo`. On the other hand, if `place` points to `x` then `f` will
80 /// be called both on the `ExprKind::Path` node that represents the expression
81 /// as well as the HirId of the local `x` itself.
82 fn for_each_consumable<'tcx>(hir: Map<'tcx>, place: TrackedValue, mut f: impl FnMut(TrackedValue)) {
84 let node = hir.find(place.hir_id());
85 if let Some(Node::Expr(expr)) = node {
87 hir::ExprKind::Path(hir::QPath::Resolved(
89 hir::Path { res: hir::def::Res::Local(hir_id), .. },
91 f(TrackedValue::Variable(*hir_id));
98 rustc_index::newtype_index! {
99 pub struct PostOrderId {
100 DEBUG_FORMAT = "id({})",
104 rustc_index::newtype_index! {
105 pub struct TrackedValueIndex {
106 DEBUG_FORMAT = "hidx({})",
110 /// Identifies a value whose drop state we need to track.
111 #[derive(PartialEq, Eq, Hash, Clone, Copy)]
113 /// Represents a named variable, such as a let binding, parameter, or upvar.
115 /// The HirId points to the variable's definition site.
117 /// A value produced as a result of an expression.
119 /// The HirId points to the expression that returns this value.
123 impl Debug for TrackedValue {
124 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125 ty::tls::with_opt(|opt_tcx| {
126 if let Some(tcx) = opt_tcx {
127 write!(f, "{}", tcx.hir().node_to_string(self.hir_id()))
130 Self::Variable(hir_id) => write!(f, "Variable({:?})", hir_id),
131 Self::Temporary(hir_id) => write!(f, "Temporary({:?})", hir_id),
139 fn hir_id(&self) -> HirId {
141 TrackedValue::Variable(hir_id) | TrackedValue::Temporary(hir_id) => *hir_id,
145 fn from_place_with_projections_allowed(place_with_id: &PlaceWithHirId<'_>) -> Self {
146 match place_with_id.place.base {
147 PlaceBase::Rvalue | PlaceBase::StaticItem => {
148 TrackedValue::Temporary(place_with_id.hir_id)
150 PlaceBase::Local(hir_id)
151 | PlaceBase::Upvar(ty::UpvarId { var_path: ty::UpvarPath { hir_id }, .. }) => {
152 TrackedValue::Variable(hir_id)
158 /// Represents a reason why we might not be able to convert a HirId or Place
159 /// into a tracked value.
161 enum TrackedValueConversionError {
162 /// Place projects are not currently supported.
164 /// The reasoning around these is kind of subtle, so we choose to be more
165 /// conservative around these for now. There is no reason in theory we
166 /// cannot support these, we just have not implemented it yet.
167 PlaceProjectionsNotSupported,
170 impl TryFrom<&PlaceWithHirId<'_>> for TrackedValue {
171 type Error = TrackedValueConversionError;
173 fn try_from(place_with_id: &PlaceWithHirId<'_>) -> Result<Self, Self::Error> {
174 if !place_with_id.place.projections.is_empty() {
176 "TrackedValue from PlaceWithHirId: {:?} has projections, which are not supported.",
179 return Err(TrackedValueConversionError::PlaceProjectionsNotSupported);
182 Ok(TrackedValue::from_place_with_projections_allowed(place_with_id))
186 pub struct DropRanges {
187 tracked_value_map: FxHashMap<TrackedValue, TrackedValueIndex>,
188 nodes: IndexVec<PostOrderId, NodeInfo>,
189 borrowed_temporaries: Option<FxHashSet<HirId>>,
193 pub fn is_dropped_at(&self, hir_id: HirId, location: usize) -> bool {
194 self.tracked_value_map
195 .get(&TrackedValue::Temporary(hir_id))
196 .or(self.tracked_value_map.get(&TrackedValue::Variable(hir_id)))
198 .map_or(false, |tracked_value_id| {
199 self.expect_node(location.into()).drop_state.contains(tracked_value_id)
203 pub fn is_borrowed_temporary(&self, expr: &hir::Expr<'_>) -> bool {
204 if let Some(b) = &self.borrowed_temporaries { b.contains(&expr.hir_id) } else { true }
207 /// Returns a reference to the NodeInfo for a node, panicking if it does not exist
208 fn expect_node(&self, id: PostOrderId) -> &NodeInfo {
213 /// Tracks information needed to compute drop ranges.
214 struct DropRangesBuilder {
215 /// The core of DropRangesBuilder is a set of nodes, which each represent
216 /// one expression. We primarily refer to them by their index in a
217 /// post-order traversal of the HIR tree, since this is what
218 /// generator_interior uses to talk about yield positions.
220 /// This IndexVec keeps the relevant details for each node. See the
221 /// NodeInfo struct for more details, but this information includes things
222 /// such as the set of control-flow successors, which variables are dropped
223 /// or reinitialized, and whether each variable has been inferred to be
224 /// known-dropped or potentially reinitialized at each point.
225 nodes: IndexVec<PostOrderId, NodeInfo>,
226 /// We refer to values whose drop state we are tracking by the HirId of
227 /// where they are defined. Within a NodeInfo, however, we store the
228 /// drop-state in a bit vector indexed by a HirIdIndex
229 /// (see NodeInfo::drop_state). The hir_id_map field stores the mapping
230 /// from HirIds to the HirIdIndex that is used to represent that value in
232 tracked_value_map: FxHashMap<TrackedValue, TrackedValueIndex>,
234 /// When building the control flow graph, we don't always know the
235 /// post-order index of the target node at the point we encounter it.
236 /// For example, this happens with break and continue. In those cases,
237 /// we store a pair of the PostOrderId of the source and the HirId
238 /// of the target. Once we have gathered all of these edges, we make a
239 /// pass over the set of deferred edges (see process_deferred_edges in
240 /// cfg_build.rs), look up the PostOrderId for the target (since now the
241 /// post-order index for all nodes is known), and add missing control flow
243 deferred_edges: Vec<(PostOrderId, HirId)>,
244 /// This maps HirIds of expressions to their post-order index. It is
245 /// used in process_deferred_edges to correctly add back-edges.
246 post_order_map: HirIdMap<PostOrderId>,
249 impl Debug for DropRangesBuilder {
250 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
251 f.debug_struct("DropRanges")
252 .field("hir_id_map", &self.tracked_value_map)
253 .field("post_order_maps", &self.post_order_map)
254 .field("nodes", &self.nodes.iter_enumerated().collect::<BTreeMap<_, _>>())
259 /// DropRanges keeps track of what values are definitely dropped at each point in the code.
261 /// Values of interest are defined by the hir_id of their place. Locations in code are identified
262 /// by their index in the post-order traversal. At its core, DropRanges maps
263 /// (hir_id, post_order_id) -> bool, where a true value indicates that the value is definitely
264 /// dropped at the point of the node identified by post_order_id.
265 impl DropRangesBuilder {
266 /// Returns the number of values (hir_ids) that are tracked
267 fn num_values(&self) -> usize {
268 self.tracked_value_map.len()
271 fn node_mut(&mut self, id: PostOrderId) -> &mut NodeInfo {
272 let size = self.num_values();
273 self.nodes.ensure_contains_elem(id, || NodeInfo::new(size));
277 fn add_control_edge(&mut self, from: PostOrderId, to: PostOrderId) {
278 trace!("adding control edge from {:?} to {:?}", from, to);
279 self.node_mut(from).successors.push(to);
285 /// IDs of nodes that can follow this one in the control flow
287 /// If the vec is empty, then control proceeds to the next node.
288 successors: Vec<PostOrderId>,
290 /// List of hir_ids that are dropped by this node.
291 drops: Vec<TrackedValueIndex>,
293 /// List of hir_ids that are reinitialized by this node.
294 reinits: Vec<TrackedValueIndex>,
296 /// Set of values that are definitely dropped at this point.
297 drop_state: BitSet<TrackedValueIndex>,
301 fn new(num_values: usize) -> Self {
306 drop_state: BitSet::new_filled(num_values),