How should this error message be improved?

Got a really confusing error message:

pub struct Error(String);

error[E0603]: tuple struct `Error` is private
    |
195 |                         Error(e.to_string()))
    |                         ^^^^^

The answer was of course:
pub struct Error(pub String);

But the error message really didn't help much. I really wanted this error message to say something like "error[E0603]: tuple struct Error is marked pub but has a private member, Maybe the member should be marked 'pub' as well?"

Agree / Disagree? I'm happy to go compiler diving to try and improve the error message, but thought I should get consensus on what a better error message might look like first.

2 Likes

It's referring to the constructor of the struct, which I thought had a correct error message in earlier rust builds.

Either the compiler internals know the correct error - E0451 and it's wrongly communicated to the user.
Or someone wrongly implemented the procedure to find out the correct error for the provided code.

I couldn't say which one it is, but I have noticed the same issue once before where the compiler returns a more general message instead.

I got this in stable, but have just checked nightly and same not-too-helpful error. (For the record: rustc 1.32.0-nightly (36a50c29f 2018-11-09 )

You're absolutely right - it should produce E0451. Minimum repetable error is:

mod x {
    pub struct MyTupleStruct(String);
}

fn main() {
    x::MyTupleStruct(String::new());
}

It reports an excellent error with normal structs, just tuple structs it seems to happen with.

The fix might just be to talk about the tuple struct constructor.

In Error(e.to_string()), the Error is actually a function in the value namespace. That function is private because the field is private.

So maybe just a small tweak to the existing string, with a "help: constructor is private because field 0 is private" pointing at String.

Filed https://github.com/rust-lang/rust/issues/58017 to address this.