Skip to content

Commit c651bac

Browse files
committed
Make FRU respect privacy of all struct fields, mentioned or unmentioned.
1 parent 7615e18 commit c651bac

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/librustc_privacy/lib.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ enum PrivacyResult {
388388

389389
enum FieldName {
390390
UnnamedField(uint), // index
391-
// FIXME #6993: change type (and name) from Ident to Name
392-
NamedField(ast::Ident),
391+
// (Name, not Ident, because struct fields are not macro-hygienic)
392+
NamedField(ast::Name),
393393
}
394394

395395
impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
@@ -663,9 +663,9 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
663663
name: FieldName) {
664664
let fields = ty::lookup_struct_fields(self.tcx, id);
665665
let field = match name {
666-
NamedField(ident) => {
667-
debug!("privacy - check named field {} in struct {:?}", ident.name, id);
668-
fields.iter().find(|f| f.name == ident.name).unwrap()
666+
NamedField(f_name) => {
667+
debug!("privacy - check named field {} in struct {:?}", f_name, id);
668+
fields.iter().find(|f| f.name == f_name).unwrap()
669669
}
670670
UnnamedField(idx) => &fields[idx]
671671
};
@@ -684,7 +684,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
684684
};
685685
let msg = match name {
686686
NamedField(name) => format!("field `{}` of {} is private",
687-
token::get_ident(name), struct_desc),
687+
token::get_name(name), struct_desc),
688688
UnnamedField(idx) => format!("field #{} of {} is private",
689689
idx + 1, struct_desc),
690690
};
@@ -871,7 +871,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
871871
match expr.node {
872872
ast::ExprField(ref base, ident) => {
873873
if let ty::ty_struct(id, _) = ty::expr_ty_adjusted(self.tcx, &**base).sty {
874-
self.check_field(expr.span, id, NamedField(ident.node));
874+
self.check_field(expr.span, id, NamedField(ident.node.name));
875875
}
876876
}
877877
ast::ExprTupField(ref base, idx) => {
@@ -895,18 +895,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
895895
}
896896
ast::ExprStruct(_, ref fields, _) => {
897897
match ty::expr_ty(self.tcx, expr).sty {
898-
ty::ty_struct(id, _) => {
899-
for field in (*fields).iter() {
900-
self.check_field(expr.span, id,
901-
NamedField(field.ident.node));
898+
ty::ty_struct(ctor_id, _) => {
899+
let all_fields = ty::lookup_struct_fields(self.tcx, ctor_id);
900+
for field in (*all_fields).iter() {
901+
self.check_field(expr.span, ctor_id,
902+
NamedField(field.name));
902903
}
903904
}
904905
ty::ty_enum(_, _) => {
905906
match self.tcx.def_map.borrow()[expr.id].clone() {
906907
def::DefVariant(_, variant_id, _) => {
907908
for field in fields.iter() {
908909
self.check_field(expr.span, variant_id,
909-
NamedField(field.ident.node));
910+
NamedField(field.ident.node.name));
910911
}
911912
}
912913
_ => self.tcx.sess.span_bug(expr.span,
@@ -971,15 +972,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
971972
ty::ty_struct(id, _) => {
972973
for field in fields.iter() {
973974
self.check_field(pattern.span, id,
974-
NamedField(field.node.ident));
975+
NamedField(field.node.ident.name));
975976
}
976977
}
977978
ty::ty_enum(_, _) => {
978979
match self.tcx.def_map.borrow().get(&pattern.id) {
979980
Some(&def::DefVariant(_, variant_id, _)) => {
980981
for field in fields.iter() {
981982
self.check_field(pattern.span, variant_id,
982-
NamedField(field.node.ident));
983+
NamedField(field.node.ident.name));
983984
}
984985
}
985986
_ => self.tcx.sess.span_bug(pattern.span,

0 commit comments

Comments
 (0)