Fix missing terminator for slice pattern

This commit is contained in:
hkalbasi 2023-06-04 20:59:27 +03:30
parent 0408af6453
commit b4907a531f
2 changed files with 70 additions and 45 deletions

View file

@ -206,6 +206,8 @@ impl MirLowerCtx<'_> {
(current, current_else)
}
Pat::Slice { prefix, slice, suffix } => {
if mode == MatchingMode::Check {
// emit runtime length check for slice
if let TyKind::Slice(_) = self.infer[pattern].kind(Interner) {
let pattern_len = prefix.len() + suffix.len();
let place_len: Place =
@ -216,7 +218,8 @@ impl MirLowerCtx<'_> {
Rvalue::Len((&mut cond_place).clone()),
pattern.into(),
);
let else_target = *current_else.get_or_insert_with(|| self.new_basic_block());
let else_target =
*current_else.get_or_insert_with(|| self.new_basic_block());
let next = self.new_basic_block();
if slice.is_none() {
self.set_terminator(
@ -257,6 +260,7 @@ impl MirLowerCtx<'_> {
}
current = next;
}
}
for (i, &pat) in prefix.iter().enumerate() {
let next_place = (&mut cond_place).project(ProjectionElem::ConstantIndex {
offset: i as u64,

View file

@ -993,6 +993,27 @@ fn f() {
);
}
#[test]
fn slice_pattern() {
check_diagnostics(
r#"
//- minicore: coerce_unsized, deref_mut, slice, copy
fn x(t: &[u8]) {
match t {
&[a, mut b] | &[a, _, mut b] => {
//^^^^^ 💡 weak: variable does not need to be mutable
a = 2;
//^^^^^ 💡 error: cannot mutate immutable variable `a`
}
_ => {}
}
}
"#,
);
}
#[test]
fn boxes() {
check_diagnostics(