@@ -25,12 +25,18 @@ use tracing::debug;
25
25
struct Context {
26
26
/// The scope that contains any new variables declared, plus its depth in
27
27
/// the scope tree.
28
- var_parent : Option < ( Scope , ScopeDepth ) > ,
28
+ var_parent : Option < Scope > ,
29
29
30
30
/// Region parent of expressions, etc., plus its depth in the scope tree.
31
31
parent : Option < ( Scope , ScopeDepth ) > ,
32
32
}
33
33
34
+ impl Context {
35
+ fn set_var_parent ( & mut self ) {
36
+ self . var_parent = self . parent . map ( |( p, _) | p) ;
37
+ }
38
+ }
39
+
34
40
struct ScopeResolutionVisitor < ' tcx > {
35
41
tcx : TyCtxt < ' tcx > ,
36
42
@@ -78,7 +84,7 @@ fn record_var_lifetime(visitor: &mut ScopeResolutionVisitor<'_>, var_id: hir::It
78
84
//
79
85
// extern fn isalnum(c: c_int) -> c_int
80
86
}
81
- Some ( ( parent_scope, _ ) ) => visitor. scope_tree . record_var_scope ( var_id, parent_scope) ,
87
+ Some ( parent_scope) => visitor. scope_tree . record_var_scope ( var_id, parent_scope) ,
82
88
}
83
89
}
84
90
@@ -113,7 +119,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
113
119
// itself has returned.
114
120
115
121
visitor. enter_node_scope_with_dtor ( blk. hir_id . local_id ) ;
116
- visitor. cx . var_parent = visitor . cx . parent ;
122
+ visitor. cx . set_var_parent ( ) ;
117
123
118
124
{
119
125
// This block should be kept approximately in sync with
@@ -132,7 +138,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
132
138
local_id : blk. hir_id . local_id ,
133
139
data : ScopeData :: Remainder ( FirstStatementIndex :: new ( i) ) ,
134
140
} ) ;
135
- visitor. cx . var_parent = visitor . cx . parent ;
141
+ visitor. cx . set_var_parent ( ) ;
136
142
visitor. visit_stmt ( statement) ;
137
143
// We need to back out temporarily to the last enclosing scope
138
144
// for the `else` block, so that even the temporaries receiving
@@ -157,7 +163,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
157
163
local_id : blk. hir_id . local_id ,
158
164
data : ScopeData :: Remainder ( FirstStatementIndex :: new ( i) ) ,
159
165
} ) ;
160
- visitor. cx . var_parent = visitor . cx . parent ;
166
+ visitor. cx . set_var_parent ( ) ;
161
167
visitor. visit_stmt ( statement)
162
168
}
163
169
hir:: StmtKind :: Item ( ..) => {
@@ -207,7 +213,7 @@ fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir:
207
213
visitor. terminating_scopes . insert ( arm. hir_id . local_id ) ;
208
214
209
215
visitor. enter_node_scope_with_dtor ( arm. hir_id . local_id ) ;
210
- visitor. cx . var_parent = visitor . cx . parent ;
216
+ visitor. cx . set_var_parent ( ) ;
211
217
212
218
if let Some ( expr) = arm. guard
213
219
&& !has_let_expr ( expr)
@@ -221,8 +227,6 @@ fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir:
221
227
}
222
228
223
229
fn resolve_pat < ' tcx > ( visitor : & mut ScopeResolutionVisitor < ' tcx > , pat : & ' tcx hir:: Pat < ' tcx > ) {
224
- visitor. record_child_scope ( Scope { local_id : pat. hir_id . local_id , data : ScopeData :: Node } ) ;
225
-
226
230
// If this is a binding then record the lifetime of that binding.
227
231
if let PatKind :: Binding ( ..) = pat. kind {
228
232
record_var_lifetime ( visitor, pat. hir_id . local_id ) ;
@@ -486,7 +490,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
486
490
ScopeData :: IfThen
487
491
} ;
488
492
visitor. enter_scope ( Scope { local_id : then. hir_id . local_id , data } ) ;
489
- visitor. cx . var_parent = visitor . cx . parent ;
493
+ visitor. cx . set_var_parent ( ) ;
490
494
visitor. visit_expr ( cond) ;
491
495
visitor. visit_expr ( then) ;
492
496
visitor. cx = expr_cx;
@@ -501,7 +505,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
501
505
ScopeData :: IfThen
502
506
} ;
503
507
visitor. enter_scope ( Scope { local_id : then. hir_id . local_id , data } ) ;
504
- visitor. cx . var_parent = visitor . cx . parent ;
508
+ visitor. cx . set_var_parent ( ) ;
505
509
visitor. visit_expr ( cond) ;
506
510
visitor. visit_expr ( then) ;
507
511
visitor. cx = expr_cx;
@@ -560,7 +564,7 @@ fn resolve_local<'tcx>(
560
564
) {
561
565
debug ! ( "resolve_local(pat={:?}, init={:?})" , pat, init) ;
562
566
563
- let blk_scope = visitor. cx . var_parent . map ( | ( p , _ ) | p ) ;
567
+ let blk_scope = visitor. cx . var_parent ;
564
568
565
569
// As an exception to the normal rules governing temporary
566
570
// lifetimes, initializers in a let have a temporary lifetime
@@ -625,10 +629,7 @@ fn resolve_local<'tcx>(
625
629
if is_binding_pat ( pat) {
626
630
visitor. scope_tree . record_rvalue_candidate (
627
631
expr. hir_id ,
628
- RvalueCandidateType :: Pattern {
629
- target : expr. hir_id . local_id ,
630
- lifetime : blk_scope,
631
- } ,
632
+ RvalueCandidate { target : expr. hir_id . local_id , lifetime : blk_scope } ,
632
633
) ;
633
634
}
634
635
}
@@ -733,10 +734,7 @@ fn resolve_local<'tcx>(
733
734
record_rvalue_scope_if_borrow_expr ( visitor, subexpr, blk_id) ;
734
735
visitor. scope_tree . record_rvalue_candidate (
735
736
subexpr. hir_id ,
736
- RvalueCandidateType :: Borrow {
737
- target : subexpr. hir_id . local_id ,
738
- lifetime : blk_id,
739
- } ,
737
+ RvalueCandidate { target : subexpr. hir_id . local_id , lifetime : blk_id } ,
740
738
) ;
741
739
}
742
740
hir:: ExprKind :: Struct ( _, fields, _) => {
@@ -857,13 +855,12 @@ impl<'tcx> Visitor<'tcx> for ScopeResolutionVisitor<'tcx> {
857
855
self . enter_body ( body. value . hir_id , |this| {
858
856
if this. tcx . hir_body_owner_kind ( owner_id) . is_fn_or_closure ( ) {
859
857
// The arguments and `self` are parented to the fn.
860
- this. cx . var_parent = this . cx . parent . take ( ) ;
858
+ this. cx . set_var_parent ( ) ;
861
859
for param in body. params {
862
860
this. visit_pat ( param. pat ) ;
863
861
}
864
862
865
863
// The body of the every fn is a root scope.
866
- this. cx . parent = this. cx . var_parent ;
867
864
this. visit_expr ( body. value )
868
865
} else {
869
866
// Only functions have an outer terminating (drop) scope, while
0 commit comments