-
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
Detect missing fields with default values and suggest ..
#135794
base: master
Are you sure you want to change the base?
Conversation
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
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; | ||
| ^^^^^^^^^^^^ |
There was a problem hiding this comment.
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).
There was a problem hiding this 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
} else { | ||
"if your crate were nightly-only, you could add \ | ||
`#![feature(default_field_values)]` to your crate and" | ||
}, |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add that.
@rustbot author |
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: (), .. }; | ++++ ```
86e6877
to
922e6bb
Compare
@rustbot review |
Split off the parts of the test meant for privacy check into its own file (and own PR: #135846). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@bors r+ rollup |
…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: (), .. }; | ++++ ```
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
…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: (), .. }; | ++++ ```
When a struct ctor use has missing fields, if all those missing fields have defaults, suggest
..
: