Skip to content

Commit afb1921

Browse files
committed
Fixup code
1 parent 09ff0ba commit afb1921

File tree

2 files changed

+21
-84
lines changed

2 files changed

+21
-84
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,7 @@ dependencies = [
28782878
name = "rustc_privacy"
28792879
version = "0.0.0"
28802880
dependencies = [
2881+
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
28812882
"rustc 0.0.0",
28822883
"rustc_data_structures 0.0.0",
28832884
"rustc_typeck 0.0.0",

src/librustc_privacy/lib.rs

Lines changed: 20 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,16 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
14951495
}
14961496

14971497
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
1498+
if self.leaks_private_dep(def_id) {
1499+
self.tcx.lint_node(lint::builtin::LEAKED_PRIVATE_DEPENDENCY,
1500+
self.item_id,
1501+
self.span,
1502+
&format!("{} `{}` from private dependency '{}' in public \
1503+
interface", kind, descr,
1504+
self.tcx.crate_name(def_id.krate)));
1505+
1506+
}
1507+
14981508
let node_id = match self.tcx.hir().as_local_node_id(def_id) {
14991509
Some(node_id) => node_id,
15001510
None => return false,
@@ -1520,16 +1530,7 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
15201530

15211531
}
15221532

1523-
if self.leaks_private_dep(trait_ref.def_id) {
1524-
self.tcx.lint_node(lint::builtin::LEAKED_PRIVATE_DEPENDENCY,
1525-
self.item_id,
1526-
self.span,
1527-
&format!("trait `{}` from private dependency '{}' in public \
1528-
interface", trait_ref,
1529-
self.tcx.crate_name(trait_ref.def_id.krate)));
1530-
1531-
}
1532-
1533+
false
15331534
}
15341535

15351536
/// An item is 'leaked' from a private dependency if all
@@ -1551,80 +1552,6 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
15511552
}
15521553
}
15531554

1554-
impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
1555-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
1556-
let ty_def_id = match ty.sty {
1557-
ty::Adt(adt, _) => Some(adt.did),
1558-
ty::Foreign(did) => Some(did),
1559-
ty::Dynamic(ref obj, ..) => Some(obj.principal().def_id()),
1560-
ty::Projection(ref proj) => {
1561-
if self.required_visibility == ty::Visibility::Invisible {
1562-
// Conservatively approximate the whole type alias as public without
1563-
// recursing into its components when determining impl publicity.
1564-
// For example, `impl <Type as Trait>::Alias {...}` may be a public impl
1565-
// even if both `Type` and `Trait` are private.
1566-
// Ideally, associated types should be substituted in the same way as
1567-
// free type aliases, but this isn't done yet.
1568-
return false;
1569-
}
1570-
let trait_ref = proj.trait_ref(self.tcx);
1571-
Some(trait_ref.def_id)
1572-
}
1573-
_ => None
1574-
};
1575-
1576-
if let Some(def_id) = ty_def_id {
1577-
// Non-local means public (private items can't leave their crate, modulo bugs).
1578-
if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
1579-
let hir_vis = match self.tcx.hir().find(node_id) {
1580-
Some(Node::Item(item)) => &item.vis,
1581-
Some(Node::ForeignItem(item)) => &item.vis,
1582-
_ => bug!("expected item of foreign item"),
1583-
};
1584-
1585-
let vis = ty::Visibility::from_hir(hir_vis, node_id, self.tcx);
1586-
1587-
if !vis.is_at_least(self.min_visibility, self.tcx) {
1588-
self.min_visibility = vis;
1589-
}
1590-
if !vis.is_at_least(self.required_visibility, self.tcx) {
1591-
let vis_adj = match hir_vis.node {
1592-
hir::VisibilityKind::Crate(_) => "crate-visible",
1593-
hir::VisibilityKind::Restricted { .. } => "restricted",
1594-
_ => "private"
1595-
};
1596-
1597-
if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
1598-
let mut err = struct_span_err!(self.tcx.sess, self.span, E0446,
1599-
"{} type `{}` in public interface", vis_adj, ty);
1600-
err.span_label(self.span, format!("can't leak {} type", vis_adj));
1601-
err.span_label(hir_vis.span, format!("`{}` declared as {}", ty, vis_adj));
1602-
err.emit();
1603-
} else {
1604-
self.tcx.lint_node(lint::builtin::PRIVATE_IN_PUBLIC,
1605-
node_id,
1606-
self.span,
1607-
&format!("{} type `{}` in public \
1608-
interface (error E0446)", vis_adj, ty));
1609-
}
1610-
}
1611-
1612-
}
1613-
1614-
if self.leaks_private_dep(def_id) {
1615-
self.tcx.lint_node(lint::builtin::LEAKED_PRIVATE_DEPENDENCY,
1616-
self.item_id,
1617-
self.span,
1618-
&format!("type '{}' from private dependency '{}' in \
1619-
public interface", ty,
1620-
self.tcx.crate_name(def_id.krate)));
1621-
}
1622-
1623-
}
1624-
1625-
ty.super_visit_with(self)
1626-
}
1627-
}
16281555

16291556
/*struct LeakedPrivateDependenciesVisitor<'a, 'tcx: 'a> {
16301557
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -1649,6 +1576,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LeakedPrivateDependenciesVisitor<'a, 'tcx> {
16491576
16501577
}*/
16511578

1579+
1580+
1581+
impl<'a, 'tcx> DefIdVisitor<'a, 'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
1582+
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
1583+
fn visit_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
1584+
self.check_def_id(def_id, kind, descr)
1585+
}
1586+
}
1587+
16521588
struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
16531589
tcx: TyCtxt<'a, 'tcx, 'tcx>,
16541590
has_pub_restricted: bool,

0 commit comments

Comments
 (0)