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

Detect missing fields with default values and suggest .. #135794

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

estebank
Copy link
Contributor

When a struct ctor use has missing fields, 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 default values, you can use those values with `..`
   |
LL |     let _ = S { field: (), .. };
   |                          ++++

@rustbot
Copy link
Collaborator

rustbot commented Jan 20, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
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. labels Jan 20, 2025
Comment on lines 13 to 23
error[E0603]: unit struct `Priv` is private
--> $DIR/non-exhaustive-ctor.rs:23:39
|
LL | let _ = S { field: (), field1: m::Priv, field2: m::Priv };
| ^^^^ private unit struct
|
note: the unit struct `Priv` is defined here
--> $DIR/non-exhaustive-ctor.rs:10:5
|
LL | struct Priv;
| ^^^^^^^^^^^^
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case is not relevant to this PR, but I'm looking on resolve to make it also suggest .. if Priv is private but field1 is public.

This will allow API designs where the construction of a public struct will always need .., protecting against future API breakage (in the same way that non_exhaustive is used in patterns).

@jieyouxu jieyouxu assigned jieyouxu and unassigned fee1-dead Jan 21, 2025
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks, I left some feedback

Comment on lines 2437 to 2439
} else {
"if your crate were nightly-only, you could add \
`#![feature(default_field_values)]` to your crate and"
},
Copy link
Member

Choose a reason for hiding this comment

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

Discussion: we probably don't want to make the suggestion to go from a crate compilable on stable -> need nightly for this feature?

@@ -0,0 +1,26 @@
#![feature(default_field_values)]
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: since the suggestions are machine-applicable, could you add //@ run-rustfix or //@ rustfix-only-machine-applicable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an intermediary step, I wanted to deal with the private types cases which will likely not be machine applicable, but I can split the tests instead and mark this one.

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, thats fine

@@ -0,0 +1,26 @@
#![feature(default_field_values)]
Copy link
Member

Choose a reason for hiding this comment

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

Discussion: also note that this test does not snapshot the other suggestion where we are nightly-only but do not have the feature gate. Maybe revision this test, but I don't think it matters that much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add that.

@jieyouxu jieyouxu added A-diagnostics Area: Messages for errors, warnings, and lints F-default_field_values `#![feature(default_field_values)]` labels Jan 21, 2025
@jieyouxu
Copy link
Member

@rustbot author

@rustbot rustbot 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 Jan 21, 2025
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: (), .. };
   |                          ++++
```
@estebank estebank force-pushed the non-exhaustive-dfv-ctor branch from 86e6877 to 922e6bb Compare January 21, 2025 21:26
@estebank
Copy link
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 21, 2025
@estebank
Copy link
Contributor Author

Split off the parts of the test meant for privacy check into its own file (and own PR: #135846).

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks!

@jieyouxu
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 22, 2025

📌 Commit 922e6bb has been approved by jieyouxu

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 Jan 22, 2025
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jan 22, 2025
…r=jieyouxu

Detect missing fields with default values and suggest `..`

When a struct ctor use has missing fields, 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 default values, you can use those values with `..`
   |
LL |     let _ = S { field: (), .. };
   |                          ++++
```
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 22, 2025
Rollup of 8 pull requests

Successful merges:

 - rust-lang#135779 (CI: free disk on linux arm runner)
 - rust-lang#135794 (Detect missing fields with default values and suggest `..`)
 - rust-lang#135814 (ci: use ghcr buildkit image)
 - rust-lang#135818 (tests: Port `translation` to rmake.rs)
 - rust-lang#135823 (make UI tests that use `--test` work on panic=abort targets)
 - rust-lang#135837 (Remove test panic from File::open)
 - rust-lang#135852 (Add `AsyncFn*` to `core` prelude)
 - rust-lang#135856 (Library: Finalize dyn compatibility renaming)

r? `@ghost`
`@rustbot` modify labels: rollup
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jan 22, 2025
…r=jieyouxu

Detect missing fields with default values and suggest `..`

When a struct ctor use has missing fields, 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 default values, you can use those values with `..`
   |
LL |     let _ = S { field: (), .. };
   |                          ++++
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-default_field_values `#![feature(default_field_values)]` 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants