Skip to content

Track initialization and moves in the borrow checker #296

@nikomatsakis

Description

@nikomatsakis

Updates #254. Related to #209.

The borrow checker currently does not track whether variables are initialized or have been moved. This means programs that use uninitialized variables or use variables after they've been moved are accepted when they should be rejected.

For example, this should be an error but currently passes:

fn foo() -> u32 {
    let x: u32;
    return x;  // x was never initialized
}

And this should also be an error:

fn foo() -> u32 {
    let x: u32 = 0_u32;
    let y: u32 = x;   // x moved here
    let z: u32 = x;   // use of moved value
    return z;
}

Ignored tests (#[ignore] in src/test/borrowck.rs):

  • use_of_uninitialized_variable — read from a never-initialized variable
  • use_of_moved_variable — read from a variable after it was moved
  • conditional_init_one_branch — variable initialized in only one if-branch, used after
  • assign_field_of_uninitialized — assign to a field of an uninitialized struct (rustc rejects this)
  • partial_move_use_whole — use whole struct after one field was moved
  • move_same_field_twice — move the same field twice
  • move_whole_then_access_field — access a field after the whole struct was moved
  • move_parent_then_access_child — access x.foo.bar after x.foo was moved
  • move_in_loop — move inside a loop body (visible on second iteration)
  • uninitialized_return — return an uninitialized value (Extend type/borrow-check to guarantee values are returned #209)

Passing tests (happy paths that should continue to pass):

  • reinit_after_move — assignment after move re-initializes
  • conditional_init_both_branches — both if-branches initialize
  • partial_move_use_sibling — sibling field still accessible after partial move

Metadata

Metadata

Assignees

Labels

good next projectMedium complexity — good after a first issue

Type

No type
No fields configured for issues without a type.

Projects

Status

Needs triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions