Skip to content

Misleading error message due to Box causing move of struct #56496

Closed

Description

@mitsuhiko

This example causes a misleading error message:

struct FooInner {
    value1: Option<String>,
    value2: Option<String>,
}

struct Foo(Box<FooInner>);

fn main() {
    let foo = Foo(Box::new(FooInner {
        value1: Some("Hello World".to_string()),
        value2: Some("Hello World".to_string()),
    }));
    let inner = foo.0;
    // uncoment this line to fix the error
    //let inner = *inner;
    let x = inner.value1;
    let y = inner.value2;
    println!("{:?}", x);
    println!("{:?}", y);
}

Error message:

error[E0382]: use of moved value: `inner`
  --> src/main.rs:16:9
   |
15 |     let x = inner.value1;
   |         - value moved here
16 |     let y = inner.value2;
   |         ^ value used here after move
   |
   = note: move occurs because `inner.value1` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait

I would have expected the compiler informing me that the entire struct was moved due to the box involved.

Activity

  1. changed the title [-]Misleading error due to Box causing move of struct[/-] [+]Misleading error message due to Box causing move of struct[/+] on Dec 4, 2018
  2. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    fixed-by-NLLBugs fixed, but only when NLL is enabled.
    on Dec 4, 2018
  3. added a commit that references this issue on Mar 11, 2019
    65cb35a
  4. added 3 commits that reference this issue on Apr 22, 2019
    bf0ea60
    c21fbfe
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-diagnosticsArea: Messages for errors, warnings, and lintsfixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions