@@ -1199,6 +1199,17 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
11991199 |_| { } ,
12001200 ) ;
12011201 }
1202+
1203+ /// Visit the leaf candidates in reverse order.
1204+ fn visit_leaves_rev < ' a > ( & ' a mut self , mut visit_leaf : impl FnMut ( & ' a mut Self ) ) {
1205+ traverse_candidate (
1206+ self ,
1207+ & mut ( ) ,
1208+ & mut move |c, _| visit_leaf ( c) ,
1209+ move |c, _| c. subcandidates . iter_mut ( ) . rev ( ) ,
1210+ |_| { } ,
1211+ ) ;
1212+ }
12021213}
12031214
12041215/// A depth-first traversal of the `Candidate` and all of its recursive
@@ -1433,23 +1444,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
14331444 let otherwise_block =
14341445 self . match_candidates ( match_start_span, scrutinee_span, block, candidates) ;
14351446
1436- // Link each leaf candidate to the `false_edge_start_block` of the next one.
1437- let mut previous_candidate: Option < & mut Candidate < ' _ , ' _ > > = None ;
1438- for candidate in candidates {
1439- candidate. visit_leaves ( |leaf_candidate| {
1440- if let Some ( ref mut prev) = previous_candidate {
1441- assert ! ( leaf_candidate. false_edge_start_block. is_some( ) ) ;
1442- prev. next_candidate_start_block = leaf_candidate. false_edge_start_block ;
1443- }
1444- previous_candidate = Some ( leaf_candidate) ;
1447+ // Link each leaf candidate to the `false_edge_start_block` of the next one. In the
1448+ // refutable case we also want a false edge to the failure block.
1449+ let mut next_candidate_start_block = if refutable { Some ( otherwise_block) } else { None } ;
1450+ for candidate in candidates. iter_mut ( ) . rev ( ) {
1451+ candidate. visit_leaves_rev ( |leaf_candidate| {
1452+ leaf_candidate. next_candidate_start_block = next_candidate_start_block;
1453+ assert ! ( leaf_candidate. false_edge_start_block. is_some( ) ) ;
1454+ next_candidate_start_block = leaf_candidate. false_edge_start_block ;
14451455 } ) ;
14461456 }
14471457
1448- if refutable {
1449- // In refutable cases there's always at least one candidate, and we want a false edge to
1450- // the failure block.
1451- previous_candidate. as_mut ( ) . unwrap ( ) . next_candidate_start_block = Some ( otherwise_block)
1452- } else {
1458+ if !refutable {
14531459 // Match checking ensures `otherwise_block` is actually unreachable in irrefutable
14541460 // cases.
14551461 let source_info = self . source_info ( scrutinee_span) ;
0 commit comments