-
Notifications
You must be signed in to change notification settings - Fork 13k
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).
…able-docs, r=jieyouxu Add `default_field_values` entry to unstable book Tracking issue: rust-lang#132162 RFC: https://github.com/rust-lang/rfcs/blob/master/text/3681-default-field-values.md
…able-docs, r=jieyouxu Add `default_field_values` entry to unstable book Tracking issue: rust-lang#132162 RFC: https://github.com/rust-lang/rfcs/blob/master/text/3681-default-field-values.md
Rollup merge of rust-lang#134855 - estebank:default-field-values-unstable-docs, r=jieyouxu Add `default_field_values` entry to unstable book Tracking issue: rust-lang#132162 RFC: https://github.com/rust-lang/rfcs/blob/master/text/3681-default-field-values.md
…, r=jieyouxu Add `default_field_values` entry to unstable book Tracking issue: rust-lang/rust#132162 RFC: https://github.com/rust-lang/rfcs/blob/master/text/3681-default-field-values.md
@Phosphorus-M I created #135859 to lint against the case you presented earlier. Doing that by accident can lead to confusion, but making it a lint allows for APIs that need it to still be able to enable it and do it. Edit: I was convinced that the lint shouldn't live in rustc nor be warn-by-default. We should reimplement it in clippy under the pedantic family, as well as figure out how to detect the same situation when it comes to manual |
Previously, we needed to specify the default values in multiple places: - the two `default{,_compact}` constructors - in the binary, as `default_*_name` - also in the binary, as the default values for flags This commit removes all those and uses `None`s in there, instead adding methods called `DisplaySettings::<field_name>_or_default` that return `Some` values if they exist, or the _actual_ defaults in case of `None`. There was another approach that came to mind, but was seen as too cumbersome: make a newtype for each such field, and implement `Default` for them -- this'd requier too much boilerplate and could require exposing the newtypes in a lot of places (most importantly, in the binary). Ideally, of course, we'd use default field values, but those aren't stabilzed yet.[^1] This also gets rid of the TODO in `mergiraf solve`, which was addressed back in #74 [^1]: rust-lang/rust#132162 Reviewed-on: https://codeberg.org/mergiraf/mergiraf/pulls/161 Reviewed-by: Antonin Delpeuch <wetneb@noreply.codeberg.org> Co-authored-by: Ada Alakbarova <ada.alakbarova@proton.me> Co-committed-by: Ada Alakbarova <ada.alakbarova@proton.me>
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 valuesimpl Default
when#[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 Default
allowing 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 associatedconst
and 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_values
feature #129514Exercise const trait interaction with default fields #134136
Make sure to use normalized ty for unevaluated const in default struct value #134314
Default
impls that could diverge against default field values:Implement
default_could_be_derived
anddefault_overrides_default_fields
lints #134441impl Default
without using..
for all default fields":Implement
default_overrides_default_fields
lint #134737Lint against manual
impl Default
that could have beenderive
d #134175default_could_be_derived
anddefault_overrides_default_fields
lints #134441 that looks at theDefault::default()
body in existingderivable_impls
clippy lint:Use MIR body to identify more "default equivalent" calls for
derivable_impls
rust-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_values
entry to unstable book #134855Default::default()
so that it can be used in default field values:Make
Default
const and add someconst Default
impls #134628Proposed formatting for default field values style-team#205
Emit single privacy error for struct literal with multiple private fields and add test for
default_field_values
privacy #135700A { .. }
ifA
has no fields:Disallow
A { .. }
ifA
has no fields #135703Do not ICE on default_field_value const with lifetimes #135711
A { field, .. }
always:..
#135794The text was updated successfully, but these errors were encountered: