@@ -40,6 +40,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4040 temp_scope_override : Option < region:: Scope > ,
4141 break_scope : region:: Scope ,
4242 variable_source_info : SourceInfo ,
43+ declare_bindings : bool ,
4344 ) -> BlockAnd < ( ) > {
4445 let this = self ;
4546 let expr = & this. thir [ expr_id] ;
@@ -53,6 +54,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5354 temp_scope_override,
5455 break_scope,
5556 variable_source_info,
57+ declare_bindings,
5658 ) ) ;
5759
5860 let rhs_then_block = unpack ! ( this. then_else_break(
@@ -61,6 +63,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6163 temp_scope_override,
6264 break_scope,
6365 variable_source_info,
66+ declare_bindings,
6467 ) ) ;
6568
6669 rhs_then_block. unit ( )
@@ -75,6 +78,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7578 temp_scope_override,
7679 local_scope,
7780 variable_source_info,
81+ true ,
7882 )
7983 } ) ;
8084 let rhs_success_block = unpack ! ( this. then_else_break(
@@ -83,6 +87,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8387 temp_scope_override,
8488 break_scope,
8589 variable_source_info,
90+ true ,
8691 ) ) ;
8792 this. cfg . goto ( lhs_success_block, variable_source_info, rhs_success_block) ;
8893 rhs_success_block. unit ( )
@@ -102,6 +107,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
102107 temp_scope_override,
103108 local_scope,
104109 variable_source_info,
110+ true ,
105111 )
106112 } ) ;
107113 this. break_for_else ( success_block, break_scope, variable_source_info) ;
@@ -116,6 +122,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
116122 temp_scope_override,
117123 break_scope,
118124 variable_source_info,
125+ declare_bindings,
119126 )
120127 } )
121128 }
@@ -125,6 +132,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
125132 temp_scope_override,
126133 break_scope,
127134 variable_source_info,
135+ declare_bindings,
128136 ) ,
129137 ExprKind :: Let { expr, ref pat } => this. lower_let_expr (
130138 block,
@@ -133,7 +141,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
133141 break_scope,
134142 Some ( variable_source_info. scope ) ,
135143 variable_source_info. span ,
136- true ,
144+ declare_bindings ,
137145 ) ,
138146 _ => {
139147 let temp_scope = temp_scope_override. unwrap_or_else ( || this. local_scope ( ) ) ;
@@ -737,13 +745,40 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
737745 ) ;
738746 } ,
739747 ) ;
740- if let Some ( Guard :: IfLet ( guard_pat, _) ) = guard {
741- // FIXME: pass a proper `opt_match_place`
742- self . declare_bindings ( visibility_scope, scope_span, guard_pat, None , None ) ;
748+ if let Some ( & Guard :: If ( guard_expr) ) = guard {
749+ self . declare_guard_bindings ( guard_expr, scope_span, visibility_scope) ;
743750 }
744751 visibility_scope
745752 }
746753
754+ /// Declare bindings in a guard. This has to be done when declaring bindings
755+ /// for an arm to ensure that or patterns only have one version of each
756+ /// variable.
757+ pub ( crate ) fn declare_guard_bindings (
758+ & mut self ,
759+ guard_expr : ExprId ,
760+ scope_span : Span ,
761+ visibility_scope : Option < SourceScope > ,
762+ ) {
763+ match self . thir . exprs [ guard_expr] . kind {
764+ ExprKind :: Let { expr : _, pat : ref guard_pat } => {
765+ // FIXME: pass a proper `opt_match_place`
766+ self . declare_bindings ( visibility_scope, scope_span, guard_pat, None , None ) ;
767+ }
768+ ExprKind :: Scope { value, .. } => {
769+ self . declare_guard_bindings ( value, scope_span, visibility_scope) ;
770+ }
771+ ExprKind :: Use { source } => {
772+ self . declare_guard_bindings ( source, scope_span, visibility_scope) ;
773+ }
774+ ExprKind :: LogicalOp { op : LogicalOp :: And , lhs, rhs } => {
775+ self . declare_guard_bindings ( lhs, scope_span, visibility_scope) ;
776+ self . declare_guard_bindings ( rhs, scope_span, visibility_scope) ;
777+ }
778+ _ => { }
779+ }
780+ }
781+
747782 pub ( crate ) fn storage_live_binding (
748783 & mut self ,
749784 block : BasicBlock ,
@@ -2043,6 +2078,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20432078 None ,
20442079 match_scope,
20452080 this. source_info ( arm. span ) ,
2081+ false ,
20462082 )
20472083 }
20482084 Guard :: IfLet ( ref pat, s) => {
0 commit comments