Skip to content

Commit 757a009

Browse files
committed
suggest dotdot for ignoring fields with private types
1 parent bdc97d1 commit 757a009

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

compiler/rustc_privacy/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interfac
2525
2626
privacy_item_is_private = {$kind} `{$descr}` is private
2727
.label = private {$kind}
28+
.suggestion = consider using `..` to ignore fields with private types
2829
2930
privacy_private_interface_or_bounds_lint = {$ty_kind} `{$ty_descr}` is more private than the item `{$item_descr}`
3031
.item_label = {$item_kind} `{$item_descr}` is reachable at visibility `{$item_vis_descr}`

compiler/rustc_privacy/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub(crate) struct ItemIsPrivate<'a> {
4242
pub span: Span,
4343
pub kind: &'a str,
4444
pub descr: DiagArgFromDisplay<'a>,
45+
#[suggestion(style = "verbose", code = "..", applicability = "maybe-incorrect")]
46+
pub pat_span: Option<Span>,
4547
}
4648

4749
#[derive(Diagnostic)]

compiler/rustc_privacy/src/lib.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,16 +1117,32 @@ struct TypePrivacyVisitor<'tcx> {
11171117
module_def_id: LocalModDefId,
11181118
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
11191119
span: Span,
1120+
hir_id: Option<hir::HirId>,
11201121
}
11211122

11221123
impl<'tcx> TypePrivacyVisitor<'tcx> {
11231124
fn item_is_accessible(&self, did: DefId) -> bool {
11241125
self.tcx.visibility(did).is_accessible_from(self.module_def_id, self.tcx)
11251126
}
11261127

1128+
fn tuple_struct_field_with_private_type_span(&self) -> Option<Span> {
1129+
if let Some(hir_id) = self.hir_id
1130+
&& let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id)
1131+
&& let hir::PatKind::Wild = pat.kind
1132+
&& let hir::Node::Pat(pat) = self.tcx.parent_hir_node(hir_id)
1133+
&& let hir::PatKind::TupleStruct(_, pat_arr, _) = pat.kind
1134+
&& pat_arr.len() == 1
1135+
{
1136+
Some(self.tcx.hir().span(hir_id))
1137+
} else {
1138+
None
1139+
}
1140+
}
1141+
11271142
// Take node-id of an expression or pattern and check its type for privacy.
11281143
fn check_expr_pat_type(&mut self, id: hir::HirId, span: Span) -> bool {
11291144
self.span = span;
1145+
self.hir_id = Some(id);
11301146
let typeck_results = self
11311147
.maybe_typeck_results
11321148
.unwrap_or_else(|| span_bug!(span, "`hir::Expr` or `hir::Pat` outside of a body"));
@@ -1143,7 +1159,12 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
11431159
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
11441160
let is_error = !self.item_is_accessible(def_id);
11451161
if is_error {
1146-
self.tcx.dcx().emit_err(ItemIsPrivate { span: self.span, kind, descr: descr.into() });
1162+
self.tcx.dcx().emit_err(ItemIsPrivate {
1163+
span: self.span,
1164+
kind,
1165+
descr: descr.into(),
1166+
pat_span: self.tuple_struct_field_with_private_type_span(),
1167+
});
11471168
}
11481169
is_error
11491170
}
@@ -1278,9 +1299,12 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12781299
let kind = self.tcx.def_descr(def_id);
12791300
let sess = self.tcx.sess;
12801301
let _ = match name {
1281-
Some(name) => {
1282-
sess.dcx().emit_err(ItemIsPrivate { span, kind, descr: (&name).into() })
1283-
}
1302+
Some(name) => sess.dcx().emit_err(ItemIsPrivate {
1303+
span,
1304+
kind,
1305+
descr: (&name).into(),
1306+
pat_span: None,
1307+
}),
12841308
None => sess.dcx().emit_err(UnnamedItemIsPrivate { span, kind }),
12851309
};
12861310
return;
@@ -1776,7 +1800,8 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
17761800
// Check privacy of explicitly written types and traits as well as
17771801
// inferred types of expressions and patterns.
17781802
let span = tcx.def_span(module_def_id);
1779-
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span };
1803+
let mut visitor =
1804+
TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span, hir_id: None };
17801805

17811806
let module = tcx.hir_module_items(module_def_id);
17821807
for def_id in module.definitions() {

0 commit comments

Comments
 (0)