Fixup code

This commit is contained in:
Aaron Hill 2019-01-13 18:07:42 -05:00
parent 09ff0bacef
commit afb1921c79
No known key found for this signature in database
GPG Key ID: B4087E510E98B164
2 changed files with 21 additions and 84 deletions

View File

@ -2878,6 +2878,7 @@ dependencies = [
name = "rustc_privacy"
version = "0.0.0"
dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0",
"rustc_data_structures 0.0.0",
"rustc_typeck 0.0.0",

View File

@ -1495,6 +1495,16 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
}
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
if self.leaks_private_dep(def_id) {
self.tcx.lint_node(lint::builtin::LEAKED_PRIVATE_DEPENDENCY,
self.item_id,
self.span,
&format!("{} `{}` from private dependency '{}' in public \
interface", kind, descr,
self.tcx.crate_name(def_id.krate)));
}
let node_id = match self.tcx.hir().as_local_node_id(def_id) {
Some(node_id) => node_id,
None => return false,
@ -1520,16 +1530,7 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
}
if self.leaks_private_dep(trait_ref.def_id) {
self.tcx.lint_node(lint::builtin::LEAKED_PRIVATE_DEPENDENCY,
self.item_id,
self.span,
&format!("trait `{}` from private dependency '{}' in public \
interface", trait_ref,
self.tcx.crate_name(trait_ref.def_id.krate)));
}
false
}
/// An item is 'leaked' from a private dependency if all
@ -1551,80 +1552,6 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
}
}
impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
let ty_def_id = match ty.sty {
ty::Adt(adt, _) => Some(adt.did),
ty::Foreign(did) => Some(did),
ty::Dynamic(ref obj, ..) => Some(obj.principal().def_id()),
ty::Projection(ref proj) => {
if self.required_visibility == ty::Visibility::Invisible {
// Conservatively approximate the whole type alias as public without
// recursing into its components when determining impl publicity.
// For example, `impl <Type as Trait>::Alias {...}` may be a public impl
// even if both `Type` and `Trait` are private.
// Ideally, associated types should be substituted in the same way as
// free type aliases, but this isn't done yet.
return false;
}
let trait_ref = proj.trait_ref(self.tcx);
Some(trait_ref.def_id)
}
_ => None
};
if let Some(def_id) = ty_def_id {
// Non-local means public (private items can't leave their crate, modulo bugs).
if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
let hir_vis = match self.tcx.hir().find(node_id) {
Some(Node::Item(item)) => &item.vis,
Some(Node::ForeignItem(item)) => &item.vis,
_ => bug!("expected item of foreign item"),
};
let vis = ty::Visibility::from_hir(hir_vis, node_id, self.tcx);
if !vis.is_at_least(self.min_visibility, self.tcx) {
self.min_visibility = vis;
}
if !vis.is_at_least(self.required_visibility, self.tcx) {
let vis_adj = match hir_vis.node {
hir::VisibilityKind::Crate(_) => "crate-visible",
hir::VisibilityKind::Restricted { .. } => "restricted",
_ => "private"
};
if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
let mut err = struct_span_err!(self.tcx.sess, self.span, E0446,
"{} type `{}` in public interface", vis_adj, ty);
err.span_label(self.span, format!("can't leak {} type", vis_adj));
err.span_label(hir_vis.span, format!("`{}` declared as {}", ty, vis_adj));
err.emit();
} else {
self.tcx.lint_node(lint::builtin::PRIVATE_IN_PUBLIC,
node_id,
self.span,
&format!("{} type `{}` in public \
interface (error E0446)", vis_adj, ty));
}
}
}
if self.leaks_private_dep(def_id) {
self.tcx.lint_node(lint::builtin::LEAKED_PRIVATE_DEPENDENCY,
self.item_id,
self.span,
&format!("type '{}' from private dependency '{}' in \
public interface", ty,
self.tcx.crate_name(def_id.krate)));
}
}
ty.super_visit_with(self)
}
}
/*struct LeakedPrivateDependenciesVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@ -1649,6 +1576,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LeakedPrivateDependenciesVisitor<'a, 'tcx> {
}*/
impl<'a, 'tcx> DefIdVisitor<'a, 'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
fn visit_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
self.check_def_id(def_id, kind, descr)
}
}
struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
has_pub_restricted: bool,