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

Attempting destructuring of struct reference to mutable variable results in partial moves of fields #102669

Closed
cdmistman opened this issue Oct 4, 2022 · 7 comments
Labels
A-patterns Relating to patterns and pattern matching C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cdmistman
Copy link

I tried this code:

struct Foo {
    bar: Bar,
}

struct Bar;

fn with_bar(bar: &Bar) {}

fn main() {
    let foo = Foo { bar: Bar };
    let Foo { mut bar } = &foo;
    with_bar(bar);
}

I expected to see this happen: The above should compile with bar being a mutable variable of type &Bar

Instead, this happened: The above fails to compile with bar being a mutable variable of type Bar.

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (015a824f2 2022-08-22)
binary: rustc
commit-hash: 015a824f2dffe32707fceb59c47effaf7b73486c
commit-date: 2022-08-22
host: aarch64-apple-darwin
release: 1.65.0-nightly
LLVM version: 15.0.0
Backtrace

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/main.rs:12:14
   |
12 |     with_bar(bar);
   |     -------- ^^^
   |     |        |
   |     |        expected `&Bar`, found struct `Bar`
   |     |        help: consider borrowing here: `&bar`
   |     arguments to this function are incorrect
   |
note: function defined here
  --> src/main.rs:7:4
   |
7  | fn with_bar(bar: &Bar) {}
   |    ^^^^^^^^ ---------

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

playground

@cdmistman cdmistman added the C-bug Category: This is a bug. label Oct 4, 2022
@cdmistman cdmistman changed the title Attempting destructuring of struct reference results in partial moves of fields Attempting destructuring of struct reference to mutable variable results in partial moves of fields Oct 4, 2022
@Enselic
Copy link
Member

Enselic commented Oct 4, 2024

Triage: Addressed by rust-lang/rfcs#3627 (see various referenced issues). Closing as duplicate.

@Enselic Enselic closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2024
@compiler-errors
Copy link
Member

I don't think this issue should be closed until the referenced RFC is stabilized. This issue still reproduces on all version of Rust, afaict?

@Enselic Enselic added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-patterns Relating to patterns and pattern matching A-edition-2024 Area: The 2024 edition labels Oct 4, 2024
@Enselic
Copy link
Member

Enselic commented Oct 4, 2024

Hmm, you are right.

We should probably create a label to make it easy to track bugs related to the implementation.

@Enselic Enselic reopened this Oct 4, 2024
@compiler-errors
Copy link
Member

it's not a bug, it's just not stable behavior yet. It's gated behind a feature.

@traviscross traviscross removed the A-edition-2024 Area: The 2024 edition label Oct 8, 2024
@traviscross
Copy link
Contributor

@Nadrieril
Copy link
Member

This is a duplicate of #105647 though

@traviscross
Copy link
Contributor

@traviscross traviscross closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-patterns Relating to patterns and pattern matching C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants