Skip to content

Commit 33c9023

Browse files
authored
Rollup merge of #138959 - meithecatte:matchpair-place-option, r=Zalathar
Revert "Make MatchPairTree::place non-optional" Reverts a part of #137875. Fixes #138958. cc `@Zalathar`
2 parents 19a53b7 + 1524060 commit 33c9023

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ impl<'tcx> MatchPairTree<'tcx> {
115115
place_builder = place_builder.project(ProjectionElem::OpaqueCast(pattern.ty));
116116
}
117117

118-
// Place can be none if the pattern refers to a non-captured place in a closure.
119118
let place = place_builder.try_to_place(cx);
120119
let mut subpairs = Vec::new();
121120
let test_case = match pattern.kind {
@@ -321,7 +320,7 @@ impl<'tcx> MatchPairTree<'tcx> {
321320
if let Some(test_case) = test_case {
322321
// This pattern is refutable, so push a new match-pair node.
323322
match_pairs.push(MatchPairTree {
324-
place: place.expect("refutable patterns should always have a place to inspect"),
323+
place,
325324
test_case,
326325
subpairs,
327326
pattern_ty: pattern.ty,

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,13 @@ impl<'tcx> TestCase<'tcx> {
12791279
#[derive(Debug, Clone)]
12801280
pub(crate) struct MatchPairTree<'tcx> {
12811281
/// This place...
1282-
place: Place<'tcx>,
1282+
///
1283+
/// ---
1284+
/// This can be `None` if it referred to a non-captured place in a closure.
1285+
///
1286+
/// Invariant: Can only be `None` when `test_case` is `Or`.
1287+
/// Therefore this must be `Some(_)` after or-pattern expansion.
1288+
place: Option<Place<'tcx>>,
12831289

12841290
/// ... must pass this test...
12851291
test_case: TestCase<'tcx>,
@@ -2099,9 +2105,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20992105
// Extract the match-pair from the highest priority candidate
21002106
let match_pair = &candidates[0].match_pairs[0];
21012107
let test = self.pick_test_for_match_pair(match_pair);
2108+
// Unwrap is ok after simplification.
2109+
let match_place = match_pair.place.unwrap();
21022110
debug!(?test, ?match_pair);
21032111

2104-
(match_pair.place, test)
2112+
(match_place, test)
21052113
}
21062114

21072115
/// Given a test, we partition the input candidates into several buckets.

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
470470
// than one, but it'd be very unusual to have two sides that
471471
// both require tests; you'd expect one side to be simplified
472472
// away.)
473-
let (match_pair_index, match_pair) =
474-
candidate.match_pairs.iter().enumerate().find(|&(_, mp)| mp.place == test_place)?;
473+
let (match_pair_index, match_pair) = candidate
474+
.match_pairs
475+
.iter()
476+
.enumerate()
477+
.find(|&(_, mp)| mp.place == Some(test_place))?;
475478

476479
// If true, the match pair is completely entailed by its corresponding test
477480
// branch, so it can be removed. If false, the match pair is _compatible_
@@ -514,7 +517,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
514517
candidate
515518
.match_pairs
516519
.iter()
517-
.any(|mp| mp.place == test_place && is_covering_range(&mp.test_case))
520+
.any(|mp| mp.place == Some(test_place) && is_covering_range(&mp.test_case))
518521
};
519522
if sorted_candidates
520523
.get(&TestBranch::Failure)

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,14 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
173173
// }
174174
// ```
175175
// Hence we fake borrow using a deep borrow.
176-
self.fake_borrow(match_pair.place, FakeBorrowKind::Deep);
176+
if let Some(place) = match_pair.place {
177+
self.fake_borrow(place, FakeBorrowKind::Deep);
178+
}
177179
} else {
178180
// Insert a Shallow borrow of any place that is switched on.
179-
self.fake_borrow(match_pair.place, FakeBorrowKind::Shallow);
181+
if let Some(place) = match_pair.place {
182+
self.fake_borrow(place, FakeBorrowKind::Shallow);
183+
}
180184

181185
for subpair in &match_pair.subpairs {
182186
self.visit_match_pair(subpair);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ edition:2024
2+
//@ check-pass
3+
4+
pub fn f(x: (u32, u32)) {
5+
let _ = || {
6+
let ((0, a) | (a, _)) = x;
7+
a
8+
};
9+
}
10+
11+
fn main() {}

0 commit comments

Comments
 (0)