Skip to content

confusing error message 'constructor is not visible here due to private fields' #83450

Closed
@jakobhellermann

Description

@jakobhellermann

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ab05c6fc2149226b37b685ca913e1707

mod module {
    pub struct SomeTupleStruct(u8);
    pub struct SomeRegularStruct {
        foo: u8
    }
    
    impl SomeTupleStruct {
        pub fn new() -> Self {
            Self(0)
        }
    }
    impl RegularStruct {
        pub fn new() -> Self {
            Self { foo: 0 }
        }
    }
}

use module::{SomeTupleStruct, SomeRegularStruct};

fn main() {
    let _ = SomeTupleStruct.new();
    let _ = SomeRegularStruct.new();
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0423]: expected value, found struct `SomeTupleStruct`
  --> src/main.rs:22:13
   |
22 |     let _ = SomeTupleStruct.new();
   |             ^^^^^^^^^^^^^^^ constructor is not visible here due to private fields

error[E0423]: expected value, found struct `SomeRegularStruct`
  --> src/main.rs:23:13
   |
23 |     let _ = SomeRegularStruct.new();
   |             ^^^^^^^^^^^^^^^^^----
   |             |
   |             help: use the path separator to refer to an item: `SomeRegularStruct::new`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0423`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Ideally the first error should look like the second, i.e. suggest using SomeTupleStruct::new instead.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions