Skip to content

Commit 557c8c4

Browse files
committed
Tweak binding collection
1 parent dc3c094 commit 557c8c4

File tree

1 file changed

+19
-24
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+19
-24
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
474474
// candidate.
475475
self.bind_and_guard_matched_candidate(
476476
candidate,
477-
&[],
477+
&mut vec![],
478478
fake_borrow_temps,
479479
scrutinee_span,
480480
arm_match_scope,
@@ -505,13 +505,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
505505
traverse_candidate(
506506
candidate,
507507
&mut Vec::new(),
508-
&mut |leaf_candidate, parent_data| {
508+
&mut |leaf_candidate, collected_data| {
509509
if let Some(arm) = arm {
510510
self.clear_top_scope(arm.scope);
511511
}
512512
let binding_end = self.bind_and_guard_matched_candidate(
513513
leaf_candidate,
514-
parent_data,
514+
collected_data,
515515
fake_borrow_temps,
516516
scrutinee_span,
517517
arm_match_scope,
@@ -523,12 +523,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
523523
}
524524
self.cfg.goto(binding_end, outer_source_info, target_block);
525525
},
526-
|inner_candidate, parent_data| {
527-
parent_data.push(inner_candidate.extra_data);
526+
|inner_candidate, collected_data| {
527+
collected_data.push(inner_candidate.extra_data);
528528
inner_candidate.subcandidates.into_iter()
529529
},
530-
|parent_data| {
531-
parent_data.pop();
530+
|collected_data| {
531+
collected_data.pop();
532532
},
533533
);
534534

@@ -929,7 +929,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
929929

930930
/// Data extracted from a pattern that doesn't affect which branch is taken. Collected during
931931
/// pattern simplification and not mutated later.
932-
#[derive(Debug, Clone)]
932+
#[derive(Debug, Clone, Default)]
933933
struct PatternExtraData<'tcx> {
934934
/// [`Span`] of the original pattern.
935935
span: Span,
@@ -1958,7 +1958,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19581958
fn bind_and_guard_matched_candidate<'pat>(
19591959
&mut self,
19601960
candidate: Candidate<'pat, 'tcx>,
1961-
parent_data: &[PatternExtraData<'tcx>],
1961+
// Manage in a stack fashion.
1962+
collected_data: &mut Vec<PatternExtraData<'tcx>>,
19621963
fake_borrows: &[(Place<'tcx>, Local)],
19631964
scrutinee_span: Span,
19641965
arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
@@ -1970,6 +1971,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19701971
debug_assert!(candidate.match_pairs.is_empty());
19711972

19721973
let candidate_source_info = self.source_info(candidate.extra_data.span);
1974+
collected_data.push(candidate.extra_data);
19731975

19741976
let mut block = candidate.pre_binding_block.unwrap();
19751977

@@ -1984,14 +1986,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19841986
block = fresh_block;
19851987
}
19861988

1987-
self.ascribe_types(
1988-
block,
1989-
parent_data
1990-
.iter()
1991-
.flat_map(|d| &d.ascriptions)
1992-
.cloned()
1993-
.chain(candidate.extra_data.ascriptions),
1994-
);
1989+
self.ascribe_types(block, collected_data.iter().flat_map(|d| &d.ascriptions).cloned());
19951990

19961991
// rust-lang/rust#27282: The `autoref` business deserves some
19971992
// explanation here.
@@ -2074,12 +2069,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20742069
// the reference that we create for the arm.
20752070
// * So we eagerly create the reference for the arm and then take a
20762071
// reference to that.
2077-
if let Some((arm, match_scope)) = arm_match_scope
2072+
let block = if let Some((arm, match_scope)) = arm_match_scope
20782073
&& let Some(guard) = arm.guard
20792074
{
20802075
let tcx = self.tcx;
2081-
let bindings =
2082-
parent_data.iter().flat_map(|d| &d.bindings).chain(&candidate.extra_data.bindings);
2076+
let bindings = collected_data.iter().flat_map(|d| &d.bindings);
20832077

20842078
self.bind_matched_candidate_for_guard(block, schedule_drops, bindings.clone());
20852079
let guard_frame = GuardFrame {
@@ -2157,10 +2151,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
21572151
// ```
21582152
//
21592153
// and that is clearly not correct.
2160-
let by_value_bindings = parent_data
2154+
let by_value_bindings = collected_data
21612155
.iter()
21622156
.flat_map(|d| &d.bindings)
2163-
.chain(&candidate.extra_data.bindings)
21642157
.filter(|binding| matches!(binding.binding_mode, BindingMode::ByValue));
21652158
// Read all of the by reference bindings to ensure that the
21662159
// place they refer to can't be modified by the guard.
@@ -2185,11 +2178,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
21852178
self.bind_matched_candidate_for_arm_body(
21862179
block,
21872180
schedule_drops,
2188-
parent_data.iter().flat_map(|d| &d.bindings).chain(&candidate.extra_data.bindings),
2181+
collected_data.iter().flat_map(|d| &d.bindings),
21892182
storages_alive,
21902183
);
21912184
block
2192-
}
2185+
};
2186+
collected_data.pop();
2187+
block
21932188
}
21942189

21952190
/// Append `AscribeUserType` statements onto the end of `block`

0 commit comments

Comments
 (0)