rust/compiler/rustc_ast/src
Esteban Küber 9ac95c10c0 Introduce default_field_values feature
Initial implementation of `#[feature(default_field_values]`, proposed in https://github.com/rust-lang/rfcs/pull/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.
2024-12-09 21:55:01 +00:00
..
attr Change AttrArgs::Eq into a struct variant 2024-12-02 10:28:58 +00:00
expand Single commit implementing the enzyme/autodiff frontend 2024-10-11 19:13:31 +02:00
util Eliminate magic numbers from expression precedence 2024-11-30 17:53:40 -08:00
ast_traits.rs Remove needless returns detected by clippy in the compiler 2024-09-09 13:32:22 +02:00
ast.rs Introduce default_field_values feature 2024-12-09 21:55:01 +00:00
entry.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
format.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
lib.rs Add warn(unreachable_pub) to several crates. 2024-08-16 08:46:13 +10:00
mut_visit.rs Introduce default_field_values feature 2024-12-09 21:55:01 +00:00
node_id.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
ptr.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
token.rs Prepare for invisible delimiters. 2024-11-21 08:22:11 +11:00
tokenstream.rs Introduce InvisibleOrigin on invisible delimiters. 2024-11-21 08:16:54 +11:00
visit.rs Introduce default_field_values feature 2024-12-09 21:55:01 +00:00