Skip to content

Commit ccc3f86

Browse files
authored
Rollup merge of rust-lang#133093 - est31:let_chains_tests, r=traviscross
Let chains tests Filing this as this marks off two of the open issues in rust-lang#132833: * extending the tests for `move-guard-if-let-chain.rs` and `conflicting_bindings.rs` to have chains with multiple let's (one implementation could for example search for the first `let` and then terminate). * An instance where a temporary lives shorter than with nested ifs, breaking compilation: rust-lang#103476. This was fixed in the end by the if let rescoping work. Closes rust-lang#103476
2 parents 0b157e8 + f502dce commit ccc3f86

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

tests/ui/pattern/usefulness/conflicting_bindings.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fn main() {
1010
//~^ ERROR: mutable more than once
1111
if let Some(ref mut y @ ref mut z) = x && true {}
1212
//~^ ERROR: mutable more than once
13+
if let Some(_) = Some(()) && let Some(ref mut y @ ref mut z) = x && true {}
14+
//~^ ERROR: mutable more than once
1315
while let Some(ref mut y @ ref mut z) = x {}
1416
//~^ ERROR: mutable more than once
1517
while let Some(ref mut y @ ref mut z) = x && true {}

tests/ui/pattern/usefulness/conflicting_bindings.stderr

+13-5
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,44 @@ LL | if let Some(ref mut y @ ref mut z) = x && true {}
3131
| value is mutably borrowed by `y` here
3232

3333
error: cannot borrow value as mutable more than once at a time
34-
--> $DIR/conflicting_bindings.rs:13:20
34+
--> $DIR/conflicting_bindings.rs:13:43
35+
|
36+
LL | if let Some(_) = Some(()) && let Some(ref mut y @ ref mut z) = x && true {}
37+
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
38+
| |
39+
| value is mutably borrowed by `y` here
40+
41+
error: cannot borrow value as mutable more than once at a time
42+
--> $DIR/conflicting_bindings.rs:15:20
3543
|
3644
LL | while let Some(ref mut y @ ref mut z) = x {}
3745
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
3846
| |
3947
| value is mutably borrowed by `y` here
4048

4149
error: cannot borrow value as mutable more than once at a time
42-
--> $DIR/conflicting_bindings.rs:15:20
50+
--> $DIR/conflicting_bindings.rs:17:20
4351
|
4452
LL | while let Some(ref mut y @ ref mut z) = x && true {}
4553
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
4654
| |
4755
| value is mutably borrowed by `y` here
4856

4957
error: cannot borrow value as mutable more than once at a time
50-
--> $DIR/conflicting_bindings.rs:18:9
58+
--> $DIR/conflicting_bindings.rs:20:9
5159
|
5260
LL | ref mut y @ ref mut z => {}
5361
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
5462
| |
5563
| value is mutably borrowed by `y` here
5664

5765
error: cannot borrow value as mutable more than once at a time
58-
--> $DIR/conflicting_bindings.rs:21:24
66+
--> $DIR/conflicting_bindings.rs:23:24
5967
|
6068
LL | () if let Some(ref mut y @ ref mut z) = x => {}
6169
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
6270
| |
6371
| value is mutably borrowed by `y` here
6472

65-
error: aborting due to 8 previous errors
73+
error: aborting due to 9 previous errors
6674

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.rs

+11
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,15 @@ fn use_in_arm_ok(c: bool) {
9494
};
9595
}
9696

97+
fn use_in_same_chain(c: bool) {
98+
let x: Box<_> = Box::new(1);
99+
100+
let v = (1, 2);
101+
102+
match v {
103+
(1, 2) if let y = x && c && let z = x => false, //~ ERROR use of moved value: `x`
104+
_ => true,
105+
};
106+
}
107+
97108
fn main() {}

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ help: borrow this binding in the pattern to avoid moving the value
6060
LL | (1, 2) if let ref y = x && c => false,
6161
| +++
6262

63-
error: aborting due to 4 previous errors
63+
error[E0382]: use of moved value: `x`
64+
--> $DIR/move-guard-if-let-chain.rs:103:41
65+
|
66+
LL | let x: Box<_> = Box::new(1);
67+
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
68+
...
69+
LL | (1, 2) if let y = x && c && let z = x => false,
70+
| - ^ value used here after move
71+
| |
72+
| value moved here
73+
|
74+
help: borrow this binding in the pattern to avoid moving the value
75+
|
76+
LL | (1, 2) if let ref y = x && c && let z = x => false,
77+
| +++
78+
79+
error: aborting due to 5 previous errors
6480

6581
For more information about this error, try `rustc --explain E0382`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// issue-103476
2+
//@ compile-flags: -Zlint-mir -Zunstable-options
3+
//@ edition: 2024
4+
//@ check-pass
5+
6+
#![feature(let_chains)]
7+
#![allow(irrefutable_let_patterns)]
8+
9+
struct Pd;
10+
11+
impl Pd {
12+
fn it(&self) -> It {
13+
todo!()
14+
}
15+
}
16+
17+
pub struct It<'a>(Box<dyn Tr<'a>>);
18+
19+
trait Tr<'a> {}
20+
21+
fn f(m: Option<Pd>) {
22+
if let Some(n) = m && let it = n.it() {};
23+
}
24+
25+
fn main() {}

0 commit comments

Comments
 (0)