Skip to content

[WIP] Structural inititalization #143625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

beepster4096
Copy link
Contributor

@beepster4096 beepster4096 commented Jul 8, 2025

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:

  1. Its type is a struct or tuple.
  2. Its type isn't a #[non_exhaustive] struct from a different crate.
  3. Its type does not have a destructor.*
  4. 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
  • 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

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 8, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

with incorrect error output though
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jul 9, 2025
@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants