-
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 struct construction with private field in field with default #135846
base: master
Are you sure you want to change the base?
Conversation
tests/ui/structs/default-field-values/non-exhaustive-ctor-2.stderr
Outdated
Show resolved
Hide resolved
5f24487
to
7e07e4f
Compare
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.
Preliminary review.
help: the type `Priv1` of field `field1` is private, but you can construct the default value defined for it in `S` using `..` in the struct initializer expression | ||
| | ||
LL - let _ = S { field: (), field1: m::Priv1 {}, field2: m::Priv2 }; | ||
LL + let _ = S { field: (), , field2: m::Priv2, .. }; |
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.
Slightly butchered suggestion (double comma).
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 need to spend some time building some machinery to properly handle all possible cases of removal of elements from the HIR. We have too many copies of the same error-prone logic that needs to account for potential trailing commas or commas between the current element being removed and the next one. I'll do that as a separate PR.
@@ -1022,6 +1023,7 @@ pub struct Resolver<'ra, 'tcx> { | |||
|
|||
/// N.B., this is used only for better diagnostics, not name resolution itself. | |||
field_names: LocalDefIdMap<Vec<Ident>>, | |||
field_defaults: LocalDefIdMap<Vec<Symbol>>, |
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.
Using Symbol
instead Ident
raises a few warning flags in my head because I'm pretty sure that this means anything involving field_defaults
doesn't handle macro hygiene correctly. This is only relevant to decl macros 2.0 (feature decl_macro
).
It might be a non-issue, I haven't thought that hard yet. I'll need to read the rest of this PR first.
Checking perf due to additional query feeding in the happy path (if I saw that correctly). @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…r=<try> Detect struct construction with private field in field with default When trying to construct a struct that has a public field of a private type, suggest using `..` if that field has a default value. ``` error[E0603]: struct `Priv1` is private --> $DIR/non-exhaustive-ctor.rs:25:39 | LL | let _ = S { field: (), field1: m::Priv1 {} }; | ------ ^^^^^ private struct | | | while setting this field | note: the struct `Priv1` is defined here --> $DIR/non-exhaustive-ctor.rs:14:4 | LL | struct Priv1 {} | ^^^^^^^^^^^^ help: the field `field1` you're trying to set has a default value, you can use `..` to use it | LL | let _ = S { field: (), .. }; | ~~ ```
This comment has been minimized.
This comment has been minimized.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (5e87c8e): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis 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.
Max RSS (memory usage)Results (primary 2.1%, secondary -0.1%)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.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 770.579s -> 772.7s (0.28%) |
When trying to construct a struct that has a public field of a private type, suggest using `..` if that field has a default value. ``` error[E0603]: struct `Priv1` is private --> $DIR/non-exhaustive-ctor.rs:25:39 | LL | let _ = S { field: (), field1: m::Priv1 {} }; | ------ ^^^^^ private struct | | | while setting this field | note: the struct `Priv1` is defined here --> $DIR/non-exhaustive-ctor.rs:14:4 | LL | struct Priv1 {} | ^^^^^^^^^^^^ help: the field `field1` you're trying to set has a default value, you can use `..` to use it | LL | let _ = S { field: (), .. }; | ~~ ```
7e07e4f
to
38f8f98
Compare
When trying to construct a struct that has a public field of a private type, suggest using
..
if that field has a default value.