Skip to content

Tracking issue for RFC 3681: Default field values #132162

Open
@estebank

Description

This is a tracking issue for the RFC "3681" (rust-lang/rfcs#3681).
The feature gate for the issue is #![feature(default_field_values)].

Allow struct definitions to provide default values for individual fields and
thereby allowing those to be omitted from initializers. When deriving Default,
the provided values will then be used. For example:

#[derive(Default)]
struct Pet {
    name: Option<String>, // impl Default for Pet will use Default::default() for name
    age: i128 = 42, // impl Default for Pet will use the literal 42 for age
}

let valid = Pet { name: None, .. };
assert_eq!(valid.age, 42);
let default = Pet::default();
assert_eq!(default.age, 42);

let invalid = Pet { .. };

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.

Steps

  • Implement the RFC (cc @rust-lang/compiler)
    • Parse the new syntax
    • Support #[derive(Default)] expansion of structs with default field values
    • Support #[derive(Default)] expansion of struct variants where every field has a default
    • Restrict #[non_exhaustive] on items with default field values
    • Restrict defaults on tuple struct and tuple variants
    • Define visibility of defaulted field values on value construction (S { .. } is allowed if a in struct S { a: () } is not visible?)
    • Restrict S { .. } when S has no fields with default values
    • Lint against explicit impl Default when #[derive(Default)] would be ok
      • Additional lints for iffy cases (maybe belongs in clippy?)
  • Adjust documentation (see instructions on rustc-dev-guide)
    • Add unstable book entry
    • Add to the Reference
    • Mention in The Book
    • Add example to Rust By Example
  • Formatting for new syntax has been added to the Style Guide (nightly-style-procedure)
  • Stabilization PR (see instructions on rustc-dev-guide)

Unresolved Questions

  • What is the right interaction wrt. #[non_exhaustive]? Made mutually exclusive.
  • Customization of behavior wrt. visibility rules (allow user to specify that value can be constructed with .. covering defaulted field that otherwise is not accessible) Following RFC: struct can't be constructed with .. if it covers a private field.
  • Allowing .. on types with no default fields, particularly in unit structs?* Disallowed, can be added later if we find a reason to support that.
  • Allowing the use of field_name: _ to specify the use of the default for that specific field?* Let's not try that, particularly in the face of ~const Default allowing for field_name: Default::default() and field_name: default()
  • Tuple structs and tuple variant support* Let file a subsequent RFC for this.
  • Integration with (potential, at this time) structural records*
  • Integration with (potential, at this time) literal type inference (_ { .. })*
  • Exposing default field values as individual consts in the syntax? (I lean towards "unneeded") If an API needs the default to be expressed, it can be an associated const and use field_name: Self::CONST_DEFAULT, making it accessible
  • Expand support to non-const values? (Personal position is that we shouldn't do that, particularly seeing how powerful const eval is becoming.)

* Decision not needed for stabilization of this feature, can be follow up work.

Implementation history

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-default_field_values`#![feature(default_field_values)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions