[WIP] Structural inititalization by beepster4096 · Pull Request #143625 · rust-lang/rust (original) (raw)
Adds the structural_init feature, allowing structs and tuples to be initialized field by field.
struct S { a: i32, b: i32, }
let s: S; s.a = 1; s.b = -1; some_function(s);
This is typically called "partial initialization" but I have named this process "structural initialization" as I feel that "partial" does not make sense when you are initializing the entire struct this way. Obviously bikeshedable, since I just came up with this on my own without any consensus.
These are the semantics I have implemented:
A place is structurally initialized when all of the following are true:
- Its type is a struct or tuple.
- Its type isn't a
#[non_exhaustive]struct from a different crate.- Its type does not have a destructor.
- All of its fields are initialized.
A structurally initialized place is initialized even if the place itself hasn't been directly assigned to.
Types that do not have destructors may have their fields assigned to, even when uninitialized.
These semantics have the strange effect that struct Empty; and () are always considered initialized. Its trivial to disable this, it just needs a decision at some point.
WIP:
- Tests
- the
ui\nll\issue-21232-**tests already exist and should be modified
- the
- Probably slow from not caching
- Needs unstable book page & tracking issue
- Probably needs diagnostics changes
- Needs at least an FCP, probably an RFC
- What's up with unsafe binders? Does that matter?
- Empty structs and tuples?
cc #54987