Skip to content

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

Closed
@mitsuhiko

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.

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