add help when constructing ADTs with private fields#156943
Conversation
|
r? @TaKO8Ki rustbot has assigned @TaKO8Ki. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| format!( | ||
| "initializer expressions cannot construct structs with private fields" | ||
| ), |
There was a problem hiding this comment.
Maybe
| format!( | |
| "initializer expressions cannot construct structs with private fields" | |
| ), | |
| format!( | |
| "initializer syntax cannot be used to construct structs with inaccessible private fields" | |
| ), |
?
| --> $DIR/suggest-private-fields.rs:17:9 | ||
| | | ||
| LL | bb: 20, | ||
| | ^^ `B` does not have this field |
There was a problem hiding this comment.
Perhaps keeping this message is better? "Struct does not have this field" and "Struct have inaccessible private fields" convey two different pieces of information.
| error[E0451]: field `b` of struct `Foo` is private | ||
| --> $DIR/E0451.rs:14:21 | ||
| | | ||
| LL | let bar::Foo{a, b} = foo; | ||
| | ^ private field |
There was a problem hiding this comment.
This should also trigger a help message.
| error[E0451]: field `b` of struct `Foo` is private | ||
| --> $DIR/E0451.rs:18:29 | ||
| | | ||
| LL | let f = bar::Foo{ a: 0, b: 0 }; | ||
| | ^ private field | ||
| | | ||
| help: initializer expressions cannot construct structs with private fields | ||
| --> $DIR/E0451.rs:18:13 | ||
| | | ||
| LL | let f = bar::Foo{ a: 0, b: 0 }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^ |
There was a problem hiding this comment.
Personally, I feel the new help message creates extra noise in some cases. For example here. I’d rather expect this to become a note, because help is used to provide suggestions.
error[E0451]: field `b` of struct `Foo` is private
--> $DIR/E0451.rs:18:29
|
LL | let f = bar::Foo{ a: 0, b: 0 };
| ^ private field
|
+ note: initializer expressions cannot construct structs with private fields
- help: initializer expressions cannot construct structs with private fields
- --> $DIR/E0451.rs:18:13
- |
- LL | let f = bar::Foo{ a: 0, b: 0 };
- | ^^^^^^^^^^^^^^^^^^^^^^|
Reminder, once the PR becomes ready for a review, use |
Fixes #153259.
I'm a little unsure about the wording. I don't want to imply that private fields can never be used in initializer expressions, since private fields are fine if the fields are in the same module. The existing error message uses the term "private", though, so I went for consistency.
I also wonder if it would be useful to include a note about
fn newbeing a common idiom to construct private structs, or even suggesting it if it exists.