Skip to content

Commit 15dbdab

Browse files
committed
privacy: Refactor top-level visiting in NamePrivacyVisitor
1 parent 5bd5d21 commit 15dbdab

File tree

1 file changed

+16
-43
lines changed
  • compiler/rustc_privacy/src

1 file changed

+16
-43
lines changed

compiler/rustc_privacy/src/lib.rs

+16-43
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_hir::def::{DefKind, Res};
2424
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, CRATE_DEF_ID};
2525
use rustc_hir::intravisit::{self, Visitor};
2626
use rustc_hir::{AssocItemKind, ForeignItemKind, ItemId, PatKind};
27-
use rustc_middle::hir::nested_filter;
2827
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level};
2928
use rustc_middle::query::Providers;
3029
use rustc_middle::ty::GenericArgs;
@@ -36,9 +35,9 @@ use rustc_span::hygiene::Transparency;
3635
use rustc_span::symbol::{kw, sym, Ident};
3736
use rustc_span::Span;
3837

38+
use std::fmt;
3939
use std::marker::PhantomData;
4040
use std::ops::ControlFlow;
41-
use std::{fmt, mem};
4241

4342
use errors::{
4443
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
@@ -935,7 +934,6 @@ impl<'tcx, 'a> Visitor<'tcx> for TestReachabilityVisitor<'tcx, 'a> {
935934
struct NamePrivacyVisitor<'tcx> {
936935
tcx: TyCtxt<'tcx>,
937936
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
938-
current_item: LocalDefId,
939937
}
940938

941939
impl<'tcx> NamePrivacyVisitor<'tcx> {
@@ -951,6 +949,7 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
951949
// Checks that a field in a struct constructor (expression or pattern) is accessible.
952950
fn check_field(
953951
&mut self,
952+
hir_id: hir::HirId, // ID of the field use
954953
use_ctxt: Span, // syntax context of the field name at the use site
955954
span: Span, // span of the field pattern, e.g., `x: 0`
956955
def: ty::AdtDef<'tcx>, // definition of the struct or enum
@@ -963,7 +962,6 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
963962

964963
// definition of the field
965964
let ident = Ident::new(kw::Empty, use_ctxt);
966-
let hir_id = self.tcx.local_def_id_to_hir_id(self.current_item);
967965
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id).1;
968966
if !field.vis.is_accessible_from(def_id, self.tcx) {
969967
self.tcx.dcx().emit_err(FieldIsPrivate {
@@ -982,33 +980,13 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
982980
}
983981

984982
impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
985-
type NestedFilter = nested_filter::All;
986-
987-
/// We want to visit items in the context of their containing
988-
/// module and so forth, so supply a crate for doing a deep walk.
989-
fn nested_visit_map(&mut self) -> Self::Map {
990-
self.tcx.hir()
991-
}
992-
993-
fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _s: Span, _n: hir::HirId) {
994-
// Don't visit nested modules, since we run a separate visitor walk
995-
// for each module in `effective_visibilities`
996-
}
997-
998-
fn visit_nested_body(&mut self, body: hir::BodyId) {
983+
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
999984
let old_maybe_typeck_results =
1000-
self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
1001-
let body = self.tcx.hir().body(body);
1002-
self.visit_body(body);
985+
self.maybe_typeck_results.replace(self.tcx.typeck_body(body_id));
986+
self.visit_body(self.tcx.hir().body(body_id));
1003987
self.maybe_typeck_results = old_maybe_typeck_results;
1004988
}
1005989

1006-
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
1007-
let orig_current_item = mem::replace(&mut self.current_item, item.owner_id.def_id);
1008-
intravisit::walk_item(self, item);
1009-
self.current_item = orig_current_item;
1010-
}
1011-
1012990
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
1013991
if let hir::ExprKind::Struct(qpath, fields, ref base) = expr.kind {
1014992
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
@@ -1022,17 +1000,17 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
10221000
let field = fields
10231001
.iter()
10241002
.find(|f| self.typeck_results().field_index(f.hir_id) == vf_index);
1025-
let (use_ctxt, span) = match field {
1026-
Some(field) => (field.ident.span, field.span),
1027-
None => (base.span, base.span),
1003+
let (hir_id, use_ctxt, span) = match field {
1004+
Some(field) => (field.hir_id, field.ident.span, field.span),
1005+
None => (base.hir_id, base.span, base.span),
10281006
};
1029-
self.check_field(use_ctxt, span, adt, variant_field, true);
1007+
self.check_field(hir_id, use_ctxt, span, adt, variant_field, true);
10301008
}
10311009
} else {
10321010
for field in fields {
1033-
let use_ctxt = field.ident.span;
1011+
let (hir_id, use_ctxt, span) = (field.hir_id, field.ident.span, field.span);
10341012
let index = self.typeck_results().field_index(field.hir_id);
1035-
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
1013+
self.check_field(hir_id, use_ctxt, span, adt, &variant.fields[index], false);
10361014
}
10371015
}
10381016
}
@@ -1046,9 +1024,9 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
10461024
let adt = self.typeck_results().pat_ty(pat).ty_adt_def().unwrap();
10471025
let variant = adt.variant_of_res(res);
10481026
for field in fields {
1049-
let use_ctxt = field.ident.span;
1027+
let (hir_id, use_ctxt, span) = (field.hir_id, field.ident.span, field.span);
10501028
let index = self.typeck_results().field_index(field.hir_id);
1051-
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
1029+
self.check_field(hir_id, use_ctxt, span, adt, &variant.fields[index], false);
10521030
}
10531031
}
10541032

@@ -1743,17 +1721,12 @@ pub fn provide(providers: &mut Providers) {
17431721

17441722
fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
17451723
// Check privacy of names not checked in previous compilation stages.
1746-
let mut visitor = NamePrivacyVisitor {
1747-
tcx,
1748-
maybe_typeck_results: None,
1749-
current_item: module_def_id.to_local_def_id(),
1750-
};
1751-
let (module, span, hir_id) = tcx.hir().get_module(module_def_id);
1752-
1753-
intravisit::walk_mod(&mut visitor, module, hir_id);
1724+
let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results: None };
1725+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut visitor);
17541726

17551727
// Check privacy of explicitly written types and traits as well as
17561728
// inferred types of expressions and patterns.
1729+
let span = tcx.def_span(module_def_id);
17571730
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span };
17581731
tcx.hir().visit_item_likes_in_module(module_def_id, &mut visitor);
17591732
}

0 commit comments

Comments
 (0)