Skip to content

Commit 849e036

Browse files
committed
Remove 'pat lifetime from some match-lowering data structures
By storing `PatRange` in an Arc, and copying a few fields out of `Pat`, we can greatly simplify the lifetimes involved in match lowering.
1 parent ee17c3b commit 849e036

File tree

8 files changed

+107
-99
lines changed

8 files changed

+107
-99
lines changed

Diff for: compiler/rustc_middle/src/thir.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::cmp::Ordering;
1212
use std::fmt;
1313
use std::ops::Index;
14+
use std::sync::Arc;
1415

1516
use rustc_abi::{FieldIdx, Integer, Size, VariantIdx};
1617
use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece};
@@ -846,7 +847,7 @@ pub enum PatKind<'tcx> {
846847
subpattern: Box<Pat<'tcx>>,
847848
},
848849

849-
Range(Box<PatRange<'tcx>>),
850+
Range(Arc<PatRange<'tcx>>),
850851

851852
/// Matches against a slice, checking the length and extracting elements.
852853
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.

Diff for: compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use rustc_middle::mir::*;
24
use rustc_middle::thir::{self, *};
35
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
@@ -12,11 +14,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1214
/// [`PatKind::Leaf`].
1315
///
1416
/// Used internally by [`MatchPairTree::for_pattern`].
15-
fn field_match_pairs<'pat>(
17+
fn field_match_pairs(
1618
&mut self,
1719
place: PlaceBuilder<'tcx>,
18-
subpatterns: &'pat [FieldPat<'tcx>],
19-
) -> Vec<MatchPairTree<'pat, 'tcx>> {
20+
subpatterns: &[FieldPat<'tcx>],
21+
) -> Vec<MatchPairTree<'tcx>> {
2022
subpatterns
2123
.iter()
2224
.map(|fieldpat| {
@@ -31,13 +33,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3133
/// array pattern or slice pattern, and adds those trees to `match_pairs`.
3234
///
3335
/// Used internally by [`MatchPairTree::for_pattern`].
34-
fn prefix_slice_suffix<'pat>(
36+
fn prefix_slice_suffix(
3537
&mut self,
36-
match_pairs: &mut Vec<MatchPairTree<'pat, 'tcx>>,
38+
match_pairs: &mut Vec<MatchPairTree<'tcx>>,
3739
place: &PlaceBuilder<'tcx>,
38-
prefix: &'pat [Box<Pat<'tcx>>],
39-
opt_slice: &'pat Option<Box<Pat<'tcx>>>,
40-
suffix: &'pat [Box<Pat<'tcx>>],
40+
prefix: &[Box<Pat<'tcx>>],
41+
opt_slice: &Option<Box<Pat<'tcx>>>,
42+
suffix: &[Box<Pat<'tcx>>],
4143
) {
4244
let tcx = self.tcx;
4345
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
@@ -83,14 +85,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8385
}
8486
}
8587

86-
impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
88+
impl<'tcx> MatchPairTree<'tcx> {
8789
/// Recursively builds a match pair tree for the given pattern and its
8890
/// subpatterns.
8991
pub(in crate::builder) fn for_pattern(
9092
mut place_builder: PlaceBuilder<'tcx>,
91-
pattern: &'pat Pat<'tcx>,
93+
pattern: &Pat<'tcx>,
9294
cx: &mut Builder<'_, 'tcx>,
93-
) -> MatchPairTree<'pat, 'tcx> {
95+
) -> MatchPairTree<'tcx> {
9496
// Force the place type to the pattern's type.
9597
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
9698
if let Some(resolved) = place_builder.resolve_upvar(cx) {
@@ -125,7 +127,7 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
125127
if range.is_full_range(cx.tcx) == Some(true) {
126128
default_irrefutable()
127129
} else {
128-
TestCase::Range(range)
130+
TestCase::Range(Arc::clone(range))
129131
}
130132
}
131133

@@ -255,6 +257,12 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
255257
PatKind::Never => TestCase::Never,
256258
};
257259

258-
MatchPairTree { place, test_case, subpairs, pattern }
260+
MatchPairTree {
261+
place,
262+
test_case,
263+
subpairs,
264+
pattern_ty: pattern.ty,
265+
pattern_span: pattern.span,
266+
}
259267
}
260268
}

0 commit comments

Comments
 (0)