From fdd8b967c180192fb74bf17f07e2fda040bb9865 Mon Sep 17 00:00:00 2001 From: Sam Radhakrishan Date: Thu, 29 Aug 2019 02:43:09 +0530 Subject: [PATCH 1/2] Fixes #63976. Incorrect error message. Fix incorrect error message when accessing private field of union --- src/librustc_typeck/check/expr.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index d139cd4264c86..4943270e193ec 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1396,8 +1396,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.tcx().sess, expr.span, E0616, - "field `{}` of struct `{}` is private", + "field `{}` of `{}` `{}` is private", field, + if let Some(def_kind) = self.tcx().def_kind(base_did){ def_kind.descr(base_did) } + else { " " }, struct_path ); // Also check if an accessible method exists, which is often what is meant. From 378c32bc90b230d1f8feffee135942d442d2a5b7 Mon Sep 17 00:00:00 2001 From: Sam Radhakrishan Date: Fri, 30 Aug 2019 00:57:20 +0530 Subject: [PATCH 2/2] Fix test. --- src/librustc_typeck/check/expr.rs | 9 ++++++--- src/test/ui/privacy/union-field-privacy-2.rs | 2 +- src/test/ui/privacy/union-field-privacy-2.stderr | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 4943270e193ec..a53fb12367d0e 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1392,14 +1392,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { base_did: DefId, ) { let struct_path = self.tcx().def_path_str(base_did); + let kind_name = match self.tcx().def_kind(base_did) { + Some(def_kind) => def_kind.descr(base_did), + _ => " ", + }; let mut err = struct_span_err!( self.tcx().sess, expr.span, E0616, - "field `{}` of `{}` `{}` is private", + "field `{}` of {} `{}` is private", field, - if let Some(def_kind) = self.tcx().def_kind(base_did){ def_kind.descr(base_did) } - else { " " }, + kind_name, struct_path ); // Also check if an accessible method exists, which is often what is meant. diff --git a/src/test/ui/privacy/union-field-privacy-2.rs b/src/test/ui/privacy/union-field-privacy-2.rs index 48279630c6302..c2458f74bc8f9 100644 --- a/src/test/ui/privacy/union-field-privacy-2.rs +++ b/src/test/ui/privacy/union-field-privacy-2.rs @@ -11,5 +11,5 @@ fn main() { let a = u.a; // OK let b = u.b; // OK - let c = u.c; //~ ERROR field `c` of struct `m::U` is private + let c = u.c; //~ ERROR field `c` of union `m::U` is private } diff --git a/src/test/ui/privacy/union-field-privacy-2.stderr b/src/test/ui/privacy/union-field-privacy-2.stderr index df054b8cff8a6..8789178caac26 100644 --- a/src/test/ui/privacy/union-field-privacy-2.stderr +++ b/src/test/ui/privacy/union-field-privacy-2.stderr @@ -1,4 +1,4 @@ -error[E0616]: field `c` of struct `m::U` is private +error[E0616]: field `c` of union `m::U` is private --> $DIR/union-field-privacy-2.rs:14:13 | LL | let c = u.c;