Skip to content

Commit c48c77e

Browse files
authored
Rollup merge of #120339 - petrochenkov:nameprivisit, r=michaelwoerister
privacy: Refactor top-level visiting in `NamePrivacyVisitor` Full hierarchical visiting (`nested_filter::All`) is not necessary, visiting all item-likes in isolation is enough. Tracking current item is not necessary, passing any `HirId` with the same parent module to `adjust_ident_and_get_scope` is enough. Follow up to #120284.
2 parents 5f1f617 + 15dbdab commit c48c77e

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
@@ -22,7 +22,6 @@ use rustc_hir::def::{DefKind, Res};
2222
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, CRATE_DEF_ID};
2323
use rustc_hir::intravisit::{self, Visitor};
2424
use rustc_hir::{AssocItemKind, ForeignItemKind, ItemId, PatKind};
25-
use rustc_middle::hir::nested_filter;
2625
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level};
2726
use rustc_middle::query::Providers;
2827
use rustc_middle::ty::GenericArgs;
@@ -34,9 +33,9 @@ use rustc_span::hygiene::Transparency;
3433
use rustc_span::symbol::{kw, sym, Ident};
3534
use rustc_span::Span;
3635

36+
use std::fmt;
3737
use std::marker::PhantomData;
3838
use std::ops::ControlFlow;
39-
use std::{fmt, mem};
4039

4140
use errors::{
4241
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
@@ -933,7 +932,6 @@ impl<'tcx, 'a> Visitor<'tcx> for TestReachabilityVisitor<'tcx, 'a> {
933932
struct NamePrivacyVisitor<'tcx> {
934933
tcx: TyCtxt<'tcx>,
935934
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
936-
current_item: LocalDefId,
937935
}
938936

939937
impl<'tcx> NamePrivacyVisitor<'tcx> {
@@ -949,6 +947,7 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
949947
// Checks that a field in a struct constructor (expression or pattern) is accessible.
950948
fn check_field(
951949
&mut self,
950+
hir_id: hir::HirId, // ID of the field use
952951
use_ctxt: Span, // syntax context of the field name at the use site
953952
span: Span, // span of the field pattern, e.g., `x: 0`
954953
def: ty::AdtDef<'tcx>, // definition of the struct or enum
@@ -961,7 +960,6 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
961960

962961
// definition of the field
963962
let ident = Ident::new(kw::Empty, use_ctxt);
964-
let hir_id = self.tcx.local_def_id_to_hir_id(self.current_item);
965963
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id).1;
966964
if !field.vis.is_accessible_from(def_id, self.tcx) {
967965
self.tcx.dcx().emit_err(FieldIsPrivate {
@@ -980,33 +978,13 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
980978
}
981979

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

1004-
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
1005-
let orig_current_item = mem::replace(&mut self.current_item, item.owner_id.def_id);
1006-
intravisit::walk_item(self, item);
1007-
self.current_item = orig_current_item;
1008-
}
1009-
1010988
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
1011989
if let hir::ExprKind::Struct(qpath, fields, ref base) = expr.kind {
1012990
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
@@ -1020,17 +998,17 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
1020998
let field = fields
1021999
.iter()
10221000
.find(|f| self.typeck_results().field_index(f.hir_id) == vf_index);
1023-
let (use_ctxt, span) = match field {
1024-
Some(field) => (field.ident.span, field.span),
1025-
None => (base.span, base.span),
1001+
let (hir_id, use_ctxt, span) = match field {
1002+
Some(field) => (field.hir_id, field.ident.span, field.span),
1003+
None => (base.hir_id, base.span, base.span),
10261004
};
1027-
self.check_field(use_ctxt, span, adt, variant_field, true);
1005+
self.check_field(hir_id, use_ctxt, span, adt, variant_field, true);
10281006
}
10291007
} else {
10301008
for field in fields {
1031-
let use_ctxt = field.ident.span;
1009+
let (hir_id, use_ctxt, span) = (field.hir_id, field.ident.span, field.span);
10321010
let index = self.typeck_results().field_index(field.hir_id);
1033-
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
1011+
self.check_field(hir_id, use_ctxt, span, adt, &variant.fields[index], false);
10341012
}
10351013
}
10361014
}
@@ -1044,9 +1022,9 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
10441022
let adt = self.typeck_results().pat_ty(pat).ty_adt_def().unwrap();
10451023
let variant = adt.variant_of_res(res);
10461024
for field in fields {
1047-
let use_ctxt = field.ident.span;
1025+
let (hir_id, use_ctxt, span) = (field.hir_id, field.ident.span, field.span);
10481026
let index = self.typeck_results().field_index(field.hir_id);
1049-
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
1027+
self.check_field(hir_id, use_ctxt, span, adt, &variant.fields[index], false);
10501028
}
10511029
}
10521030

@@ -1741,17 +1719,12 @@ pub fn provide(providers: &mut Providers) {
17411719

17421720
fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
17431721
// Check privacy of names not checked in previous compilation stages.
1744-
let mut visitor = NamePrivacyVisitor {
1745-
tcx,
1746-
maybe_typeck_results: None,
1747-
current_item: module_def_id.to_local_def_id(),
1748-
};
1749-
let (module, span, hir_id) = tcx.hir().get_module(module_def_id);
1750-
1751-
intravisit::walk_mod(&mut visitor, module, hir_id);
1722+
let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results: None };
1723+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut visitor);
17521724

17531725
// Check privacy of explicitly written types and traits as well as
17541726
// inferred types of expressions and patterns.
1727+
let span = tcx.def_span(module_def_id);
17551728
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span };
17561729
tcx.hir().visit_item_likes_in_module(module_def_id, &mut visitor);
17571730
}

0 commit comments

Comments
 (0)