Skip to content

Commit 889f707

Browse files
committed
Store Place instead of PlaceBuilder in MatchPair
1 parent ae472c5 commit 889f707

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,10 @@ impl<'pat, 'tcx> TestCase<'pat, 'tcx> {
10921092
#[derive(Debug, Clone)]
10931093
pub(crate) struct MatchPair<'pat, 'tcx> {
10941094
/// This place...
1095-
place: PlaceBuilder<'tcx>,
1095+
// This can be `None` if it referred to a non-captured place in a closure.
1096+
// Invariant: place.is_none() => test_case is Irrefutable
1097+
// In other words this must be `Some(_)` after simplification.
1098+
place: Option<Place<'tcx>>,
10961099

10971100
/// ... must pass this test...
10981101
// Invariant: after creation and simplification in `Candidate::new()`, this must not be
@@ -1573,11 +1576,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15731576
fn pick_test(
15741577
&mut self,
15751578
candidates: &mut [&mut Candidate<'_, 'tcx>],
1576-
) -> (PlaceBuilder<'tcx>, Test<'tcx>) {
1579+
) -> (Place<'tcx>, Test<'tcx>) {
15771580
// Extract the match-pair from the highest priority candidate
15781581
let match_pair = &candidates.first().unwrap().match_pairs[0];
15791582
let test = self.test(match_pair);
1580-
let match_place = match_pair.place.clone();
1583+
// Unwrap is ok after simplification.
1584+
let match_place = match_pair.place.unwrap();
15811585
debug!(?test, ?match_pair);
15821586

15831587
(match_place, test)
@@ -1618,7 +1622,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16181622
/// - candidate 1 becomes `[y @ false]` since we know that `x` was `false`.
16191623
fn sort_candidates<'b, 'c, 'pat>(
16201624
&mut self,
1621-
match_place: &PlaceBuilder<'tcx>,
1625+
match_place: Place<'tcx>,
16221626
test: &Test<'tcx>,
16231627
mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>],
16241628
) -> (
@@ -1636,7 +1640,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16361640
// sorting.
16371641
while let Some(candidate) = candidates.first_mut() {
16381642
let Some(branch) =
1639-
self.sort_candidate(&match_place, test, candidate, &target_candidates)
1643+
self.sort_candidate(match_place, test, candidate, &target_candidates)
16401644
else {
16411645
break;
16421646
};
@@ -1764,7 +1768,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17641768
// For each of the N possible test outcomes, build the vector of candidates that applies if
17651769
// the test has that particular outcome.
17661770
let (remaining_candidates, target_candidates) =
1767-
self.sort_candidates(&match_place, &test, candidates);
1771+
self.sort_candidates(match_place, &test, candidates);
17681772

17691773
// The block that we should branch to if none of the
17701774
// `target_candidates` match.
@@ -1804,7 +1808,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18041808
scrutinee_span,
18051809
start_block,
18061810
remainder_start,
1807-
&match_place,
1811+
match_place,
18081812
&test,
18091813
target_blocks,
18101814
);

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// identify what tests are needed, perform the tests, and then filter
66
// the candidates based on the result.
77

8-
use crate::build::expr::as_place::PlaceBuilder;
98
use crate::build::matches::{Candidate, MatchPair, Test, TestBranch, TestCase, TestKind};
109
use crate::build::Builder;
1110
use rustc_data_structures::fx::FxIndexMap;
@@ -55,18 +54,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5554
Test { span: match_pair.pattern.span, kind }
5655
}
5756

58-
#[instrument(skip(self, target_blocks, place_builder), level = "debug")]
57+
#[instrument(skip(self, target_blocks, place), level = "debug")]
5958
pub(super) fn perform_test(
6059
&mut self,
6160
match_start_span: Span,
6261
scrutinee_span: Span,
6362
block: BasicBlock,
6463
otherwise_block: BasicBlock,
65-
place_builder: &PlaceBuilder<'tcx>,
64+
place: Place<'tcx>,
6665
test: &Test<'tcx>,
6766
target_blocks: FxIndexMap<TestBranch<'tcx>, BasicBlock>,
6867
) {
69-
let place = place_builder.to_place(self);
7068
let place_ty = place.ty(&self.local_decls, self.tcx);
7169
debug!(?place, ?place_ty);
7270
let target_block = |branch| target_blocks.get(&branch).copied().unwrap_or(otherwise_block);
@@ -475,7 +473,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
475473
/// tighter match code if we do something a bit different.
476474
pub(super) fn sort_candidate(
477475
&mut self,
478-
test_place: &PlaceBuilder<'tcx>,
476+
test_place: Place<'tcx>,
479477
test: &Test<'tcx>,
480478
candidate: &mut Candidate<'_, 'tcx>,
481479
sorted_candidates: &FxIndexMap<TestBranch<'tcx>, Vec<&mut Candidate<'_, 'tcx>>>,
@@ -486,8 +484,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
486484
// than one, but it'd be very unusual to have two sides that
487485
// both require tests; you'd expect one side to be simplified
488486
// away.)
489-
let (match_pair_index, match_pair) =
490-
candidate.match_pairs.iter().enumerate().find(|&(_, mp)| mp.place == *test_place)?;
487+
let (match_pair_index, match_pair) = candidate
488+
.match_pairs
489+
.iter()
490+
.enumerate()
491+
.find(|&(_, mp)| mp.place == Some(test_place))?;
491492

492493
let fully_matched;
493494
let ret = match (&test.kind, &match_pair.test_case) {
@@ -521,7 +522,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
521522
candidate
522523
.match_pairs
523524
.iter()
524-
.any(|mp| mp.place == *test_place && is_covering_range(&mp.test_case))
525+
.any(|mp| mp.place == Some(test_place) && is_covering_range(&mp.test_case))
525526
};
526527
if sorted_candidates
527528
.get(&TestBranch::Failure)

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
119119
place_builder = place_builder.project(ProjectionElem::OpaqueCast(pattern.ty));
120120
}
121121

122+
let place = place_builder.try_to_place(cx);
122123
let default_irrefutable = || TestCase::Irrefutable { binding: None, ascription: None };
123124
let mut subpairs = Vec::new();
124125
let test_case = match pattern.kind {
@@ -143,13 +144,13 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
143144
..
144145
} => {
145146
// Apply the type ascription to the value at `match_pair.place`
146-
let ascription = place_builder.try_to_place(cx).map(|source| super::Ascription {
147+
let ascription = place.map(|source| super::Ascription {
147148
annotation: annotation.clone(),
148149
source,
149150
variance,
150151
});
151152

152-
subpairs.push(MatchPair::new(place_builder.clone(), subpattern, cx));
153+
subpairs.push(MatchPair::new(place_builder, subpattern, cx));
153154
TestCase::Irrefutable { ascription, binding: None }
154155
}
155156

@@ -162,7 +163,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
162163
ref subpattern,
163164
is_primary: _,
164165
} => {
165-
let binding = place_builder.try_to_place(cx).map(|source| super::Binding {
166+
let binding = place.map(|source| super::Binding {
166167
span: pattern.span,
167168
source,
168169
var_id: var,
@@ -171,14 +172,14 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
171172

172173
if let Some(subpattern) = subpattern.as_ref() {
173174
// this is the `x @ P` case; have to keep matching against `P` now
174-
subpairs.push(MatchPair::new(place_builder.clone(), subpattern, cx));
175+
subpairs.push(MatchPair::new(place_builder, subpattern, cx));
175176
}
176177
TestCase::Irrefutable { ascription: None, binding }
177178
}
178179

179180
PatKind::InlineConstant { subpattern: ref pattern, def, .. } => {
180181
// Apply a type ascription for the inline constant to the value at `match_pair.place`
181-
let ascription = place_builder.try_to_place(cx).map(|source| {
182+
let ascription = place.map(|source| {
182183
let span = pattern.span;
183184
let parent_id = cx.tcx.typeck_root_def_id(cx.def_id.to_def_id());
184185
let args = ty::InlineConstArgs::new(
@@ -204,7 +205,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
204205
super::Ascription { annotation, source, variance: ty::Contravariant }
205206
});
206207

207-
subpairs.push(MatchPair::new(place_builder.clone(), pattern, cx));
208+
subpairs.push(MatchPair::new(place_builder, pattern, cx));
208209
TestCase::Irrefutable { ascription, binding: None }
209210
}
210211

@@ -226,7 +227,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
226227
}
227228

228229
PatKind::Variant { adt_def, variant_index, args, ref subpatterns } => {
229-
let downcast_place = place_builder.clone().downcast(adt_def, variant_index); // `(x as Variant)`
230+
let downcast_place = place_builder.downcast(adt_def, variant_index); // `(x as Variant)`
230231
subpairs = cx.field_match_pairs(downcast_place, subpatterns);
231232

232233
let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
@@ -248,18 +249,17 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
248249
}
249250

250251
PatKind::Leaf { ref subpatterns } => {
251-
subpairs = cx.field_match_pairs(place_builder.clone(), subpatterns);
252+
subpairs = cx.field_match_pairs(place_builder, subpatterns);
252253
default_irrefutable()
253254
}
254255

255256
PatKind::Deref { ref subpattern } => {
256-
let place_builder = place_builder.clone().deref();
257-
subpairs.push(MatchPair::new(place_builder, subpattern, cx));
257+
subpairs.push(MatchPair::new(place_builder.deref(), subpattern, cx));
258258
default_irrefutable()
259259
}
260260
};
261261

262-
MatchPair { place: place_builder, test_case, subpairs, pattern }
262+
MatchPair { place, test_case, subpairs, pattern }
263263
}
264264
}
265265

@@ -305,8 +305,8 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
305305
}
306306
} else {
307307
// Insert a Shallow borrow of any place that is switched on.
308-
if let Some(resolved_place) = match_pair.place.try_to_place(self.cx) {
309-
self.fake_borrows.insert(resolved_place);
308+
if let Some(place) = match_pair.place {
309+
self.fake_borrows.insert(place);
310310
}
311311

312312
for subpair in &match_pair.subpairs {

0 commit comments

Comments
 (0)