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

E0507 E0009 should be mutually exclusive #64135

Closed
Wolvereness opened this issue Sep 3, 2019 · 2 comments
Closed

E0507 E0009 should be mutually exclusive #64135

Wolvereness opened this issue Sep 3, 2019 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Wolvereness
Copy link

E0507 E0009 should be mutually exclusive

E0009

cannot bind by-move and by-ref in the same pattern

E0507

cannot move out of borrowed content

Example

Playground

fn main() {
    Holder {
        copy_type: 0,
        almost_copy: AlmostCopy(42),
        never_copy: "Hello world".to_string(),
    }.func()
}

#[derive(Debug)]
struct AlmostCopy(usize);

#[derive(Debug)]
struct Holder {
    copy_type: usize,
    almost_copy: AlmostCopy,
    never_copy: String,
}

impl Holder {
    fn func(&self) {
        let &Holder {
            copy_type,
            almost_copy,
            ref never_copy,
        } = self;
        println!(
            "{}: {:?} -- {:?}", 
            copy_type, 
            almost_copy, 
            never_copy,
        );
    }
}

Output

   Compiling playground v0.0.1 (/playground)
error[E0009]: cannot bind by-move and by-ref in the same pattern
  --> src/main.rs:24:13
   |
24 |             almost_copy,
   |             ^^^^^^^^^^^ by-move pattern here
25 |             ref never_copy,
   |             -------------- both by-ref and by-move used

error[E0507]: cannot move out of `self.almost_copy` which is behind a shared reference
  --> src/main.rs:26:13
   |
24 |             almost_copy,
   |             -----------
   |             |
   |             data moved here
   |             move occurs because `almost_copy` has type `AlmostCopy`, which does not implement the `Copy` trait
25 |             ref never_copy,
26 |         } = self;
   |             ^^^^
help: consider removing the `&`
   |
22 |         let Holder {
23 |             copy_type,
24 |             almost_copy,
25 |             ref never_copy,
26 |         } = self;
   |

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0009, E0507.
For more information about an error, try `rustc --explain E0009`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Reasoning

A situation where both trigger, E0507 is a much more clear indication of something wrong in the code. In this example, the intention is that AlmostCopy implements Copy, and will subsequently trigger neither error. The presence of E0009 is misleading when it doesn't include any mention of Copy without the click-through.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 3, 2019
@estebank
Copy link
Contributor

estebank commented Sep 3, 2019

CC #45600, which if it were fixed would also close this ticket.

Centril added a commit to Centril/rust that referenced this issue Sep 16, 2019
On obligation errors point at the unfulfilled binding when possible

CC rust-lang#42855, rust-lang#64130, rust-lang#64135.
Centril added a commit to Centril/rust that referenced this issue Sep 19, 2019
On obligation errors point at the unfulfilled binding when possible

CC rust-lang#42855, rust-lang#64130, rust-lang#64135.
bors added a commit that referenced this issue Sep 22, 2019
On obligation errors point at the unfulfilled binding when possible

CC #42855, #64130, #64135. Fix #61860.
@Centril
Copy link
Contributor

Centril commented Feb 17, 2020

This is now resolved when you add #![feature(move_ref_pattern)] as you only get E0507.
cc #68354.

@Centril Centril closed this as completed Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

3 participants