Fix drop scopes in mir

This commit is contained in:
hkalbasi 2023-06-04 01:03:32 +03:30
parent f44fc271d4
commit 08f89193b5
5 changed files with 237 additions and 24 deletions

View file

@ -512,6 +512,38 @@ fn main() {
f(x);
}
}
"#,
);
check_diagnostics(
r#"
fn check(_: i32) -> bool {
false
}
fn main() {
loop {
let x = 1;
if check(x) {
break;
}
let y = (1, 2);
if check(y.1) {
return;
}
let z = (1, 2);
match z {
(k @ 5, ref mut t) if { continue; } => {
//^^^^^^^^^ 💡 error: cannot mutate immutable variable `z`
*t = 5;
}
_ => {
let y = (1, 2);
if check(y.1) {
return;
}
}
}
}
}
"#,
);
check_diagnostics(
@ -592,7 +624,7 @@ fn f((x, y): (i32, i32)) {
fn for_loop() {
check_diagnostics(
r#"
//- minicore: iterators
//- minicore: iterators, copy
fn f(x: [(i32, u8); 10]) {
for (a, mut b) in x {
//^^^^^ 💡 weak: variable does not need to be mutable
@ -604,6 +636,28 @@ fn f(x: [(i32, u8); 10]) {
);
}
#[test]
fn while_let() {
check_diagnostics(
r#"
//- minicore: iterators, copy
fn f(x: [(i32, u8); 10]) {
let mut it = x.into_iter();
while let Some((a, mut b)) = it.next() {
//^^^^^ 💡 weak: variable does not need to be mutable
while let Some((c, mut d)) = it.next() {
//^^^^^ 💡 weak: variable does not need to be mutable
a = 2;
//^^^^^ 💡 error: cannot mutate immutable variable `a`
c = 2;
//^^^^^ 💡 error: cannot mutate immutable variable `c`
}
}
}
"#,
);
}
#[test]
fn index() {
check_diagnostics(