@@ -3181,11 +3181,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3181
3181
self . resolve_pattern_top ( & local. pat , PatternSource :: Let ) ;
3182
3182
}
3183
3183
3184
- /// build a map from pattern identifiers to binding-info's.
3185
- /// this is done hygienically. This could arise for a macro
3186
- /// that expands into an or-pattern where one 'x' was from the
3187
- /// user and one 'x' came from the macro.
3188
- fn binding_mode_map ( & mut self , pat : & Pat ) -> FxIndexMap < Ident , BindingInfo > {
3184
+ /// Build a map from pattern identifiers to binding-info's, and check the bindings are
3185
+ /// consistent when encountering or-patterns.
3186
+ /// This is done hygienically: this could arise for a macro that expands into an or-pattern
3187
+ /// where one 'x' was from the user and one 'x' came from the macro.
3188
+ fn compute_and_check_binding_map ( & mut self , pat : & Pat ) -> FxIndexMap < Ident , BindingInfo > {
3189
3189
let mut binding_map = FxIndexMap :: default ( ) ;
3190
3190
3191
3191
pat. walk ( & mut |pat| {
@@ -3198,9 +3198,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3198
3198
PatKind :: Or ( ref ps) => {
3199
3199
// Check the consistency of this or-pattern and
3200
3200
// then add all bindings to the larger map.
3201
- for bm in self . check_consistent_bindings ( ps) {
3202
- binding_map. extend ( bm) ;
3203
- }
3201
+ let bm = self . compute_and_check_or_pat_binding_map ( ps) ;
3202
+ binding_map. extend ( bm) ;
3204
3203
return false ;
3205
3204
}
3206
3205
_ => { }
@@ -3219,18 +3218,19 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3219
3218
)
3220
3219
}
3221
3220
3222
- /// Checks that all of the arms in an or-pattern have exactly the
3223
- /// same set of bindings, with the same binding modes for each.
3224
- fn check_consistent_bindings (
3221
+ /// Compute the binding map for an or-pattern. Checks that all of the arms in the or-pattern
3222
+ /// have exactly the same set of bindings, with the same binding modes for each.
3223
+ /// Returns the computed binding map.
3224
+ fn compute_and_check_or_pat_binding_map (
3225
3225
& mut self ,
3226
3226
pats : & [ P < Pat > ] ,
3227
- ) -> Vec < FxIndexMap < Ident , BindingInfo > > {
3228
- // pats is consistent.
3227
+ ) -> FxIndexMap < Ident , BindingInfo > {
3229
3228
let mut missing_vars = FxIndexMap :: default ( ) ;
3230
3229
let mut inconsistent_vars = FxIndexMap :: default ( ) ;
3231
3230
3232
3231
// 1) Compute the binding maps of all arms.
3233
- let maps = pats. iter ( ) . map ( |pat| self . binding_mode_map ( pat) ) . collect :: < Vec < _ > > ( ) ;
3232
+ let maps =
3233
+ pats. iter ( ) . map ( |pat| self . compute_and_check_binding_map ( pat) ) . collect :: < Vec < _ > > ( ) ;
3234
3234
3235
3235
// 2) Record any missing bindings or binding mode inconsistencies.
3236
3236
for ( map_outer, pat_outer) in maps. iter ( ) . zip ( pats. iter ( ) ) {
@@ -3239,13 +3239,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3239
3239
. iter ( )
3240
3240
. zip ( pats. iter ( ) )
3241
3241
. filter ( |( _, pat) | pat. id != pat_outer. id )
3242
- . flat_map ( |( map, _) | map)
3243
- . map ( |( key, binding) | ( key. name , map_outer. get ( key) , binding) ) ;
3244
-
3245
- let inners = inners. collect :: < Vec < _ > > ( ) ;
3242
+ . flat_map ( |( map, _) | map) ;
3246
3243
3247
- for ( name, info, & binding_inner) in inners {
3248
- match info {
3244
+ for ( key, binding_inner) in inners {
3245
+ let name = key. name ;
3246
+ match map_outer. get ( key) {
3249
3247
None => {
3250
3248
// The inner binding is missing in the outer.
3251
3249
let binding_error =
@@ -3286,15 +3284,19 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3286
3284
self . report_error ( v. 0 , ResolutionError :: VariableBoundWithDifferentMode ( name, v. 1 ) ) ;
3287
3285
}
3288
3286
3289
- // 5) Finally bubble up all the binding maps.
3290
- maps
3287
+ // 5) Bubble up the final binding map.
3288
+ let mut binding_map = FxIndexMap :: default ( ) ;
3289
+ for bm in maps {
3290
+ binding_map. extend ( bm) ;
3291
+ }
3292
+ binding_map
3291
3293
}
3292
3294
3293
- /// Check the consistency of the outermost or-patterns.
3294
- fn check_consistent_bindings_top ( & mut self , pat : & ' ast Pat ) {
3295
+ /// Check the consistency of bindings wrt or-patterns.
3296
+ fn check_consistent_bindings ( & mut self , pat : & ' ast Pat ) {
3295
3297
pat. walk ( & mut |pat| match pat. kind {
3296
3298
PatKind :: Or ( ref ps) => {
3297
- self . check_consistent_bindings ( ps) ;
3299
+ let _ = self . compute_and_check_or_pat_binding_map ( ps) ;
3298
3300
false
3299
3301
}
3300
3302
_ => true ,
@@ -3327,7 +3329,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3327
3329
visit:: walk_pat ( self , pat) ;
3328
3330
self . resolve_pattern_inner ( pat, pat_src, bindings) ;
3329
3331
// This has to happen *after* we determine which pat_idents are variants:
3330
- self . check_consistent_bindings_top ( pat) ;
3332
+ self . check_consistent_bindings ( pat) ;
3331
3333
}
3332
3334
3333
3335
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
0 commit comments