Skip to content

Commit 122d917

Browse files
committed
hir_id to accesslevel in resolve and applied in privacy
1 parent a508006 commit 122d917

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,8 @@ impl<'tcx> TyCtxt<'tcx> {
11611161
)
11621162
}
11631163

1164-
pub fn get_resolver_access_level(self, def_id: DefId) -> Option<AccessLevel> {
1165-
self.gcx.untracked_resolutions.access_levels.get(&def_id).copied()
1164+
pub fn get_resolver_access_level(self, hir_id: HirId) -> Option<AccessLevel> {
1165+
self.gcx.untracked_resolutions.access_levels.get(&hir_id).copied()
11661166
}
11671167

11681168
pub fn lift<T: Lift<'tcx>>(self, value: T) -> Option<T::Lifted> {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub struct ResolverOutputs {
119119
pub definitions: rustc_hir::definitions::Definitions,
120120
pub cstore: Box<CrateStoreDyn>,
121121
pub visibilities: FxHashMap<LocalDefId, Visibility>,
122-
pub access_levels: FxHashMap<DefId, AccessLevel>,
122+
pub access_levels: FxHashMap<HirId, AccessLevel>,
123123
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
124124
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
125125
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,

compiler/rustc_privacy/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,13 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
681681
// all of the items of a mod in `visit_mod` looking for use statements, we handle
682682
// making sure that intermediate use statements have their visibilities updated here.
683683
hir::ItemKind::Use(ref path, ..) => {
684-
tracing::trace!("item: def_id={:?}, last_segment: {:?}", item.def_id.to_def_id(), path.segments.last());
684+
tracing::trace!(
685+
"item: def_id={:?}, last_segment: {:?}",
686+
item.def_id.to_def_id(),
687+
path.segments.last()
688+
);
685689
if item.vis.node.is_pub() {
686-
let access_level = self.tcx.get_resolver_access_level(item.def_id.to_def_id());
690+
let access_level = self.tcx.get_resolver_access_level(item.hir_id());
687691
self.update(item.hir_id(), access_level);
688692
}
689693
}

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
469469
prefix.is_empty() || prefix.len() == 1 && prefix[0].ident.name == kw::PathRoot
470470
};
471471
match use_tree.kind {
472-
ast::UseTreeKind::Simple(rename, ..) => {
472+
ast::UseTreeKind::Simple(rename, id1, id2) => {
473473
let mut ident = use_tree.ident();
474474
let mut module_path = prefix;
475475
let mut source = module_path.pop().unwrap();
@@ -579,7 +579,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
579579
},
580580
type_ns_only,
581581
nested,
582+
additional_ids: (id1, id2),
582583
};
584+
583585
self.add_import(
584586
module_path,
585587
kind,

compiler/rustc_resolve/src/imports.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum ImportKind<'a> {
5050
type_ns_only: bool,
5151
/// Did this import result from a nested import? ie. `use foo::{bar, baz};`
5252
nested: bool,
53+
additional_ids: (NodeId, NodeId),
5354
},
5455
Glob {
5556
is_prelude: bool,
@@ -838,7 +839,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
838839
import.span,
839840
);
840841
import.vis.set(orig_vis);
841-
tracing::trace!("resolve_import: determinacy: {:?}", binding.map(|binding| binding.span));
842+
tracing::trace!(
843+
"resolve_import: determinacy: {:?}",
844+
binding.map(|binding| binding.span)
845+
);
842846
source_bindings[ns].set(binding);
843847
} else {
844848
return;

compiler/rustc_resolve/src/lib.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ pub struct Resolver<'a> {
10341034

10351035
main_def: Option<MainDefinition>,
10361036

1037-
node_privacy: FxHashMap<DefId, AccessLevel>
1037+
node_privacy: FxHashMap<HirId, AccessLevel>,
10381038
}
10391039

10401040
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1535,40 +1535,64 @@ impl<'a> Resolver<'a> {
15351535

15361536
if let Some(def_id) = root.def_id() {
15371537
if let Some(exports) = self.export_map.get(&def_id.expect_local()).cloned() {
1538-
tracing::trace!("exports={:?}", exports);
1539-
let public_exports = exports.iter().filter(|ex| ex.vis == Visibility::Public).collect::<Vec<_>>();
1538+
let public_exports =
1539+
exports.iter().filter(|ex| ex.vis == Visibility::Public).collect::<Vec<_>>();
15401540
for export in public_exports.into_iter() {
1541-
self.per_ns(|this, ns| {
1542-
let new_key = this.new_key(export.ident, ns);
1543-
let name_res = this.resolution(root, new_key);
1541+
if let Some(ns) = export.res.ns() {
1542+
let key = self.new_key(export.ident, ns);
1543+
let name_res = self.resolution(root, key);
15441544
if let Some(binding) = name_res.borrow().binding() {
1545-
this.recursive_define_access_level(binding, AccessLevel::Public, 30);
1545+
self.recursive_define_access_level(binding, AccessLevel::Public, 30);
15461546
}
1547-
});
1547+
}
15481548
}
15491549
}
15501550
}
15511551

15521552
tracing::info!("node_privacy: {:#?}", self.node_privacy);
15531553
}
15541554

1555-
fn recursive_define_access_level(&mut self, binding: &NameBinding<'a>, access_level: AccessLevel, max_recurse: usize) {
1555+
fn recursive_define_access_level(
1556+
&mut self,
1557+
binding: &NameBinding<'a>,
1558+
access_level: AccessLevel,
1559+
max_recurse: usize,
1560+
) {
15561561
// Is this useful in case of very very veryyyyyy deep nesting?
15571562
if max_recurse == 0 {
15581563
return;
15591564
}
15601565

15611566
if let NameBindingKind::Import { binding, import, .. } = binding.kind {
1562-
let def_id = self.opt_local_def_id(import.id).map(|local_def_id| local_def_id.to_def_id());
1563-
if let Some(def_id) = def_id {
1564-
tracing::trace!("binding found! import.id={:?} def_id={:?}", import.id, def_id);
1565-
self.node_privacy.insert(def_id, access_level);
1566-
}
1567+
tracing::trace!(
1568+
"binding found! import.id={:?}, import.root_id={:?}, res={:?}",
1569+
import.id,
1570+
import.root_id,
1571+
binding.res()
1572+
);
1573+
self.mark_node_with_access_level(import.id, access_level);
1574+
match import.kind {
1575+
ImportKind::Single { additional_ids, .. } => {
1576+
self.mark_node_with_access_level(additional_ids.0, access_level);
1577+
self.mark_node_with_access_level(additional_ids.1, access_level);
1578+
}
1579+
_ => {}
1580+
};
15671581

15681582
self.recursive_define_access_level(binding, AccessLevel::Exported, max_recurse - 1);
15691583
}
15701584
}
15711585

1586+
fn mark_node_with_access_level(&mut self, node_id: NodeId, access_level: AccessLevel) -> bool {
1587+
if let Some(local_def_id) = self.opt_local_def_id(node_id) {
1588+
if let Some(hir_id) = self.definitions().def_id_to_hir_id.get(local_def_id.to_def_id()) {
1589+
self.node_privacy.insert(hir_id, access_level).is_none();
1590+
}
1591+
} else {
1592+
false
1593+
}
1594+
}
1595+
15721596
pub fn traits_in_scope(
15731597
&mut self,
15741598
current_trait: Option<Module<'a>>,

0 commit comments

Comments
 (0)