rust-lang / rust-lang/rfcs

partially initialized structs in local variables, similar to partially moved structs, perhaps should be possible

Open
#448 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Here are two examples where currently a dummy value for a struct field is needed, but the compiler could allow it to be safely left uninitialized by noticing that it's always initialized before being used externally. This is quite analogous to partially moved structs, where the compiler currently allows moving out fields as long as you put them back before using the struct externally (even if the latter is in a loop, as in the second example); but in this case the struct would start in a "partially moved" state.

Even though I think this is a fairly unusual use case (at least, as long as it doesn't allow calling methods in the partially uninitialized state), the added semantic complexity is pretty minimal, so I think it's worth considering.

use std::cell::RefCell;
struct Foo<'a> {
    a: RefCell<int>,
    b: &'a RefCell<int>,
}
struct Bar {
    a: int,
    // assume some other stuff
}
fn main() {
    // Case 1: assigning a reference to point to the struct itself (somewhat
    // limited since the struct can't be moved, but potentially useful)
    let mut foo = Foo {
        a: RefCell::new(1),
        b: /* dummy */ &RefCell::new(1),
    };
    foo.b = &foo.a;

    // Case 2: assigning inside a loop
    let mut bar = Bar {
        /* dummy */ a: 0,
    };
    for &a in [1, 2, 7].iter() {
        bar.a = a;
        // call some method on bar
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the two examples in the issue and the discussion history, since no implementation files or tests are named. Determine the language rules needed for partially initialized local structs and how safety is preserved before proposing an RFC-level design; done means the behavior and its restrictions are specified clearly enough for compiler work.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.