Rollup merge of #70331 - jeremystucki:privacy, r=estebank

Increase verbosity when using update syntax with private fields

Resolves #70323
This commit is contained in:
Mazdak Farrokhzad 2020-03-25 06:45:27 +01:00 committed by GitHub
commit d03c02a589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -1023,12 +1023,19 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
span: Span, // span of the field pattern, e.g., `x: 0`
def: &'tcx ty::AdtDef, // definition of the struct or enum
field: &'tcx ty::FieldDef,
in_update_syntax: bool,
) {
// definition of the field
let ident = Ident::new(kw::Invalid, use_ctxt);
let current_hir = self.current_item;
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did, current_hir).1;
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
let label = if in_update_syntax {
format!("field `{}` is private", field.ident)
} else {
"private field".to_string()
};
struct_span_err!(
self.tcx.sess,
span,
@ -1038,7 +1045,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
def.variant_descr(),
self.tcx.def_path_str(def.did)
)
.span_label(span, "private field")
.span_label(span, label)
.emit();
}
}
@ -1106,13 +1113,13 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
Some(field) => (field.ident.span, field.span),
None => (base.span, base.span),
};
self.check_field(use_ctxt, span, adt, variant_field);
self.check_field(use_ctxt, span, adt, variant_field, true);
}
} else {
for field in fields {
let use_ctxt = field.ident.span;
let index = self.tcx.field_index(field.hir_id, self.tables);
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
}
}
}
@ -1131,7 +1138,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
for field in fields {
let use_ctxt = field.ident.span;
let index = self.tcx.field_index(field.hir_id, self.tables);
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
}
}
_ => {}

View File

@ -2,7 +2,7 @@ error[E0451]: field `secret_uid` of struct `foo::S` is private
--> $DIR/functional-struct-update-respects-privacy.rs:28:49
|
LL | let s_2 = foo::S { b: format!("ess two"), ..s_1 }; // FRU ...
| ^^^ private field
| ^^^ field `secret_uid` is private
error: aborting due to previous error