Skip to content
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

Introduce default_field_values feature #129514

Merged
merged 7 commits into from
Dec 10, 2024

Conversation

estebank
Copy link
Contributor

Initial implementation of #[feature(default_field_values], proposed in rust-lang/rfcs#3681.

We now parse const expressions after a = in a field definition, to specify a struct field default value.

We now allow Struct { field, .. } where there's no base after ...

#[derive(Default)] now uses the default value if present, continuing to use Default::default() if not.

#[derive(Debug)]
pub struct S;

#[derive(Debug, Default)]
pub struct Foo {
    pub bar: S = S,
    pub baz: i32 = 42 + 3,
}

fn main () {
    let x = Foo { .. };
    let y = Foo::default();
    let z = Foo { baz: 1, .. };

    assert_eq!(45, x.baz);
    assert_eq!(45, y.baz);
    assert_eq!(1, z.baz);
}

@rustbot
Copy link
Collaborator

rustbot commented Aug 24, 2024

r? @cjgillot

rustbot has assigned @cjgillot.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 24, 2024
@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor Author

It just occurred to me to try how the following fails, and it Just Works™️ 😄:

pub struct Foo {
    pub baz: i32 = Self::X,
}

impl Foo {
    const X: i32 = 1;
}

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a first pass. I don't have anything substantial to say, that's a great pr.

Just an avalanche of nits. An additional one: could you prefer matching on Rest now there are >2 cases?

compiler/rustc_ast/src/ast.rs Outdated Show resolved Hide resolved
compiler/rustc_builtin_macros/src/deriving/default.rs Outdated Show resolved Hide resolved
compiler/rustc_hir/src/intravisit.rs Outdated Show resolved Hide resolved
compiler/rustc_hir/src/hir.rs Outdated Show resolved Hide resolved
compiler/rustc_hir/src/intravisit.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/into.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/into.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 24, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment on lines 123 to 124
// We use `Default::default()`.
None => default_call(cx, field.span),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this, I filed rust-lang/rfcs#3683 as this bit is independent of the default values RFC.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@estebank estebank force-pushed the default-field-values branch 2 times, most recently from 0f49c94 to 3a64e38 Compare September 24, 2024 00:21
@rust-log-analyzer

This comment has been minimized.

@estebank estebank force-pushed the default-field-values branch 2 times, most recently from d59696d to 27549fb Compare September 24, 2024 00:53
@bors

This comment was marked as outdated.

@estebank estebank force-pushed the default-field-values branch from 27549fb to e9c76a3 Compare September 27, 2024 01:14
@bors

This comment was marked as outdated.

@compiler-errors
Copy link
Member

Could you also squash all of those line 2-line commits into the original commit?

@estebank estebank force-pushed the default-field-values branch from 8da8084 to 6335a14 Compare December 6, 2024 23:28
@estebank
Copy link
Contributor Author

estebank commented Dec 7, 2024

Could you also squash all of those line 2-line commits into the original commit?

I'll do a squash-rebase soon, once I've addressed all comments. Trying to keep things tidy enough for both review and trying to avoid accidentally squashing different things in the same commit.

Edit: squashed the commits that didn't need splitting up into their "parents". The rewordings span changes from multiple commits so I left them as is. I believe I addressed all the review comments so far. (I can push a branch with the commits before the squash rebase, if that'll be helpful.)

@estebank estebank force-pushed the default-field-values branch from e23a0cb to 66a6956 Compare December 7, 2024 17:43
@bors

This comment was marked as resolved.

@estebank estebank force-pushed the default-field-values branch from 66a6956 to e37271d Compare December 9, 2024 16:51
@@ -1104,6 +1104,23 @@ fn check_type_defn<'tcx>(
for variant in variants.iter() {
// All field types must be well-formed.
for field in &variant.fields {
if let Some(def_id) = field.value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, erroring eagerly even if we don't instantiate the default is consistent with how defaults in struct const generics works:

struct Foo<const N: u8 = {130 + 130}> {}

which errors with:

error[E0080]: evaluation of constant value failed
 --> src/lib.rs:1:27
  |
1 | struct Foo<const N: u8 = {130 + 130}> {}
  |                           ^^^^^^^^^ attempt to compute `130_u8 + 130_u8`, which would overflow

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

Not totally certain why the RFC specified that it would only be linted against, but this seems to me to be the correct behavior.

@compiler-errors
Copy link
Member

compiler-errors commented Dec 9, 2024

Please address nits and then please clean up the history a bit (that zip_eq commit could be squashed into whatever commit, and bbdd6ba too -- have you ever heard of git absorb? that might be useful to automate this type of cleanup).

Then r=me

Initial implementation of `#[feature(default_field_values]`, proposed in rust-lang/rfcs#3681.

Support default fields in enum struct variant

Allow default values in an enum struct variant definition:

```rust
pub enum Bar {
    Foo {
        bar: S = S,
        baz: i32 = 42 + 3,
    }
}
```

Allow using `..` without a base on an enum struct variant

```rust
Bar::Foo { .. }
```

`#[derive(Default)]` doesn't account for these as it is still gating `#[default]` only being allowed on unit variants.

Support `#[derive(Default)]` on enum struct variants with all defaulted fields

```rust
pub enum Bar {
    #[default]
    Foo {
        bar: S = S,
        baz: i32 = 42 + 3,
    }
}
```

Check for missing fields in typeck instead of mir_build.

Expand test with `const` param case (needs `generic_const_exprs` enabled).

Properly instantiate MIR const

The following works:

```rust
struct S<A> {
    a: Vec<A> = Vec::new(),
}
S::<i32> { .. }
```

Add lint for default fields that will always fail const-eval

We *allow* this to happen for API writers that might want to rely on users'
getting a compile error when using the default field, different to the error
that they would get when the field isn't default. We could change this to
*always* error instead of being a lint, if we wanted.

This will *not* catch errors for partially evaluated consts, like when the
expression relies on a const parameter.

Suggestions when encountering `Foo { .. }` without `#[feature(default_field_values)]`:

 - Suggest adding a base expression if there are missing fields.
 - Suggest enabling the feature if all the missing fields have optional values.
 - Suggest removing `..` if there are no missing fields.
Emit a specific error for unsupported default field value syntax in tuple structs.
People might extrapolate from `Struct { .. }` that `Struct(..)` would work, but it doesn't.
…errors

Emit E0080 always on struct definition with default fields that have unconditional const errors and remove `default_field_always_invalid_const` lint.
@estebank estebank force-pushed the default-field-values branch from e37271d to fa331f4 Compare December 9, 2024 21:58
@compiler-errors
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Dec 9, 2024

📌 Commit fa331f4 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2024
@fmease
Copy link
Member

fmease commented Dec 9, 2024

@bors p=10 bitrotty

@bors
Copy link
Contributor

bors commented Dec 10, 2024

⌛ Testing commit fa331f4 with merge 974ccc1...

@bors
Copy link
Contributor

bors commented Dec 10, 2024

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing 974ccc1 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 10, 2024
@bors bors merged commit 974ccc1 into rust-lang:master Dec 10, 2024
7 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Dec 10, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (974ccc1): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.7% [0.3%, 2.3%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.7% [-0.7%, -0.7%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -0.0%, secondary 2.4%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.3% [1.1%, 1.5%] 2
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
-1.4% [-2.2%, -0.7%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-2.2%, 1.5%] 4

Cycles

Results (secondary 2.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.1%, secondary 0.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.2%] 53
Regressions ❌
(secondary)
0.0% [0.0%, 0.1%] 13
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.0%, 0.2%] 53

Bootstrap: 767.262s -> 767.421s (0.02%)
Artifact size: 330.86 MiB -> 331.00 MiB (0.04%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-default_field_values `#![feature(default_field_values)]` merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants