Skip to content

Commit d03c02a

Browse files
authored
Rollup merge of #70331 - jeremystucki:privacy, r=estebank
Increase verbosity when using update syntax with private fields Resolves #70323
2 parents 02046a5 + 17b97ee commit d03c02a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/librustc_privacy/lib.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1023,12 +1023,19 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
10231023
span: Span, // span of the field pattern, e.g., `x: 0`
10241024
def: &'tcx ty::AdtDef, // definition of the struct or enum
10251025
field: &'tcx ty::FieldDef,
1026+
in_update_syntax: bool,
10261027
) {
10271028
// definition of the field
10281029
let ident = Ident::new(kw::Invalid, use_ctxt);
10291030
let current_hir = self.current_item;
10301031
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did, current_hir).1;
10311032
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
1033+
let label = if in_update_syntax {
1034+
format!("field `{}` is private", field.ident)
1035+
} else {
1036+
"private field".to_string()
1037+
};
1038+
10321039
struct_span_err!(
10331040
self.tcx.sess,
10341041
span,
@@ -1038,7 +1045,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
10381045
def.variant_descr(),
10391046
self.tcx.def_path_str(def.did)
10401047
)
1041-
.span_label(span, "private field")
1048+
.span_label(span, label)
10421049
.emit();
10431050
}
10441051
}
@@ -1106,13 +1113,13 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
11061113
Some(field) => (field.ident.span, field.span),
11071114
None => (base.span, base.span),
11081115
};
1109-
self.check_field(use_ctxt, span, adt, variant_field);
1116+
self.check_field(use_ctxt, span, adt, variant_field, true);
11101117
}
11111118
} else {
11121119
for field in fields {
11131120
let use_ctxt = field.ident.span;
11141121
let index = self.tcx.field_index(field.hir_id, self.tables);
1115-
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
1122+
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
11161123
}
11171124
}
11181125
}
@@ -1131,7 +1138,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
11311138
for field in fields {
11321139
let use_ctxt = field.ident.span;
11331140
let index = self.tcx.field_index(field.hir_id, self.tables);
1134-
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
1141+
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
11351142
}
11361143
}
11371144
_ => {}

src/test/ui/functional-struct-update/functional-struct-update-respects-privacy.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0451]: field `secret_uid` of struct `foo::S` is private
22
--> $DIR/functional-struct-update-respects-privacy.rs:28:49
33
|
44
LL | let s_2 = foo::S { b: format!("ess two"), ..s_1 }; // FRU ...
5-
| ^^^ private field
5+
| ^^^ field `secret_uid` is private
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)