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

Incorrect error message when accessing private field of union #63976

Closed
gnzlbg opened this issue Aug 28, 2019 · 5 comments · Fixed by #63982
Closed

Incorrect error message when accessing private field of union #63976

gnzlbg opened this issue Aug 28, 2019 · 5 comments · Fixed by #63982
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-visibility Area: Visibility / privacy C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Aug 28, 2019

#[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

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-visibility Area: Visibility / privacy C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 28, 2019
@estebank estebank added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Aug 28, 2019
@sam09
Copy link
Contributor

sam09 commented Aug 28, 2019

Can I pick this up if no-one is working on this?

@estebank
Copy link
Contributor

estebank commented Aug 28, 2019

@sam09 go ahead!

You need to use base_did here

let struct_path = self.tcx().def_path_str(base_did);
let mut err = struct_span_err!(
self.tcx().sess,
expr.span,
E0616,
"field `{}` of struct `{}` is private",
field,
struct_path
);

to get whether it is a struct or union using DefKind::descr(DefId).

@sam09
Copy link
Contributor

sam09 commented Aug 28, 2019

Thanks @estebank .
Newbie question, but how do I test this while development? Do I build the stage1 compiler and then write a sample test?

@sam09
Copy link
Contributor

sam09 commented Aug 28, 2019

#63982

@estebank
Copy link
Contributor

estebank commented Aug 28, 2019

@sam09 ./x.py build src/test/ui -i --stage 1 --keep-stage 1 --bless is the way to get the fastest iteration time. --keep-stage 1 is theoretically suspect but should affect you on what you're working on and -i is marginally slower on first compile but much faster on subsequent compiles. You rarely (if ever) need --stage 2.

Centril added a commit to Centril/rust that referenced this issue Aug 30, 2019
When accessing private field of union, do not misidentify it as a struct

Fix incorrect error message when accessing private field of union.

Fixes rust-lang#63976.
bors added a commit that referenced this issue Aug 30, 2019
When accessing private field of union, do not misidentify it as a struct

Fix incorrect error message when accessing private field of union.

Fixes #63976.
@bors bors closed this as completed in fdd8b96 Aug 30, 2019
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-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. 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.

4 participants