Skip to content

Confusing error message when using re-exported struct [E0423] #133343

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
PitiBouchon opened this issue Nov 22, 2024 · 0 comments · May be fixed by #133477
Open

Confusing error message when using re-exported struct [E0423] #133343

PitiBouchon opened this issue Nov 22, 2024 · 0 comments · May be fixed by #133477
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PitiBouchon
Copy link

PitiBouchon commented Nov 22, 2024

Code

pub use my_mod::MyStruct; // this pub use is causing the problem

mod my_mod {
    #[derive(Debug)]
    pub struct MyStruct(u32);

    mod my_sub_mod {
        use crate::MyStruct; // import the rexported struct

        fn my_func() {
            let s = MyStruct(42);
            println!("MyStruct: {:?}", s);
        }
    }
}

Current output

error[E0423]: expected function, tuple struct or tuple variant, found struct `MyStruct`
  --> src/lib.rs:11:21
   |
11 |             let s = MyStruct(42);
   |                     ^^^^^^^^

For more information about this error, try `rustc --explain E0423`.

Desired output

Either having no error or the error saying the import is bad

Rust Version

rustc 1.81.0

Anything else?

This is a really confusing message that is not helping

@PitiBouchon PitiBouchon added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 22, 2024
estebank added a commit to estebank/rust that referenced this issue Nov 25, 2024
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.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant