@@ -25,12 +25,18 @@ use tracing::debug;
2525struct Context {
2626 /// The scope that contains any new variables declared, plus its depth in
2727 /// the scope tree.
28- var_parent : Option < ( Scope , ScopeDepth ) > ,
28+ var_parent : Option < Scope > ,
2929
3030 /// Region parent of expressions, etc., plus its depth in the scope tree.
3131 parent : Option < ( Scope , ScopeDepth ) > ,
3232}
3333
34+ impl Context {
35+ fn set_var_parent ( & mut self ) {
36+ self . var_parent = self . parent . map ( |( p, _) | p) ;
37+ }
38+ }
39+
3440struct ScopeResolutionVisitor < ' tcx > {
3541 tcx : TyCtxt < ' tcx > ,
3642
@@ -78,7 +84,7 @@ fn record_var_lifetime(visitor: &mut ScopeResolutionVisitor<'_>, var_id: hir::It
7884 //
7985 // extern fn isalnum(c: c_int) -> c_int
8086 }
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) ,
8288 }
8389}
8490
@@ -113,7 +119,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
113119 // itself has returned.
114120
115121 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 ( ) ;
117123
118124 {
119125 // This block should be kept approximately in sync with
@@ -132,7 +138,7 @@ fn resolve_block<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, blk: &'tcx hi
132138 local_id : blk. hir_id . local_id ,
133139 data : ScopeData :: Remainder ( FirstStatementIndex :: new ( i) ) ,
134140 } ) ;
135- visitor. cx . var_parent = visitor . cx . parent ;
141+ visitor. cx . set_var_parent ( ) ;
136142 visitor. visit_stmt ( statement) ;
137143 // We need to back out temporarily to the last enclosing scope
138144 // 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
157163 local_id : blk. hir_id . local_id ,
158164 data : ScopeData :: Remainder ( FirstStatementIndex :: new ( i) ) ,
159165 } ) ;
160- visitor. cx . var_parent = visitor . cx . parent ;
166+ visitor. cx . set_var_parent ( ) ;
161167 visitor. visit_stmt ( statement)
162168 }
163169 hir:: StmtKind :: Item ( ..) => {
@@ -207,7 +213,7 @@ fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir:
207213 visitor. terminating_scopes . insert ( arm. hir_id . local_id ) ;
208214
209215 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 ( ) ;
211217
212218 if let Some ( expr) = arm. guard
213219 && !has_let_expr ( expr)
@@ -484,7 +490,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
484490 ScopeData :: IfThen
485491 } ;
486492 visitor. enter_scope ( Scope { local_id : then. hir_id . local_id , data } ) ;
487- visitor. cx . var_parent = visitor . cx . parent ;
493+ visitor. cx . set_var_parent ( ) ;
488494 visitor. visit_expr ( cond) ;
489495 visitor. visit_expr ( then) ;
490496 visitor. cx = expr_cx;
@@ -499,7 +505,7 @@ fn resolve_expr<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, expr: &'tcx hi
499505 ScopeData :: IfThen
500506 } ;
501507 visitor. enter_scope ( Scope { local_id : then. hir_id . local_id , data } ) ;
502- visitor. cx . var_parent = visitor . cx . parent ;
508+ visitor. cx . set_var_parent ( ) ;
503509 visitor. visit_expr ( cond) ;
504510 visitor. visit_expr ( then) ;
505511 visitor. cx = expr_cx;
@@ -558,7 +564,7 @@ fn resolve_local<'tcx>(
558564) {
559565 debug ! ( "resolve_local(pat={:?}, init={:?})" , pat, init) ;
560566
561- let blk_scope = visitor. cx . var_parent . map ( | ( p , _ ) | p ) ;
567+ let blk_scope = visitor. cx . var_parent ;
562568
563569 // As an exception to the normal rules governing temporary
564570 // lifetimes, initializers in a let have a temporary lifetime
@@ -849,7 +855,7 @@ impl<'tcx> Visitor<'tcx> for ScopeResolutionVisitor<'tcx> {
849855 self . enter_body ( body. value . hir_id , |this| {
850856 if this. tcx . hir_body_owner_kind ( owner_id) . is_fn_or_closure ( ) {
851857 // The arguments and `self` are parented to the fn.
852- this. cx . var_parent = this . cx . parent ;
858+ this. cx . set_var_parent ( ) ;
853859 for param in body. params {
854860 this. visit_pat ( param. pat ) ;
855861 }
0 commit comments