Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Error for instantiating tuple struct with private field has regressed on nightly #39703

Closed
jonas-schievink opened this issue Feb 9, 2017 · 4 comments · Fixed by #65153
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jonas-schievink
Copy link
Contributor

fn main() {
    module::TupleStruct(0);
}

pub mod module {
    pub struct TupleStruct(u8);
}

On stable/beta, the error message is very good:

error[E0450]: cannot invoke tuple struct constructor with private fields
 --> <anon>:2:5
  |
2 |     module::TupleStruct(0);
  |     ^^^^^^^^^^^^^^^^^^^ cannot construct with a private field
...
6 |     pub struct TupleStruct(u8);
  |                            --- private field declared here

error: aborting due to previous error

On nightly, it's flat out lying to the user:

error: tuple struct `TupleStruct` is private
 --> <anon>:2:5
  |
2 |     module::TupleStruct(0);
  |     ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
@petrochenkov
Copy link
Contributor

The error was detected by a special code in rustc_privacy previously, now it's caught by a generic privacy check in resolve.
The code is here: https://github.com/rust-lang/rust/blob/master/src/librustc_resolve/lib.rs#L3147
It should probably detect the case def is Def::StructCtor(..) and add a note about constructor visibility being determined by visibility of its fields.

@petrochenkov petrochenkov self-assigned this Feb 19, 2017
@petrochenkov petrochenkov added A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution labels Feb 19, 2017
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@Restioson
Copy link

Restioson commented Nov 30, 2017

Made a couple example playgrounds in a duplicate I made (#46407):

Here we see that when using an absolute path to the struct we get the misleading error: playground.

Here we see that adding a use for the struct gives us a better error, though imo the 'better candidate' section is wrong as we already do what it suggests: playground

It's also worth noting that this misleading error is not only on nightly -- it is also on stable.

@estebank
Copy link
Contributor

Current output:

error[E0603]: tuple struct `Foo` is private
  --> src/main.rs:12:14
   |
12 |         ::A::Foo(0)
   |              ^^^
error[E0423]: expected function, found struct `Foo`
  --> src/main.rs:13:9
   |
13 |         Foo(0)
   |         ^^^ constructor is not visible here due to private fields

@nilslice
Copy link
Contributor

nilslice commented Aug 8, 2019

FWIW, this caught me just now and although just a short google away, it still had me scratching my head for a bit!

error[E0603]: tuple struct `Link` is private
  --> sandbox/src/main.rs:24:25
   |
24 |         address: derp::Link(Address{
   |           

rustc 1.36.0 (a53f9df 2019-07-03)


For those who reach this issue and still have not resolved the error, you must add pub prefixed to your type parameter within the tuple, as such:

pub struct Link(pub Address);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

7 participants