Open
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
}
}