Skip to content

Incorrect error message when accessing private field of union #63976

Closed
@gnzlbg

Description

@gnzlbg
#[derive(Copy, Clone)]
pub struct B;

mod a {
    pub union U {
        pub a: i32,
        b: crate::B,
    }
}

fn new(a: i32) -> B {
    unsafe { a::U { a }.b }
}

(Playground)

Errors:

    Compiling playground v0.0.1 (/playground)
error[E0616]: field `b` of struct `a::U` is private
  --> src/lib.rs:12:14
   |
12 |     unsafe { a::U { a }.b }
   |              ^^^^^^^^^^^^

error: aborting due to previous error

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

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

Notice how it says "struct" when it should say "union".

cc @estebank this looks like low-hanging fruit

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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