-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Tracking issue for RFC 3681: Default field values #132162
Copy link
Copy link
Open
1 / 11 of 1 issue completedLabels
B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.Blocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCF-default_field_values`#![feature(default_field_values)]``#![feature(default_field_values)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Metadata
Metadata
Assignees
Labels
B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.Blocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCF-default_field_values`#![feature(default_field_values)]``#![feature(default_field_values)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Type
Fields
Give feedbackNo fields configured for issues without a type.
View all comments
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
structdefinitions to provide default values for individual fields andthereby allowing those to be omitted from initializers. When deriving
Default,the provided values will then be used. For example:
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
#[derive(Default)]expansion of structs with default field values#[derive(Default)]expansion of struct variants where every field has a default#[non_exhaustive]on items with default field valuesS { .. }is allowed ifainstruct S { a: () }is not visible?)S { .. }whenShas no fields with default valuesimpl Defaultwhen#[derive(Default)]would be okUnresolved 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._ { .. })*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* Decision not needed for stabilization of this feature, can be follow up work.
Implementation history
Introduce
default_field_valuesfeature #129514Exercise const trait interaction with default fields #134136
Make sure to use normalized ty for unevaluated const in default struct value #134314
Defaultimpls that could diverge against default field values:Implement
default_could_be_derivedanddefault_overrides_default_fieldslints #134441impl Defaultwithout using..for all default fields":Implement
default_overrides_default_fieldslint #134737Lint against manual
impl Defaultthat could have beenderived #134175default_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 for
derivable_implsrust-clippy#13988Bar { .. }.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 for
Default, so either we should have both or neither.#[non_exhaustive]:Restrict
#[non_exaustive]on structs with default field values #134539Add
default_field_valuesentry to unstable book #134855Default::default()so that it can be used in default field values:Make
Defaultconst and add someconst Defaultimpls #134628Proposed formatting for default field values style-team#205
Add style guide for default field values #149423
Emit single privacy error for struct literal with multiple private fields and add test for
default_field_valuesprivacy #135700#[const_trait] Default:Add test for
default_field_valuesandconst_default#143649A { .. }ifAhas no fields:Disallow
A { .. }ifAhas no fields #135703Do not ICE on default_field_value const with lifetimes #135711
A { field, .. }always:..#135794syn: Support parsing#![feature(default_field_values)]dtolnay/syn#1851Type::Verbatimto parse: Parse default field values asType::Verbatimdtolnay/syn#1870