Skip to content

Detect tuple structs that are unconstructable due to re-export #133477

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Conversation

estebank
Copy link
Contributor

When a tuple-struct is re-exported that has inaccessible fields at the use scope, the type's constructor cannot be accessed through that re-export. We now account for this case and extend the resulting resolution error. We also check if the constructor would be accessible directly, not through the re-export, and if so, we suggest using the full path instead.

error[E0423]: cannot initialize a tuple struct which contains private fields
  --> $DIR/ctor-not-accessible-due-to-inaccessible-field-in-reexport.rs:12:33
   |
LL |             let crate::Foo(x) = crate::Foo(42);
   |                                 ^^^^^^^^^^
   |
note: the type is accessed through this re-export, but the type's constructor is not visible in this import's scope due to private fields
  --> $DIR/ctor-not-accessible-due-to-inaccessible-field-in-reexport.rs:3:9
   |
LL | pub use my_mod::Foo;
   |         ^^^^^^^^^^^
help: the type can be constructed directly, because its fields are available from the current scope
   |
LL |             let crate::Foo(x) = crate::my_mod::Foo(42);
   |                                 ~~~~~~~~~~~~~~~~~~

Fix #133343.

When a tuple-struct is re-exported that has inaccessible fields at the `use` scope, the type's constructor cannot be accessed through that re-export. We now account for this case and extend the resulting resolution error. We also check if the constructor would be accessible directly, not through the re-export, and if so, we suggest using the full path instead.

```
error[E0423]: cannot initialize a tuple struct which contains private fields
  --> $DIR/ctor-not-accessible-due-to-inaccessible-field-in-reexport.rs:12:33
   |
LL |             let crate::Foo(x) = crate::Foo(42);
   |                                 ^^^^^^^^^^
   |
note: the type is accessed through this re-export, but the type's constructor is not visible in this import's scope due to private fields
  --> $DIR/ctor-not-accessible-due-to-inaccessible-field-in-reexport.rs:3:9
   |
LL | pub use my_mod::Foo;
   |         ^^^^^^^^^^^
help: the type can be constructed directly, because its fields are available from the current scope
   |
LL |             let crate::Foo(x) = crate::my_mod::Foo(42);
   |                                 ~~~~~~~~~~~~~~~~~~
```

Fix rust-lang#133343.
@rustbot
Copy link
Collaborator

rustbot commented Nov 25, 2024

r? @cjgillot

rustbot has assigned @cjgillot.
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 Nov 25, 2024
@bors

This comment was marked as resolved.

@wesleywiser
Copy link
Member

r? rust-lang/compiler

@rustbot rustbot assigned davidtwco and unassigned cjgillot Mar 27, 2025
@davidtwco
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 16, 2025

📌 Commit 4063732 has been approved by davidtwco

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 Apr 16, 2025
@davidtwco
Copy link
Member

@bors r- needs a rebase

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 16, 2025
# for free to join this conversation on GitHub. Already have an account? # 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 error message when using re-exported struct [E0423]
6 participants