Closed
Description
In the following, the first case compiles (playground) and the second errors (playground). The only difference is adding a variant to the enum.
enum NonCopyEnum1 {
Variant { copy_field: u32 },
}
fn foo1(x: NonCopyEnum1) {
match x {
y @ NonCopyEnum1::Variant { copy_field: z } => {}
}
}
enum NonCopyEnum2 {
Variant { copy_field: u32 },
None,
}
fn foo2(x: NonCopyEnum2) {
match x {
y @ NonCopyEnum2::Variant { copy_field: z } => {} // ERROR use of moved value: `x`
_ => {}
}
}
Error:
error[E0382]: use of moved value: `x`
--> src/lib.rs:9:49
|
7 | fn foo2(x: NonCopyEnum2) {
| - move occurs because `x` has type `NonCopyEnum2`, which does not implement the `Copy` trait
8 | match x {
9 | y @ NonCopyEnum2::Variant { copy_field: z } => {}
| - value moved here ^ value used here after move
|
help: borrow this binding in the pattern to avoid moving the value
|
9 | ref y @ NonCopyEnum2::Variant { copy_field: z } => {}
| +++
This is an extension to #69971. I discovered this while poking at the match lowering code. Essentially, #69971 was accidentally only fixed for the case of irrefutable patterns, since patterns that require a test go through a different code path.