-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for RFC 3681: Default field values #132162
Comments
Hi @estebank , I have a question Are we considering a syntax like the following? #[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
foo: Foo = Foo { something: 10 },
}
#[derive(Default)]
struct Foo {
something: i32 = 2,
} Something like struct inception as default or overwriting the default implementation. It's just a question, I don't have the intention to annoying, the RFC is really cool! |
@Phosphorus-M that would indeed work, |
This comment has been minimized.
This comment has been minimized.
@SUPERCILEX See this section. It will be supported once the implementation is merged. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…ive, r=<try> Lint against manual `impl Default` that could have been `derive`d ``` error: `impl Default` that could be derived --> $DIR/manual-default-impl-could-be-derived.rs:74:1 | LL | / impl Default for G { LL | | fn default() -> Self { LL | | G { LL | | f: F::Unit, LL | | } LL | | } LL | | } | |_^ | help: you don't need to manually `impl Default`, you can derive it | LL ~ #[derive(Default)] struct G { | ``` As part of rust-lang#132162/rust-lang/rfcs#3681 we want to lint when default fields values could preclude the need of a manual `impl Default`, but there are already cases where these manual impls could be derived. This PR introduces a new `default_could_be_derived` lint that makes a best effort check of the body of the `Default::default()` implementation to see if all the fields of a single expression in that body are either known to be `Default` already (like an explicit call to `Default::default()`, a `0` literal, or `Option::None` path) or are identified to be equivalent to the field's type's `Default` value (by opportunistically looking at the `Default::default()` body for that field's type).
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 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 ifa
instruct S { a: () }
is not visible?)S { .. }
whenS
has no fields with default valuesUnresolved Questions
#[non_exhaustive]
?..
covering defaulted field that otherwise is not accessible)..
on types with no default fields, particularly in unit structs?field_name: _
to specify the use of the default for that specific field?_ { .. }
)Implementation history
default_field_values
feature #129514Default
impls that could diverge against default field values: Implementdefault_could_be_derived
anddefault_overrides_default_fields
lints #134441#[non_exhaustive]
: Restrict#[non_exaustive]
on structs with default field values #134539The text was updated successfully, but these errors were encountered: