Skip to content

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

Closed
@KallDrexx

Description

@KallDrexx
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-visibilityArea: Visibility / privacyC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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