Skip to content

Commit 4119d74

Browse files
committed
Use let-chains for determining length in prefix_slice_suffix
1 parent 9a1d156 commit 4119d74

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

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

+18-12
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4040
suffix: &'pat [Box<Pat<'tcx>>],
4141
) {
4242
let tcx = self.tcx;
43-
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
44-
match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
45-
ty::Array(_, length) => (
46-
length
47-
.try_to_target_usize(tcx)
48-
.expect("expected len of array pat to be definite"),
49-
true,
50-
),
51-
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
52-
}
43+
44+
// Minimum length of the sequence being matched, needed by projections.
45+
// If `exact_size` is true, that "minimum" is actually an exact length.
46+
//
47+
// The caller of `prefix_slice_suffix` is responsible for actually
48+
// enforcing this, e.g. by checking the length of the scrutinee slice.
49+
let min_length: u64;
50+
let exact_size: bool;
51+
52+
if let Some(place_resolved) = place.try_to_place(self)
53+
&& let ty::Array(_, length) = place_resolved.ty(&self.local_decls, tcx).ty.kind()
54+
{
55+
min_length =
56+
length.try_to_target_usize(tcx).expect("expected len of array pat to be definite");
57+
exact_size = true;
5358
} else {
54-
((prefix.len() + suffix.len()).try_into().unwrap(), false)
55-
};
59+
min_length = u64::try_from(prefix.len() + suffix.len()).unwrap();
60+
exact_size = false;
61+
}
5662

5763
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
5864
let elem =

0 commit comments

Comments
 (0)