Skip to content

Suggest using dotdot for ignoring pat fields #137623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
1 change: 1 addition & 0 deletions compiler/rustc_privacy/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interfac

privacy_item_is_private = {$kind} `{$descr}` is private
.label = private {$kind}
.suggestion = consider using `..` to ignore the field with private type

privacy_private_interface_or_bounds_lint = {$ty_kind} `{$ty_descr}` is more private than the item `{$item_descr}`
.item_label = {$item_kind} `{$item_descr}` is reachable at visibility `{$item_vis_descr}`
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_privacy/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub(crate) struct ItemIsPrivate<'a> {
pub span: Span,
pub kind: &'a str,
pub descr: DiagArgFromDisplay<'a>,
#[suggestion(style = "verbose", code = "..", applicability = "maybe-incorrect")]
pub pat_span: Option<Span>,
}

#[derive(Diagnostic)]
Expand Down
35 changes: 30 additions & 5 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,16 +1117,32 @@ struct TypePrivacyVisitor<'tcx> {
module_def_id: LocalModDefId,
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
span: Span,
hir_id: Option<hir::HirId>,
}

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

fn tuple_struct_field_with_private_type_span(&self) -> Option<Span> {
if let Some(hir_id) = self.hir_id
&& let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id)
&& let hir::PatKind::Wild = pat.kind
&& let hir::Node::Pat(pat) = self.tcx.parent_hir_node(hir_id)
&& let hir::PatKind::TupleStruct(_, pat_arr, _) = pat.kind
&& pat_arr.len() == 1
{
Some(self.tcx.hir().span(hir_id))
} else {
None
}
}

// Take node-id of an expression or pattern and check its type for privacy.
fn check_expr_pat_type(&mut self, id: hir::HirId, span: Span) -> bool {
self.span = span;
self.hir_id = Some(id);
let typeck_results = self
.maybe_typeck_results
.unwrap_or_else(|| span_bug!(span, "`hir::Expr` or `hir::Pat` outside of a body"));
Expand All @@ -1143,7 +1159,12 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
let is_error = !self.item_is_accessible(def_id);
if is_error {
self.tcx.dcx().emit_err(ItemIsPrivate { span: self.span, kind, descr: descr.into() });
self.tcx.dcx().emit_err(ItemIsPrivate {
span: self.span,
kind,
descr: descr.into(),
pat_span: self.tuple_struct_field_with_private_type_span(),
});
}
is_error
}
Expand Down Expand Up @@ -1278,9 +1299,12 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
let kind = self.tcx.def_descr(def_id);
let sess = self.tcx.sess;
let _ = match name {
Some(name) => {
sess.dcx().emit_err(ItemIsPrivate { span, kind, descr: (&name).into() })
}
Some(name) => sess.dcx().emit_err(ItemIsPrivate {
span,
kind,
descr: (&name).into(),
pat_span: None,
}),
None => sess.dcx().emit_err(UnnamedItemIsPrivate { span, kind }),
};
return;
Expand Down Expand Up @@ -1776,7 +1800,8 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
// Check privacy of explicitly written types and traits as well as
// inferred types of expressions and patterns.
let span = tcx.def_span(module_def_id);
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span };
let mut visitor =
TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span, hir_id: None };

let module = tcx.hir_module_items(module_def_id);
for def_id in module.definitions() {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/privacy/private-pat-field-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod foo {
struct Bar;
pub enum Foo {
#[allow(private_interfaces)]
A(Bar),
}
}
fn foo_bar(v: foo::Foo) {
match v {
foo::Foo::A(_) => {} //~ ERROR type `Bar` is private
}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/privacy/private-pat-field-types.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: type `Bar` is private
--> $DIR/private-pat-field-types.rs:10:21
|
LL | foo::Foo::A(_) => {}
| ^ private type
|
help: consider using `..` to ignore the field with private type
|
LL - foo::Foo::A(_) => {}
LL + foo::Foo::A(..) => {}
|

error: aborting due to 1 previous error

Loading