1
Fork 0

Remove ScopeDepth entirely.

The scope depth was tracked, but never actually used for anything.
This commit is contained in:
Mara Bos 2025-03-28 08:15:18 +01:00
parent 3f690c2257
commit 3a9a5770ef
3 changed files with 18 additions and 30 deletions

View file

@ -23,18 +23,11 @@ use tracing::debug;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
struct Context { struct Context {
/// The scope that contains any new variables declared, plus its depth in /// The scope that contains any new variables declared.
/// the scope tree.
var_parent: Option<Scope>, var_parent: Option<Scope>,
/// Region parent of expressions, etc., plus its depth in the scope tree. /// Region parent of expressions, etc.
parent: Option<(Scope, ScopeDepth)>, parent: Option<Scope>,
}
impl Context {
fn set_var_parent(&mut self) {
self.var_parent = self.parent.map(|(p, _)| p);
}
} }
struct ScopeResolutionVisitor<'tcx> { struct ScopeResolutionVisitor<'tcx> {
@ -119,7 +112,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
// itself has returned. // itself has returned.
visitor.enter_node_scope_with_dtor(blk.hir_id.local_id); visitor.enter_node_scope_with_dtor(blk.hir_id.local_id);
visitor.cx.set_var_parent(); visitor.cx.var_parent = visitor.cx.parent;
{ {
// This block should be kept approximately in sync with // This block should be kept approximately in sync with
@ -138,7 +131,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
local_id: blk.hir_id.local_id, local_id: blk.hir_id.local_id,
data: ScopeData::Remainder(FirstStatementIndex::new(i)), data: ScopeData::Remainder(FirstStatementIndex::new(i)),
}); });
visitor.cx.set_var_parent(); visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_stmt(statement); visitor.visit_stmt(statement);
// We need to back out temporarily to the last enclosing scope // We need to back out temporarily to the last enclosing scope
// for the `else` block, so that even the temporaries receiving // for the `else` block, so that even the temporaries receiving
@ -163,7 +156,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
local_id: blk.hir_id.local_id, local_id: blk.hir_id.local_id,
data: ScopeData::Remainder(FirstStatementIndex::new(i)), data: ScopeData::Remainder(FirstStatementIndex::new(i)),
}); });
visitor.cx.set_var_parent(); visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_stmt(statement) visitor.visit_stmt(statement)
} }
hir::StmtKind::Item(..) => { hir::StmtKind::Item(..) => {
@ -213,7 +206,7 @@ fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir:
visitor.terminating_scopes.insert(arm.hir_id.local_id); visitor.terminating_scopes.insert(arm.hir_id.local_id);
visitor.enter_node_scope_with_dtor(arm.hir_id.local_id); visitor.enter_node_scope_with_dtor(arm.hir_id.local_id);
visitor.cx.set_var_parent(); visitor.cx.var_parent = visitor.cx.parent;
if let Some(expr) = arm.guard if let Some(expr) = arm.guard
&& !has_let_expr(expr) && !has_let_expr(expr)
@ -490,7 +483,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
ScopeData::IfThen ScopeData::IfThen
}; };
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data }); visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
visitor.cx.set_var_parent(); visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_expr(cond); visitor.visit_expr(cond);
visitor.visit_expr(then); visitor.visit_expr(then);
visitor.cx = expr_cx; visitor.cx = expr_cx;
@ -505,7 +498,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
ScopeData::IfThen ScopeData::IfThen
}; };
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data }); visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
visitor.cx.set_var_parent(); visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_expr(cond); visitor.visit_expr(cond);
visitor.visit_expr(then); visitor.visit_expr(then);
visitor.cx = expr_cx; visitor.cx = expr_cx;
@ -545,7 +538,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
// Keep traversing up while we can. // Keep traversing up while we can.
match visitor.scope_tree.parent_map.get(&scope) { match visitor.scope_tree.parent_map.get(&scope) {
// Don't cross from closure bodies to their parent. // Don't cross from closure bodies to their parent.
Some(&(superscope, _)) => match superscope.data { Some(&superscope) => match superscope.data {
ScopeData::CallSite => break, ScopeData::CallSite => break,
_ => scope = superscope, _ => scope = superscope,
}, },
@ -782,19 +775,16 @@ fn resolve_local<'tcx>(
impl<'tcx> ScopeResolutionVisitor<'tcx> { impl<'tcx> ScopeResolutionVisitor<'tcx> {
/// Records the current parent (if any) as the parent of `child_scope`. /// Records the current parent (if any) as the parent of `child_scope`.
/// Returns the depth of `child_scope`. /// Returns the depth of `child_scope`.
fn record_child_scope(&mut self, child_scope: Scope) -> ScopeDepth { fn record_child_scope(&mut self, child_scope: Scope) {
let parent = self.cx.parent; let parent = self.cx.parent;
self.scope_tree.record_scope_parent(child_scope, parent); self.scope_tree.record_scope_parent(child_scope, parent);
// If `child_scope` has no parent, it must be the root node, and so has
// a depth of 1. Otherwise, its depth is one more than its parent's.
parent.map_or(1, |(_p, d)| d + 1)
} }
/// Records the current parent (if any) as the parent of `child_scope`, /// Records the current parent (if any) as the parent of `child_scope`,
/// and sets `child_scope` as the new current parent. /// and sets `child_scope` as the new current parent.
fn enter_scope(&mut self, child_scope: Scope) { fn enter_scope(&mut self, child_scope: Scope) {
let child_depth = self.record_child_scope(child_scope); self.record_child_scope(child_scope);
self.cx.parent = Some((child_scope, child_depth)); self.cx.parent = Some(child_scope);
} }
fn enter_node_scope_with_dtor(&mut self, id: hir::ItemLocalId) { fn enter_node_scope_with_dtor(&mut self, id: hir::ItemLocalId) {
@ -855,7 +845,7 @@ impl<'tcx> Visitor<'tcx> for ScopeResolutionVisitor<'tcx> {
self.enter_body(body.value.hir_id, |this| { self.enter_body(body.value.hir_id, |this| {
if this.tcx.hir_body_owner_kind(owner_id).is_fn_or_closure() { if this.tcx.hir_body_owner_kind(owner_id).is_fn_or_closure() {
// The arguments and `self` are parented to the fn. // The arguments and `self` are parented to the fn.
this.cx.set_var_parent(); this.cx.var_parent = this.cx.parent;
for param in body.params { for param in body.params {
this.visit_pat(param.pat); this.visit_pat(param.pat);
} }

View file

@ -199,8 +199,6 @@ impl Scope {
} }
} }
pub type ScopeDepth = u32;
/// The region scope tree encodes information about region relationships. /// The region scope tree encodes information about region relationships.
#[derive(Default, Debug, HashStable)] #[derive(Default, Debug, HashStable)]
pub struct ScopeTree { pub struct ScopeTree {
@ -213,7 +211,7 @@ pub struct ScopeTree {
/// conditional expression or repeating block. (Note that the /// conditional expression or repeating block. (Note that the
/// enclosing scope ID for the block associated with a closure is /// enclosing scope ID for the block associated with a closure is
/// the closure itself.) /// the closure itself.)
pub parent_map: FxIndexMap<Scope, (Scope, ScopeDepth)>, pub parent_map: FxIndexMap<Scope, Scope>,
/// Maps from a variable or binding ID to the block in which that /// Maps from a variable or binding ID to the block in which that
/// variable is declared. /// variable is declared.
@ -328,7 +326,7 @@ pub struct YieldData {
} }
impl ScopeTree { impl ScopeTree {
pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) { pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
debug!("{:?}.parent = {:?}", child, parent); debug!("{:?}.parent = {:?}", child, parent);
if let Some(p) = parent { if let Some(p) = parent {
@ -353,7 +351,7 @@ impl ScopeTree {
/// Returns the narrowest scope that encloses `id`, if any. /// Returns the narrowest scope that encloses `id`, if any.
pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> { pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
self.parent_map.get(&id).cloned().map(|(p, _)| p) self.parent_map.get(&id).cloned()
} }
/// Returns the lifetime of the local variable `var_id`, if any. /// Returns the lifetime of the local variable `var_id`, if any.

View file

@ -38,7 +38,7 @@ impl RvalueScopes {
let mut id = Scope { local_id: expr_id, data: ScopeData::Node }; let mut id = Scope { local_id: expr_id, data: ScopeData::Node };
let mut backwards_incompatible = None; let mut backwards_incompatible = None;
while let Some(&(p, _)) = region_scope_tree.parent_map.get(&id) { while let Some(&p) = region_scope_tree.parent_map.get(&id) {
match p.data { match p.data {
ScopeData::Destruction => { ScopeData::Destruction => {
debug!("temporary_scope({expr_id:?}) = {id:?} [enclosing]"); debug!("temporary_scope({expr_id:?}) = {id:?} [enclosing]");