Skip to content

Inconsistent error when using bindings after @ #120210

Closed
@Nadrieril

Description

@Nadrieril

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-bindings_after_at`#![feature(bindings_after_at)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions