Commit Graph

8 Commits

Author SHA1 Message Date
Esteban Küber
922e6bb2d4 Detect missing fields with default values and suggest ..
When a struct definition has default field values, and the use struct ctor has missing field, if all those missing fields have defaults suggest `..`:

```
error[E0063]: missing fields `field1` and `field2` in initializer of `S`
  --> $DIR/non-exhaustive-ctor.rs:16:13
   |
LL |     let _ = S { field: () };
   |             ^ missing `field1` and `field2`
   |
help: all remaining fields have defaults, use `..`
   |
LL |     let _ = S { field: (), .. };
   |                          ++++
```
2025-01-21 21:26:37 +00:00
许杰友 Jieyou Xu (Joe)
6db2d1aae5
Rollup merge of #135700 - estebank:priv-field-dfv, r=wesleywiser
Emit single privacy error for struct literal with multiple private fields and add test for `default_field_values` privacy

Add test ensuring that struct with default field values is not constructable if the fields are not accessible.

Collect all unreachable fields in a single struct literal struct and emit a single error, instead of one error per private field.

```
error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
  --> $DIR/visibility.rs:18:13
   |
LL |     let _x = Alpha {
   |              ----- in this type
LL |         beta: 0,
   |         ^^^^^^^ private field
LL |         ..
   |         ^^ field `gamma` is private
```
2025-01-20 12:37:54 +08:00
Esteban Küber
27f079ae24 Disallow A { .. } if A has no fields
```
error: `A` has no fields, `..` needs at least one default field in the struct definition
  --> $DIR/empty-struct.rs:16:17
   |
LL |     let _ = A { .. };
   |             -   ^^
   |             |
   |             this type has no fields
```
2025-01-18 21:05:09 +00:00
Esteban Küber
deef3ebaec Emit a single privacy error for multiple fields on the same struct expression
Collect all unreachable fields in a single struct literal struct and emit a single error, instead of one error per private field.

```
error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private
  --> $DIR/visibility.rs:18:13
   |
LL |     let _x = Alpha {
   |              ----- in this type
LL |         beta: 0,
   |         ^^^^^^^ private field
LL |         ..
   |         ^^ field `gamma` is private
```
2025-01-18 20:33:15 +00:00
Esteban Küber
bbcf26fc33 Add context on private fields that are not on the same line as the struct name
```
error[E0451]: field `x` of struct `S` is private
  --> $DIR/visibility.rs:24:9
   |
LL |     let a = baz::S {
   |             ------ in this type
LL |         ..
   |         ^^ field `x` is private
```
2025-01-18 01:56:22 +00:00
Esteban Küber
3e99055c40 Add test for construction of struct with private field with default value
```
error[E0451]: field `beta` of struct `Alpha` is private
  --> $DIR/visibility.rs:11:37
   |
LL |         let x = crate::foo::Alpha { .. };
   |                                     ^^ field `beta` is private
```
2025-01-18 00:30:50 +00:00
Michael Goulet
f870761cd8 Make sure to use normalized ty for unevaluated const for default struct value 2024-12-14 18:05:19 +00:00
Michael Goulet
ad30caebdd Move default-field-values tests into a subdirectory 2024-12-14 18:05:19 +00:00