@@ -23,18 +23,11 @@ use tracing::debug;
23
23
24
24
#[ derive( Debug , Copy , Clone ) ]
25
25
struct Context {
26
- /// The scope that contains any new variables declared, plus its depth in
27
- /// the scope tree.
26
+ /// The scope that contains any new variables declared.
28
27
var_parent : Option < Scope > ,
29
28
30
- /// Region parent of expressions, etc., plus its depth in the scope tree.
31
- parent : Option < ( Scope , ScopeDepth ) > ,
32
- }
33
-
34
- impl Context {
35
- fn set_var_parent ( & mut self ) {
36
- self . var_parent = self . parent . map ( |( p, _) | p) ;
37
- }
29
+ /// Region parent of expressions, etc.
30
+ parent : Option < Scope > ,
38
31
}
39
32
40
33
struct ScopeResolutionVisitor < ' tcx > {
@@ -119,7 +112,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
119
112
// itself has returned.
120
113
121
114
visitor. enter_node_scope_with_dtor ( blk. hir_id . local_id ) ;
122
- visitor. cx . set_var_parent ( ) ;
115
+ visitor. cx . var_parent = visitor . cx . parent ;
123
116
124
117
{
125
118
// This block should be kept approximately in sync with
@@ -138,7 +131,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
138
131
local_id : blk. hir_id . local_id ,
139
132
data : ScopeData :: Remainder ( FirstStatementIndex :: new ( i) ) ,
140
133
} ) ;
141
- visitor. cx . set_var_parent ( ) ;
134
+ visitor. cx . var_parent = visitor . cx . parent ;
142
135
visitor. visit_stmt ( statement) ;
143
136
// We need to back out temporarily to the last enclosing scope
144
137
// 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
163
156
local_id : blk. hir_id . local_id ,
164
157
data : ScopeData :: Remainder ( FirstStatementIndex :: new ( i) ) ,
165
158
} ) ;
166
- visitor. cx . set_var_parent ( ) ;
159
+ visitor. cx . var_parent = visitor . cx . parent ;
167
160
visitor. visit_stmt ( statement)
168
161
}
169
162
hir:: StmtKind :: Item ( ..) => {
@@ -213,7 +206,7 @@ fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir:
213
206
visitor. terminating_scopes . insert ( arm. hir_id . local_id ) ;
214
207
215
208
visitor. enter_node_scope_with_dtor ( arm. hir_id . local_id ) ;
216
- visitor. cx . set_var_parent ( ) ;
209
+ visitor. cx . var_parent = visitor . cx . parent ;
217
210
218
211
if let Some ( expr) = arm. guard
219
212
&& !has_let_expr ( expr)
@@ -490,7 +483,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
490
483
ScopeData :: IfThen
491
484
} ;
492
485
visitor. enter_scope ( Scope { local_id : then. hir_id . local_id , data } ) ;
493
- visitor. cx . set_var_parent ( ) ;
486
+ visitor. cx . var_parent = visitor . cx . parent ;
494
487
visitor. visit_expr ( cond) ;
495
488
visitor. visit_expr ( then) ;
496
489
visitor. cx = expr_cx;
@@ -505,7 +498,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
505
498
ScopeData :: IfThen
506
499
} ;
507
500
visitor. enter_scope ( Scope { local_id : then. hir_id . local_id , data } ) ;
508
- visitor. cx . set_var_parent ( ) ;
501
+ visitor. cx . var_parent = visitor . cx . parent ;
509
502
visitor. visit_expr ( cond) ;
510
503
visitor. visit_expr ( then) ;
511
504
visitor. cx = expr_cx;
@@ -545,7 +538,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
545
538
// Keep traversing up while we can.
546
539
match visitor. scope_tree . parent_map . get ( & scope) {
547
540
// Don't cross from closure bodies to their parent.
548
- Some ( & ( superscope, _ ) ) => match superscope. data {
541
+ Some ( & superscope) => match superscope. data {
549
542
ScopeData :: CallSite => break ,
550
543
_ => scope = superscope,
551
544
} ,
@@ -781,20 +774,16 @@ fn resolve_local<'tcx>(
781
774
782
775
impl < ' tcx > ScopeResolutionVisitor < ' tcx > {
783
776
/// Records the current parent (if any) as the parent of `child_scope`.
784
- /// Returns the depth of `child_scope`.
785
- fn record_child_scope ( & mut self , child_scope : Scope ) -> ScopeDepth {
777
+ fn record_child_scope ( & mut self , child_scope : Scope ) {
786
778
let parent = self . cx . parent ;
787
779
self . scope_tree . record_scope_parent ( child_scope, parent) ;
788
- // If `child_scope` has no parent, it must be the root node, and so has
789
- // a depth of 1. Otherwise, its depth is one more than its parent's.
790
- parent. map_or ( 1 , |( _p, d) | d + 1 )
791
780
}
792
781
793
782
/// Records the current parent (if any) as the parent of `child_scope`,
794
783
/// and sets `child_scope` as the new current parent.
795
784
fn enter_scope ( & mut self , child_scope : Scope ) {
796
- let child_depth = self . record_child_scope ( child_scope) ;
797
- self . cx . parent = Some ( ( child_scope, child_depth ) ) ;
785
+ self . record_child_scope ( child_scope) ;
786
+ self . cx . parent = Some ( child_scope) ;
798
787
}
799
788
800
789
fn enter_node_scope_with_dtor ( & mut self , id : hir:: ItemLocalId ) {
@@ -855,7 +844,7 @@ impl<'tcx> Visitor<'tcx> for ScopeResolutionVisitor<'tcx> {
855
844
self . enter_body ( body. value . hir_id , |this| {
856
845
if this. tcx . hir_body_owner_kind ( owner_id) . is_fn_or_closure ( ) {
857
846
// The arguments and `self` are parented to the fn.
858
- this. cx . set_var_parent ( ) ;
847
+ this. cx . var_parent = this . cx . parent ;
859
848
for param in body. params {
860
849
this. visit_pat ( param. pat ) ;
861
850
}
0 commit comments