mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 05:53:45 +00:00
Fix missing terminator for slice pattern
This commit is contained in:
parent
0408af6453
commit
b4907a531f
2 changed files with 70 additions and 45 deletions
|
@ -206,6 +206,8 @@ impl MirLowerCtx<'_> {
|
||||||
(current, current_else)
|
(current, current_else)
|
||||||
}
|
}
|
||||||
Pat::Slice { prefix, slice, suffix } => {
|
Pat::Slice { prefix, slice, suffix } => {
|
||||||
|
if mode == MatchingMode::Check {
|
||||||
|
// emit runtime length check for slice
|
||||||
if let TyKind::Slice(_) = self.infer[pattern].kind(Interner) {
|
if let TyKind::Slice(_) = self.infer[pattern].kind(Interner) {
|
||||||
let pattern_len = prefix.len() + suffix.len();
|
let pattern_len = prefix.len() + suffix.len();
|
||||||
let place_len: Place =
|
let place_len: Place =
|
||||||
|
@ -216,7 +218,8 @@ impl MirLowerCtx<'_> {
|
||||||
Rvalue::Len((&mut cond_place).clone()),
|
Rvalue::Len((&mut cond_place).clone()),
|
||||||
pattern.into(),
|
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();
|
let next = self.new_basic_block();
|
||||||
if slice.is_none() {
|
if slice.is_none() {
|
||||||
self.set_terminator(
|
self.set_terminator(
|
||||||
|
@ -257,6 +260,7 @@ impl MirLowerCtx<'_> {
|
||||||
}
|
}
|
||||||
current = next;
|
current = next;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for (i, &pat) in prefix.iter().enumerate() {
|
for (i, &pat) in prefix.iter().enumerate() {
|
||||||
let next_place = (&mut cond_place).project(ProjectionElem::ConstantIndex {
|
let next_place = (&mut cond_place).project(ProjectionElem::ConstantIndex {
|
||||||
offset: i as u64,
|
offset: i as u64,
|
||||||
|
|
|
@ -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]
|
#[test]
|
||||||
fn boxes() {
|
fn boxes() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
|
|
Loading…
Reference in a new issue