Skip to content

"borrow of possibly uninitialized variable" instead of "use of moved value" #52669

Closed
@stephaneyfx

Description

@stephaneyfx

NLL combined with a struct field assignment gives an error mentioning the use/borrow of an uninitialized variable instead of the use/borrow of a moved variable.

Reproduction example

Simplified code based on @Yatekii's question on IRC
Playground

#![feature(nll)]

struct A {
    b: B,
}

#[derive(Clone)]
struct B;

fn foo(_: A) {}

fn bar(mut a: A) -> B {
    // Commenting the line below gives a move error instead of uninitialized
    // error.
    a.b = B;
    foo(a);
    a.b.clone()
}

fn main() {}

Expected behavior

The compiler gives an error about using a moved value, similar to the following:

Without NLL:

error[E0382]: use of moved value: `a.b`
  --> src/main.rs:17:5
   |
16 |     foo(a);
   |         - value moved here
17 |     a.b.clone()
   |     ^^^ value used here after move
   |
   = note: move occurs because `a` has type `A`, which does not implement the `Copy` trait

With NLL but without the field assignment:

error[E0382]: borrow of moved value: `a.b`
  --> src/main.rs:17:5
   |
16 |     foo(a);
   |         - value moved here
17 |     a.b.clone()
   |     ^^^ value borrowed here after move
   |
   = note: move occurs because `a` has type `A`, which does not implement the `Copy` trait

Actual behavior

The error mentions an uninitialized value instead of a moved value:

error[E0381]: borrow of possibly uninitialized variable: `a.b`
  --> src/main.rs:17:5
   |
17 |     a.b.clone()
   |     ^^^ use of possibly uninitialized `a.b`

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.NLL-diagnosticsWorking towards the "diagnostic parity" goal

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions