-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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 ifainstruct S { a: () }is not visible?) - Restrict
S { .. }whenShas no fields with default values - Lint against explicit
impl Defaultwhen#[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.Made mutually exclusive.#[non_exhaustive]?Customization of behavior wrt. visibility rules (allow user to specify that value can be constructed withFollowing RFC: struct can't be constructed with..covering defaulted field that otherwise is not accessible)..if it covers a private field.AllowingDisallowed, can be added later if we find a reason to support that...on types with no default fields, particularly in unit structs?*Allowing the use ofLet's not try that, particularly in the face offield_name: _to specify the use of the default for that specific field?*~const Defaultallowing forfield_name: Default::default()andfield_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 associatedconstand usefield_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
- Initial implementation:
Introducedefault_field_valuesfeature #129514 - Minor test add:
Exercise const trait interaction with default fields #134136 - Make sure to use normalized ty for unevaluated const in default struct value [ICE fix]:
Make sure to use normalized ty for unevaluated const in default struct value #134314 - Lints for
Defaultimpls that could diverge against default field values:
Implementdefault_could_be_derivedanddefault_overrides_default_fieldslints #134441- Only lint for "manual
impl Defaultwithout using..for all default fields":
Implementdefault_overrides_default_fieldslint #134737 - 1st Experiment:
Lint against manualimpl Defaultthat could have beenderived #134175 - Reuse logic from Implement
default_could_be_derivedanddefault_overrides_default_fieldslints #134441 that looks at theDefault::default()body in existingderivable_implsclippy lint:
Use MIR body to identify more "default equivalent" calls forderivable_implsrust-clippy#13988 - Lint against
Bar { .. }.foo.x != Foo { .. }.x:
Detect when a field default is not using that field's type's default values #135859
Closed, should likely live in clippy as allow-by-default. There's no analogue lint for the same case forDefault, so either we should have both or neither.
- Only lint for "manual
- Restrict
#[non_exhaustive]:
Restrict#[non_exaustive]on structs with default field values #134539 - Add documentation entry to unstable book:
Adddefault_field_valuesentry to unstable book #134855 - Constify
Default::default()so that it can be used in default field values:
MakeDefaultconst and add someconst Defaultimpls #134628 - Propose rustfmt style:
Proposed formatting for default field values style-team#205 - Add test for privacy rules:
Emit single privacy error for struct literal with multiple private fields and add test fordefault_field_valuesprivacy #135700 - Add test for use of
#[const_trait] Default:
Add test fordefault_field_valuesandconst_default#143649 - Disallow
A { .. }ifAhas no fields:
DisallowA { .. }ifAhas no fields #135703 - Fix lifetime checking ICE:
Do not ICE on default_field_value const with lifetimes #135711 - Detect APIs with non-exhaustive struct constructors forcing
A { field, .. }always: - Support parsing in
syn: Support parsing#![feature(default_field_values)]dtolnay/syn#1851