Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
fn check_field(&mut self, span: Span, def: ty::AdtDef<'tcx>, field: ty::FieldDef<'tcx>) {
if def.adt_kind() == ty::AdtKind::Struct &&
!field.vis.is_accessible_from(self.curitem, &self.tcx.map) {
span_err!(self.tcx.sess, span, E0451, "field `{}` of struct `{}` is private",
field.name, self.tcx.item_path_str(def.did));
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of struct `{}` is private",
field.name, self.tcx.item_path_str(def.did))
.span_label(span, &format!("field `{}` is private", field.name))
.emit();
}
}

Expand Down Expand Up @@ -425,14 +427,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
let method = self.tcx.tables.borrow().method_map[&method_call];
self.check_method(expr.span, method.def_id);
}
hir::ExprStruct(..) => {
hir::ExprStruct(_, ref fields, _) => {
let adt = self.tcx.expr_ty(expr).ty_adt_def().unwrap();
let variant = adt.variant_of_def(self.tcx.expect_def(expr.id));
// RFC 736: ensure all unmentioned fields are visible.
// Rather than computing the set of unmentioned fields
// (i.e. `all_fields - fields`), just check them all.
for field in &variant.fields {
self.check_field(expr.span, adt, field);
for field in variant.fields.iter() {
let span = if let Some(f) = fields.iter().find(|f| f.name.node == field.name) {
f.span
} else {
expr.span
};
self.check_field(span, adt, field);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work now, however I'm a bit concerned that the complexity here went from linear to quadratic (for all struct literals). I'm not sure how much of an impact this will have on real world code however there are crates with structs with 1500+ fields in which case this may not be ideal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't expect so big structs, but this code works only on matching expressions, does any one match more then tens fields?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the situation correctly, this code runs for every struct literal, ie. let _ = Foo { bar: "hi!" }. And initialising such large structs does happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll rework it

}
}
hir::ExprPath(..) => {
Expand Down Expand Up @@ -472,7 +479,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
let adt = self.tcx.pat_ty(pattern).ty_adt_def().unwrap();
let variant = adt.variant_of_def(self.tcx.expect_def(pattern.id));
for field in fields {
self.check_field(pattern.span, adt, variant.field_named(field.node.name));
self.check_field(field.span, adt, variant.field_named(field.node.name));
}
}
PatKind::TupleStruct(_, ref fields, ddpos) => {
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/E0451.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ mod Bar {
pub a: isize,
b: isize,
}

pub struct FooTuple (
pub isize,
isize,
);
}

fn pat_match(foo: Bar::Foo) {
let Bar::Foo{a:a, b:b} = foo; //~ ERROR E0451
//~^ NOTE field `b` is private
}

fn pat_match_tuple(foo: Bar::FooTuple) {
let Bar::FooTuple(a,b) = foo; //~ ERROR E0451
//~^ NOTE field `1` is private
}

fn main() {
let f = Bar::Foo{ a: 0, b: 0 }; //~ ERROR E0451
//~^ NOTE field `b` is private
}