Do not suggest struct literal when fields are private
This commit is contained in:
parent
e0e379b6fd
commit
12a8bb8d9b
2 changed files with 65 additions and 36 deletions
|
@ -1449,6 +1449,34 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
),
|
||||
_ => (": val", "literal", Applicability::HasPlaceholders, None),
|
||||
};
|
||||
|
||||
let fields = match def_id.as_local() {
|
||||
Some(def_id) => {
|
||||
self.r.struct_constructors.get(&def_id).cloned().map(|(_, _, f)| f)
|
||||
}
|
||||
None => Some(
|
||||
self.r
|
||||
.tcx
|
||||
.associated_item_def_ids(def_id)
|
||||
.iter()
|
||||
.map(|field_id| self.r.tcx.visibility(field_id))
|
||||
.collect(),
|
||||
),
|
||||
};
|
||||
|
||||
let hidden_fields = fields.map_or(false, |fields| {
|
||||
fields
|
||||
.iter()
|
||||
.filter(|vis| {
|
||||
!self.r.is_accessible_from(**vis, self.parent_scope.module)
|
||||
})
|
||||
.next()
|
||||
.is_some()
|
||||
});
|
||||
|
||||
if !hidden_fields {
|
||||
// If the fields of the type are private, we shouldn't be suggesting using
|
||||
// the struct literal syntax at all, as that will cause a subsequent error.
|
||||
let field_ids = self.r.field_def_ids(def_id);
|
||||
let (fields, applicability) = match field_ids {
|
||||
Some(field_ids) => {
|
||||
|
@ -1488,6 +1516,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
applicability,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
err.span_label(span, fallback_label.to_string());
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ error[E0423]: expected value, found struct `xcrate_unit_struct::StructWithPrivFi
|
|||
--> $DIR/xcrate-unit-struct.rs:11:13
|
||||
|
|
||||
LL | let _ = xcrate_unit_struct::StructWithPrivFields;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `xcrate_unit_struct::StructWithPrivFields { foo: val }`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
::: $DIR/auxiliary/xcrate_unit_struct.rs:25:1
|
||||
|
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue