Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let chains tests #133093

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/ui/pattern/usefulness/conflicting_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fn main() {
//~^ ERROR: mutable more than once
if let Some(ref mut y @ ref mut z) = x && true {}
//~^ ERROR: mutable more than once
if let Some(_) = Some(()) && let Some(ref mut y @ ref mut z) = x && true {}
//~^ ERROR: mutable more than once
while let Some(ref mut y @ ref mut z) = x {}
//~^ ERROR: mutable more than once
while let Some(ref mut y @ ref mut z) = x && true {}
Expand Down
18 changes: 13 additions & 5 deletions tests/ui/pattern/usefulness/conflicting_bindings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,44 @@ LL | if let Some(ref mut y @ ref mut z) = x && true {}
| value is mutably borrowed by `y` here

error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:13:20
--> $DIR/conflicting_bindings.rs:13:43
|
LL | if let Some(_) = Some(()) && let Some(ref mut y @ ref mut z) = x && true {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
| |
| value is mutably borrowed by `y` here

error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:15:20
|
LL | while let Some(ref mut y @ ref mut z) = x {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
| |
| value is mutably borrowed by `y` here

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

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

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

error: aborting due to 8 previous errors
error: aborting due to 9 previous errors

11 changes: 11 additions & 0 deletions tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,15 @@ fn use_in_arm_ok(c: bool) {
};
}

fn use_in_same_chain(c: bool) {
let x: Box<_> = Box::new(1);

let v = (1, 2);

match v {
(1, 2) if let y = x && c && let z = x => false, //~ ERROR use of moved value: `x`
_ => true,
};
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ help: borrow this binding in the pattern to avoid moving the value
LL | (1, 2) if let ref y = x && c => false,
| +++

error: aborting due to 4 previous errors
error[E0382]: use of moved value: `x`
--> $DIR/move-guard-if-let-chain.rs:103:41
|
LL | let x: Box<_> = Box::new(1);
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
...
LL | (1, 2) if let y = x && c && let z = x => false,
| - ^ value used here after move
| |
| value moved here
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | (1, 2) if let ref y = x && c && let z = x => false,
| +++

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0382`.
25 changes: 25 additions & 0 deletions tests/ui/rfcs/rfc-2497-if-let-chains/temporary-early-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// issue-103476
//@ compile-flags: -Zlint-mir -Zunstable-options
//@ edition: 2024
//@ check-pass

#![feature(let_chains)]
#![allow(irrefutable_let_patterns)]

struct Pd;

impl Pd {
fn it(&self) -> It {
todo!()
}
}

pub struct It<'a>(Box<dyn Tr<'a>>);

trait Tr<'a> {}

fn f(m: Option<Pd>) {
if let Some(n) = m && let it = n.it() {};
}

fn main() {}
Loading