Skip to content

Commit a3185a2

Browse files
authored
Unrolled build for rust-lang#127159
Rollup merge of rust-lang#127159 - Nadrieril:hide-candidate, r=matthewjasper match lowering: Hide `Candidate` from outside the lowering algorithm The internals of `Candidate` are tricky and a source of confusion. This PR makes it so we don't expose `Candidate`s outside the lowering algorithm. Now: - false edges are handled in `lower_match_tree`; - `lower_match_tree` takes a list of patterns as input; - `lower_match_tree` returns a flat datastructure that contains only the necessary information. r? ```@matthewjasper```
2 parents 71b2116 + 08bcc01 commit a3185a2

File tree

54 files changed

+971
-907
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+971
-907
lines changed

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

+279-181
Large diffs are not rendered by default.

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

+14-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::marker::PhantomData;
2-
31
use rustc_data_structures::fx::FxIndexMap;
42
use rustc_middle::mir::*;
53
use rustc_middle::ty::Ty;
@@ -18,18 +16,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1816
&mut self,
1917
from_block: BasicBlock,
2018
real_target: BasicBlock,
21-
imaginary_target: Option<BasicBlock>,
19+
imaginary_target: BasicBlock,
2220
source_info: SourceInfo,
2321
) {
24-
match imaginary_target {
25-
Some(target) if target != real_target => {
26-
self.cfg.terminate(
27-
from_block,
28-
source_info,
29-
TerminatorKind::FalseEdge { real_target, imaginary_target: target },
30-
);
31-
}
32-
_ => self.cfg.goto(from_block, source_info, real_target),
22+
if imaginary_target != real_target {
23+
self.cfg.terminate(
24+
from_block,
25+
source_info,
26+
TerminatorKind::FalseEdge { real_target, imaginary_target },
27+
);
28+
} else {
29+
self.cfg.goto(from_block, source_info, real_target)
3330
}
3431
}
3532
}
@@ -71,10 +68,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7168
/// a MIR pass run after borrow checking.
7269
pub(super) fn collect_fake_borrows<'tcx>(
7370
cx: &mut Builder<'_, 'tcx>,
74-
candidates: &[&mut Candidate<'_, 'tcx>],
71+
candidates: &[Candidate<'_, 'tcx>],
7572
temp_span: Span,
7673
scrutinee_base: PlaceBase,
7774
) -> Vec<(Place<'tcx>, Local, FakeBorrowKind)> {
75+
if candidates.iter().all(|candidate| !candidate.has_guard) {
76+
// Fake borrows are only used when there is a guard.
77+
return Vec::new();
78+
}
7879
let mut collector =
7980
FakeBorrowCollector { cx, scrutinee_base, fake_borrows: FxIndexMap::default() };
8081
for candidate in candidates.iter() {
@@ -222,57 +223,6 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
222223
}
223224
}
224225

225-
/// Visit all the bindings of these candidates. Because or-alternatives bind the same variables, we
226-
/// only explore the first one of each or-pattern.
227-
pub(super) fn visit_bindings<'tcx>(
228-
candidates: &[&mut Candidate<'_, 'tcx>],
229-
f: impl FnMut(&Binding<'tcx>),
230-
) {
231-
let mut visitor = BindingsVisitor { f, phantom: PhantomData };
232-
for candidate in candidates.iter() {
233-
visitor.visit_candidate(candidate);
234-
}
235-
}
236-
237-
pub(super) struct BindingsVisitor<'tcx, F> {
238-
f: F,
239-
phantom: PhantomData<&'tcx ()>,
240-
}
241-
242-
impl<'tcx, F> BindingsVisitor<'tcx, F>
243-
where
244-
F: FnMut(&Binding<'tcx>),
245-
{
246-
fn visit_candidate(&mut self, candidate: &Candidate<'_, 'tcx>) {
247-
for binding in &candidate.extra_data.bindings {
248-
(self.f)(binding)
249-
}
250-
for match_pair in &candidate.match_pairs {
251-
self.visit_match_pair(match_pair);
252-
}
253-
}
254-
255-
fn visit_flat_pat(&mut self, flat_pat: &FlatPat<'_, 'tcx>) {
256-
for binding in &flat_pat.extra_data.bindings {
257-
(self.f)(binding)
258-
}
259-
for match_pair in &flat_pat.match_pairs {
260-
self.visit_match_pair(match_pair);
261-
}
262-
}
263-
264-
fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'_, 'tcx>) {
265-
if let TestCase::Or { pats, .. } = &match_pair.test_case {
266-
// All the or-alternatives should bind the same locals, so we only visit the first one.
267-
self.visit_flat_pat(&pats[0])
268-
} else {
269-
for subpair in &match_pair.subpairs {
270-
self.visit_match_pair(subpair);
271-
}
272-
}
273-
}
274-
}
275-
276226
#[must_use]
277227
pub(crate) fn ref_pat_borrow_kind(ref_mutability: Mutability) -> BorrowKind {
278228
match ref_mutability {

tests/mir-opt/building/match/match_false_edges.full_tested_match.built.after.mir

+30-26
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ fn full_tested_match() -> () {
3737
}
3838

3939
bb2: {
40-
falseEdge -> [real: bb7, imaginary: bb3];
40+
falseEdge -> [real: bb8, imaginary: bb3];
4141
}
4242

4343
bb3: {
44-
falseEdge -> [real: bb12, imaginary: bb5];
44+
falseEdge -> [real: bb7, imaginary: bb5];
4545
}
4646

4747
bb4: {
@@ -50,26 +50,41 @@ fn full_tested_match() -> () {
5050

5151
bb5: {
5252
_1 = (const 3_i32, const 3_i32);
53-
goto -> bb13;
53+
goto -> bb14;
5454
}
5555

5656
bb6: {
5757
goto -> bb1;
5858
}
5959

6060
bb7: {
61+
StorageLive(_9);
62+
_9 = ((_2 as Some).0: i32);
63+
StorageLive(_10);
64+
_10 = _9;
65+
_1 = (const 2_i32, move _10);
66+
StorageDead(_10);
67+
StorageDead(_9);
68+
goto -> bb14;
69+
}
70+
71+
bb8: {
6172
StorageLive(_6);
6273
_6 = &((_2 as Some).0: i32);
6374
_3 = &fake shallow _2;
6475
StorageLive(_7);
65-
_7 = guard() -> [return: bb8, unwind: bb15];
76+
_7 = guard() -> [return: bb10, unwind: bb16];
6677
}
6778

68-
bb8: {
69-
switchInt(move _7) -> [0: bb10, otherwise: bb9];
79+
bb9: {
80+
goto -> bb3;
7081
}
7182

72-
bb9: {
83+
bb10: {
84+
switchInt(move _7) -> [0: bb12, otherwise: bb11];
85+
}
86+
87+
bb11: {
7388
StorageDead(_7);
7489
FakeRead(ForMatchGuard, _3);
7590
FakeRead(ForGuardBinding, _6);
@@ -81,44 +96,33 @@ fn full_tested_match() -> () {
8196
StorageDead(_8);
8297
StorageDead(_5);
8398
StorageDead(_6);
84-
goto -> bb13;
99+
goto -> bb14;
85100
}
86101

87-
bb10: {
88-
goto -> bb11;
102+
bb12: {
103+
goto -> bb13;
89104
}
90105

91-
bb11: {
106+
bb13: {
92107
StorageDead(_7);
93108
StorageDead(_6);
94-
goto -> bb3;
95-
}
96-
97-
bb12: {
98-
StorageLive(_9);
99-
_9 = ((_2 as Some).0: i32);
100-
StorageLive(_10);
101-
_10 = _9;
102-
_1 = (const 2_i32, move _10);
103-
StorageDead(_10);
104-
StorageDead(_9);
105-
goto -> bb13;
109+
goto -> bb9;
106110
}
107111

108-
bb13: {
112+
bb14: {
109113
PlaceMention(_1);
110114
StorageDead(_2);
111115
StorageDead(_1);
112116
_0 = const ();
113117
return;
114118
}
115119

116-
bb14: {
120+
bb15: {
117121
FakeRead(ForMatchedPlace(None), _1);
118122
unreachable;
119123
}
120124

121-
bb15 (cleanup): {
125+
bb16 (cleanup): {
122126
resume;
123127
}
124128
}

tests/mir-opt/building/match/match_false_edges.full_tested_match2.built.after.mir

+24-20
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn full_tested_match2() -> () {
3737
}
3838

3939
bb2: {
40-
falseEdge -> [real: bb7, imaginary: bb5];
40+
falseEdge -> [real: bb8, imaginary: bb5];
4141
}
4242

4343
bb3: {
@@ -48,34 +48,43 @@ fn full_tested_match2() -> () {
4848
_1 = (const 2_i32, move _10);
4949
StorageDead(_10);
5050
StorageDead(_9);
51-
goto -> bb13;
51+
goto -> bb14;
5252
}
5353

5454
bb4: {
5555
goto -> bb1;
5656
}
5757

5858
bb5: {
59-
falseEdge -> [real: bb12, imaginary: bb3];
59+
falseEdge -> [real: bb7, imaginary: bb3];
6060
}
6161

6262
bb6: {
6363
goto -> bb1;
6464
}
6565

6666
bb7: {
67+
_1 = (const 3_i32, const 3_i32);
68+
goto -> bb14;
69+
}
70+
71+
bb8: {
6772
StorageLive(_6);
6873
_6 = &((_2 as Some).0: i32);
6974
_3 = &fake shallow _2;
7075
StorageLive(_7);
71-
_7 = guard() -> [return: bb8, unwind: bb15];
76+
_7 = guard() -> [return: bb10, unwind: bb16];
7277
}
7378

74-
bb8: {
75-
switchInt(move _7) -> [0: bb10, otherwise: bb9];
79+
bb9: {
80+
falseEdge -> [real: bb3, imaginary: bb5];
7681
}
7782

78-
bb9: {
83+
bb10: {
84+
switchInt(move _7) -> [0: bb12, otherwise: bb11];
85+
}
86+
87+
bb11: {
7988
StorageDead(_7);
8089
FakeRead(ForMatchGuard, _3);
8190
FakeRead(ForGuardBinding, _6);
@@ -87,38 +96,33 @@ fn full_tested_match2() -> () {
8796
StorageDead(_8);
8897
StorageDead(_5);
8998
StorageDead(_6);
90-
goto -> bb13;
99+
goto -> bb14;
91100
}
92101

93-
bb10: {
94-
goto -> bb11;
102+
bb12: {
103+
goto -> bb13;
95104
}
96105

97-
bb11: {
106+
bb13: {
98107
StorageDead(_7);
99108
StorageDead(_6);
100-
falseEdge -> [real: bb3, imaginary: bb5];
101-
}
102-
103-
bb12: {
104-
_1 = (const 3_i32, const 3_i32);
105-
goto -> bb13;
109+
goto -> bb9;
106110
}
107111

108-
bb13: {
112+
bb14: {
109113
PlaceMention(_1);
110114
StorageDead(_2);
111115
StorageDead(_1);
112116
_0 = const ();
113117
return;
114118
}
115119

116-
bb14: {
120+
bb15: {
117121
FakeRead(ForMatchedPlace(None), _1);
118122
unreachable;
119123
}
120124

121-
bb15 (cleanup): {
125+
bb16 (cleanup): {
122126
resume;
123127
}
124128
}

0 commit comments

Comments
 (0)