Skip to content

Misleading error message, privacy error reported as type error #64684

@gnzlbg

Description

@gnzlbg
mod foo {
    pub struct Foo {
        private: i32,
    }
}

use foo::Foo;

fn main() {
    let mut foo = Foo { private: 0_usize };
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:11:34
   |
11 |     let mut foo = Foo { private: 0_usize } ;
   |                                  ^^^^^^^ expected i32, found usize

error: aborting due to previous error

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

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

The error message here is incorrect. Whether the types match or not is irrelevant, because the field is private. The error should say something like this instead (playground):

 error[E0451]: field `private` of struct `foo::Foo` is private
  --> src/main.rs:10:25
   |
10 |     let mut foo = Foo { private: 0_i32 };
   |                         ^^^^^^^^^^^^^^ field `private` is private

error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.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