Skip to content

Commit 4295eea

Browse files
committed
Auto merge of #63982 - sam09:fix-63976, r=estebank
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.
2 parents ecca4b8 + 378c32b commit 4295eea

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/librustc_typeck/check/expr.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1392,12 +1392,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13921392
base_did: DefId,
13931393
) {
13941394
let struct_path = self.tcx().def_path_str(base_did);
1395+
let kind_name = match self.tcx().def_kind(base_did) {
1396+
Some(def_kind) => def_kind.descr(base_did),
1397+
_ => " ",
1398+
};
13951399
let mut err = struct_span_err!(
13961400
self.tcx().sess,
13971401
expr.span,
13981402
E0616,
1399-
"field `{}` of struct `{}` is private",
1403+
"field `{}` of {} `{}` is private",
14001404
field,
1405+
kind_name,
14011406
struct_path
14021407
);
14031408
// Also check if an accessible method exists, which is often what is meant.

src/test/ui/privacy/union-field-privacy-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ fn main() {
1111

1212
let a = u.a; // OK
1313
let b = u.b; // OK
14-
let c = u.c; //~ ERROR field `c` of struct `m::U` is private
14+
let c = u.c; //~ ERROR field `c` of union `m::U` is private
1515
}

src/test/ui/privacy/union-field-privacy-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0616]: field `c` of struct `m::U` is private
1+
error[E0616]: field `c` of union `m::U` is private
22
--> $DIR/union-field-privacy-2.rs:14:13
33
|
44
LL | let c = u.c;

0 commit comments

Comments
 (0)