Skip to content

Unnecessary 'cannot bind by-move with sub-bindings' with bindings_after_at #69971

Description

@comex

The following code (playground link) attempts to move a struct while using a sub-binding to copy one of its fields:

#![feature(bindings_after_at)]

struct NonCopyStruct {
    copy_field: u32,
}

fn foo(x: NonCopyStruct) {
    let y @ NonCopyStruct { copy_field: z } = x;
}

On nightly, it produces two errors:

error[E0007]: cannot bind by-move with sub-bindings
 --> src/lib.rs:8:9
  |
8 |     let y @ NonCopyStruct { copy_field: z } = x;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ binds an already bound by-move value by moving it
error[E0382]: use of moved value: `x`
 --> src/lib.rs:8:41
  |
7 | fn foo(x: NonCopyStruct) {
  |        - move occurs because `x` has type `NonCopyStruct`, which does not implement the `Copy` trait
8 |     let y @ NonCopyStruct { copy_field: z } = x;
  |         --------------------------------^--
  |         |                               |
  |         |                               value used here after move

However, it would be sound to allow this, since it's equivalent to copying the field before or after moving the struct.

(There are no errors if the struct does implement Copy.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-borrow-checkerArea: The borrow checkerC-feature-requestCategory: A feature request, i.e: not implemented / a PR.F-bindings_after_at`#![feature(bindings_after_at)]`T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions