Skip to content

Commit 4097274

Browse files
comments about the workings
1 parent bc114c4 commit 4097274

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

compiler/rustc_middle/src/middle/region.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ pub struct ScopeTree {
234234
/// escape into 'static and should have no local cleanup scope.
235235
rvalue_scopes: FxHashMap<hir::ItemLocalId, Option<Scope>>,
236236

237+
/// Sometimes destruction scopes are too overarching in the sense that
238+
/// all temporaries are dropped while it may be the case that
239+
/// only a subset of temporaries is requested to be dropped.
240+
/// For instance, `x[y.z()]` indexing operation can discard the temporaries
241+
/// generated in evaluating `y.z()`.
242+
/// `eager_scopes` is a map from items to a region smaller than a destruction scope
243+
/// so that the temporaries do not need to linger unnecessarily.
237244
eager_scopes: FxHashMap<hir::ItemLocalId, Scope>,
238245

239246
/// If there are any `yield` nested within a scope, this map
@@ -380,6 +387,8 @@ impl ScopeTree {
380387
pub fn record_eager_scope(&mut self, var: hir::ItemLocalId, proposed_lifetime: Scope) {
381388
if let Some(destruction) = self.temporary_scope(var) {
382389
if self.is_subscope_of(destruction, proposed_lifetime) {
390+
// If the current temporary scope is already a subset of the proposed region,
391+
// it is safe to keep this scope and discard the proposal.
383392
return;
384393
}
385394
}

compiler/rustc_passes/src/region.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ fn mark_local_terminating_scopes<'tcx>(expr: &'tcx hir::Expr<'tcx>) -> FxHashSet
221221
Project,
222222
Local,
223223
}
224+
// Here we are looking for a chain of access to extract a place reference for
225+
// a local variable.
226+
// For instance, given `&*(&*x[y]).z().u`, `probe` will pick out
227+
// the sub-expressions `&*x[y]` and `y` as expressions making accesses to locals.
228+
// The place references generated can be discarded earlier as MIR allows,
229+
// and, thus, can be assigned a smaller region.
224230
loop {
225231
match nested_expr.kind {
226232
hir::ExprKind::Path(hir::QPath::Resolved(
@@ -268,6 +274,9 @@ fn mark_local_terminating_scopes<'tcx>(expr: &'tcx hir::Expr<'tcx>) -> FxHashSet
268274
}
269275
}
270276
OpTy::Project => {
277+
// The generated MIR produces temporaries for the each of the nested references,
278+
// such as `&x`, `&&x` in `(&&&x).y` for the field access of `y`.
279+
// These temporaries must stay alive even after the field access.
271280
ref_level.clear();
272281
}
273282
OpTy::Local => {
@@ -290,6 +299,8 @@ fn mark_local_terminating_scopes<'tcx>(expr: &'tcx hir::Expr<'tcx>) -> FxHashSet
290299
| hir::ExprKind::Field(..)
291300
| hir::ExprKind::Index(..)
292301
| hir::ExprKind::Path(..) => self.probe(expr),
302+
303+
// We do not probe into other function bodies or blocks.
293304
hir::ExprKind::Block(..)
294305
| hir::ExprKind::Closure(..)
295306
| hir::ExprKind::ConstBlock(..) => {}
@@ -300,7 +311,6 @@ fn mark_local_terminating_scopes<'tcx>(expr: &'tcx hir::Expr<'tcx>) -> FxHashSet
300311
let mut locals = Default::default();
301312
let mut resolution_visitor = LocalAccessResolutionVisitor { locals: &mut locals };
302313
resolution_visitor.visit_expr(expr);
303-
// visitor.terminating_scopes.extend(locals.iter().copied());
304314
locals
305315
}
306316

0 commit comments

Comments
 (0)