Skip to content

add help when constructing ADTs with private fields#156943

Open
euclio wants to merge 1 commit into
rust-lang:mainfrom
euclio:private-initializer
Open

add help when constructing ADTs with private fields#156943
euclio wants to merge 1 commit into
rust-lang:mainfrom
euclio:private-initializer

Conversation

@euclio
Copy link
Copy Markdown
Contributor

@euclio euclio commented May 25, 2026

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 new being a common idiom to construct private structs, or even suggesting it if it exists.

@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 May 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 25, 2026

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

Comment on lines +2644 to +2646
format!(
"initializer expressions cannot construct structs with private fields"
),
Copy link
Copy Markdown
Member

@SpriteOvO SpriteOvO May 26, 2026

Choose a reason for hiding this comment

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

Maybe

Suggested change
format!(
"initializer expressions cannot construct structs with private fields"
),
format!(
"initializer syntax cannot be used to construct structs with inaccessible private fields"
),

?

View changes since the review

--> $DIR/suggest-private-fields.rs:17:9
|
LL | bb: 20,
| ^^ `B` does not have this field
Copy link
Copy Markdown
Member

@SpriteOvO SpriteOvO May 26, 2026

Choose a reason for hiding this comment

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

Perhaps keeping this message is better? "Struct does not have this field" and "Struct have inaccessible private fields" convey two different pieces of information.

View changes since the review

Comment on lines 1 to 5
error[E0451]: field `b` of struct `Foo` is private
--> $DIR/E0451.rs:14:21
|
LL | let bar::Foo{a, b} = foo;
| ^ private field
Copy link
Copy Markdown
Member

@SpriteOvO SpriteOvO May 26, 2026

Choose a reason for hiding this comment

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

This should also trigger a help message.

View changes since the review

Comment on lines 7 to +17
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 };
| ^^^^^^^^^^^^^^^^^^^^^^
Copy link
Copy Markdown
Member

@SpriteOvO SpriteOvO May 26, 2026

Choose a reason for hiding this comment

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

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 };
-    |             ^^^^^^^^^^^^^^^^^^^^^^

View changes since the review

@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 May 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 26, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Confusing note about "all struct fields are already assigned" when some fields are private

4 participants