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

Non-public tuple fields causes confusing error message when crossing module boundaries #52144

Closed
KallDrexx opened this issue Jul 7, 2018 · 3 comments · Fixed by #106579
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-visibility Area: Visibility / privacy C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@KallDrexx
Copy link

KallDrexx commented Jul 7, 2018

mod first {
    #[derive(Debug)]
    pub struct SomeId(u32);    
}

mod second {
    use ::first::SomeId;

    #[derive(Debug)]    
    pub enum MyEnum {
        Variant1 {
            id: SomeId
        }
    }
}

fn main() {
    use first::SomeId;
    use second::MyEnum;

    let test = MyEnum::Variant1 {
        id: SomeId(32),
    };
    
    println!("{:?}", test);
}

This gave me the following error message:

error[E0423]: expected function, found struct `SomeId`
  --> src/main.rs:22:13
   |
22 |         id: SomeId(32),
   |             ^^^^^^
   |             |
   |             did you mean `Some`?
   |             constructor is not visible here due to private fields
help: possible better candidate is found in another module, you can import it into scope
   |
1  | use first::SomeId;
   |

It turns out the fix for this was to change my struct definition to pub struct SomeId(pub u32);. This was not obvious to me for several reasons:

  1. The expected function, found struct SomeId is odd because that's what I'm trying to perform, so it appears like it's erroring because it's doing what I want.
  2. The suggestion for the use statement is completely incorrect, as that use statement already exists and won't fix the problem
  3. The constructor is not visible here due to private fields didn't make sense to me as the rust book never gives an example of the Struct(pub fieldType) syntax, so I assumed tuple fields had the same visibility as the struct itself.

It probably needs a recommendation for tuple structs to show that all fields should be marked as pub

@oyvindln
Copy link
Contributor

oyvindln commented Aug 11, 2019

Even more confusing when you have an empty tuple like this:
pub struct Tuple(())

The solution is of course:
pub struct Tuple(pub ())
😕

@estebank
Copy link
Contributor

estebank commented Jan 6, 2020

Current output:

error[E0423]: expected function, tuple struct or tuple variant, found struct `SomeId`
  --> file17.rs:22:13
   |
22 |         id: SomeId(32),
   |             ^^^^^^
   |             |
   |             constructor is not visible here due to private fields
   |             help: a tuple variant with a similar name exists: `Some`

We should point at the def span of SomeId when we say constructor is not visible here due to private fields.

@estebank estebank added A-visibility Area: Visibility / privacy D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 6, 2020
@compiler-errors
Copy link
Member

Current output:

error[[E0423]](https://doc.rust-lang.org/stable/error-index.html#E0423): cannot initialize a tuple struct which contains private fields
  --> src/main.rs:22:13
   |
22 |         id: SomeId(32),
   |             ^^^^^^ help: a tuple variant with a similar name exists: `Some`
   |
note: constructor is not visible here due to private fields
  --> src/main.rs:3:23
   |
3  |     pub struct SomeId(u32);    
   |                       ^^^ private field

Could provide a structured suggestion, but at least does point at the private field in question.

Noratrieb added a commit to Noratrieb/rust that referenced this issue Jan 12, 2023
Suggest making private tuple struct field public

Fix rust-lang#52144.
Noratrieb added a commit to Noratrieb/rust that referenced this issue Jan 12, 2023
Suggest making private tuple struct field public

Fix rust-lang#52144.
@bors bors closed this as completed in ad13d9f Jan 12, 2023
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-visibility Area: Visibility / privacy C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

5 participants