Skip to content

Commit b01976a

Browse files
committed
Precompute ancestors when checking privacy
Precompute ancestors of the old error node set so that check for private types and traits in public interfaces can in constant time determine if the current item has any descendants in the old error set. No functional changes intended.
1 parent fd20a8b commit b01976a

File tree

1 file changed

+21
-33
lines changed
  • compiler/rustc_privacy/src

1 file changed

+21
-33
lines changed

compiler/rustc_privacy/src/lib.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -1822,49 +1822,26 @@ impl DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> {
18221822
}
18231823
}
18241824

1825-
struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
1825+
struct PrivateItemsInPublicInterfacesVisitor<'tcx> {
18261826
tcx: TyCtxt<'tcx>,
18271827
has_pub_restricted: bool,
1828-
old_error_set: &'a HirIdSet,
1828+
old_error_set_ancestry: HirIdSet,
18291829
}
18301830

1831-
impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
1831+
impl<'tcx> PrivateItemsInPublicInterfacesVisitor<'tcx> {
18321832
fn check(
18331833
&self,
18341834
item_id: hir::HirId,
18351835
required_visibility: ty::Visibility,
18361836
) -> SearchInterfaceForPrivateItemsVisitor<'tcx> {
1837-
let mut has_old_errors = false;
1838-
1839-
// Slow path taken only if there any errors in the crate.
1840-
for &id in self.old_error_set {
1841-
// Walk up the nodes until we find `item_id` (or we hit a root).
1842-
let mut id = id;
1843-
loop {
1844-
if id == item_id {
1845-
has_old_errors = true;
1846-
break;
1847-
}
1848-
let parent = self.tcx.hir().get_parent_node(id);
1849-
if parent == id {
1850-
break;
1851-
}
1852-
id = parent;
1853-
}
1854-
1855-
if has_old_errors {
1856-
break;
1857-
}
1858-
}
1859-
18601837
SearchInterfaceForPrivateItemsVisitor {
18611838
tcx: self.tcx,
18621839
item_id,
18631840
item_def_id: self.tcx.hir().local_def_id(item_id).to_def_id(),
18641841
span: self.tcx.hir().span(item_id),
18651842
required_visibility,
18661843
has_pub_restricted: self.has_pub_restricted,
1867-
has_old_errors,
1844+
has_old_errors: self.old_error_set_ancestry.contains(&item_id),
18681845
in_assoc_ty: false,
18691846
}
18701847
}
@@ -1890,7 +1867,7 @@ impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
18901867
}
18911868
}
18921869

1893-
impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
1870+
impl<'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'tcx> {
18941871
type Map = Map<'tcx>;
18951872

18961873
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
@@ -2114,11 +2091,22 @@ fn check_private_in_public(tcx: TyCtxt<'_>, krate: CrateNum) {
21142091
pub_restricted_visitor.has_pub_restricted
21152092
};
21162093

2094+
let mut old_error_set_ancestry = HirIdSet::default();
2095+
for mut id in visitor.old_error_set.iter().copied() {
2096+
loop {
2097+
if !old_error_set_ancestry.insert(id) {
2098+
break;
2099+
}
2100+
let parent = tcx.hir().get_parent_node(id);
2101+
if parent == id {
2102+
break;
2103+
}
2104+
id = parent;
2105+
}
2106+
}
2107+
21172108
// Check for private types and traits in public interfaces.
2118-
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
2119-
tcx,
2120-
has_pub_restricted,
2121-
old_error_set: &visitor.old_error_set,
2122-
};
2109+
let mut visitor =
2110+
PrivateItemsInPublicInterfacesVisitor { tcx, has_pub_restricted, old_error_set_ancestry };
21232111
krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
21242112
}

0 commit comments

Comments
 (0)