Skip to content

Commit 89de52e

Browse files
committed
Add field current_vis to Resolver.
1 parent c1362d8 commit 89de52e

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl<'b> Resolver<'b> {
8181
/// Constructs the reduced graph for one item.
8282
fn build_reduced_graph_for_item(&mut self, item: &Item) {
8383
let parent = self.current_module;
84+
let parent_vis = self.current_vis;
8485
let name = item.ident.name;
8586
let sp = item.span;
8687
let vis = self.resolve_visibility(&item.vis);
@@ -207,7 +208,10 @@ impl<'b> Resolver<'b> {
207208
});
208209
self.define(parent, name, TypeNS, (module, sp, vis));
209210
self.module_map.insert(item.id, module);
210-
self.current_module = module; // Descend into the module.
211+
212+
// Descend into the module.
213+
self.current_module = module;
214+
self.current_vis = ty::Visibility::Restricted(item.id);
211215
}
212216

213217
ItemKind::ForeignMod(..) => {}
@@ -303,6 +307,7 @@ impl<'b> Resolver<'b> {
303307

304308
visit::walk_item(&mut BuildReducedGraphVisitor { resolver: self }, item);
305309
self.current_module = parent;
310+
self.current_vis = parent_vis;
306311
}
307312

308313
// Constructs the reduced graph for one variant. Variants exist in the

src/librustc_resolve/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,10 @@ pub struct Resolver<'a> {
969969
// The module that represents the current item scope.
970970
current_module: Module<'a>,
971971

972+
// The visibility of `pub(self)` items in the current scope.
973+
// Equivalently, the visibility required for an item to be accessible from the current scope.
974+
current_vis: ty::Visibility,
975+
972976
// The current set of local scopes, for values.
973977
// FIXME #4948: Reuse ribs to avoid allocation.
974978
value_ribs: Vec<Rib<'a>>,
@@ -1154,6 +1158,7 @@ impl<'a> Resolver<'a> {
11541158
indeterminate_imports: Vec::new(),
11551159

11561160
current_module: graph_root,
1161+
current_vis: ty::Visibility::Restricted(ast::CRATE_NODE_ID),
11571162
value_ribs: vec![Rib::new(ModuleRibKind(graph_root))],
11581163
type_ribs: vec![Rib::new(ModuleRibKind(graph_root))],
11591164
label_ribs: Vec::new(),
@@ -1197,6 +1202,7 @@ impl<'a> Resolver<'a> {
11971202
/// Entry point to crate resolution.
11981203
pub fn resolve_crate(&mut self, krate: &Crate) {
11991204
self.current_module = self.graph_root;
1205+
self.current_vis = ty::Visibility::Restricted(ast::CRATE_NODE_ID);
12001206
visit::walk_crate(self, krate);
12011207

12021208
check_unused::check_crate(self, krate);
@@ -1562,13 +1568,15 @@ impl<'a> Resolver<'a> {
15621568
let module = self.module_map.get(&id).cloned(); // clones a reference
15631569
if let Some(module) = module {
15641570
// Move down in the graph.
1565-
let orig_module = ::std::mem::replace(&mut self.current_module, module);
1571+
let orig_module = replace(&mut self.current_module, module);
1572+
let orig_vis = replace(&mut self.current_vis, ty::Visibility::Restricted(id));
15661573
self.value_ribs.push(Rib::new(ModuleRibKind(module)));
15671574
self.type_ribs.push(Rib::new(ModuleRibKind(module)));
15681575

15691576
f(self);
15701577

15711578
self.current_module = orig_module;
1579+
self.current_vis = orig_vis;
15721580
self.value_ribs.pop();
15731581
self.type_ribs.pop();
15741582
} else {
@@ -2706,7 +2714,6 @@ impl<'a> Resolver<'a> {
27062714
fn with_empty_ribs<T, F>(&mut self, f: F) -> T
27072715
where F: FnOnce(&mut Resolver<'a>) -> T,
27082716
{
2709-
use ::std::mem::replace;
27102717
let value_ribs = replace(&mut self.value_ribs, Vec::new());
27112718
let type_ribs = replace(&mut self.type_ribs, Vec::new());
27122719
let label_ribs = replace(&mut self.label_ribs, Vec::new());
@@ -3264,13 +3271,7 @@ impl<'a> Resolver<'a> {
32643271
ast::Visibility::Public => return ty::Visibility::Public,
32653272
ast::Visibility::Crate(_) => return ty::Visibility::Restricted(ast::CRATE_NODE_ID),
32663273
ast::Visibility::Restricted { ref path, id } => (path, id),
3267-
ast::Visibility::Inherited => {
3268-
let current_module =
3269-
self.get_nearest_normal_module_parent_or_self(self.current_module);
3270-
let id =
3271-
self.definitions.as_local_node_id(current_module.def_id().unwrap()).unwrap();
3272-
return ty::Visibility::Restricted(id);
3273-
}
3274+
ast::Visibility::Inherited => return self.current_vis,
32743275
};
32753276

32763277
let segments: Vec<_> = path.segments.iter().map(|seg| seg.identifier.name).collect();
@@ -3299,9 +3300,7 @@ impl<'a> Resolver<'a> {
32993300
}
33003301

33013302
fn is_accessible(&self, vis: ty::Visibility) -> bool {
3302-
let current_module = self.get_nearest_normal_module_parent_or_self(self.current_module);
3303-
let node_id = self.definitions.as_local_node_id(current_module.def_id().unwrap()).unwrap();
3304-
vis.is_accessible_from(node_id, self)
3303+
vis.is_at_least(self.current_vis, self)
33053304
}
33063305

33073306
fn check_privacy(&mut self, name: Name, binding: &'a NameBinding<'a>, span: Span) {

src/librustc_resolve/resolve_imports.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
356356
// remain or unsuccessfully when no forward progress in resolving imports
357357
// is made.
358358

359+
fn set_current_module(&mut self, module: Module<'b>) {
360+
self.current_module = module;
361+
self.current_vis = ty::Visibility::Restricted({
362+
let normal_module = self.get_nearest_normal_module_parent_or_self(module);
363+
self.definitions.as_local_node_id(normal_module.def_id().unwrap()).unwrap()
364+
});
365+
}
366+
359367
/// Resolves all imports for the crate. This method performs the fixed-
360368
/// point iteration.
361369
fn resolve_imports(&mut self) {
@@ -449,7 +457,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
449457
module_to_string(self.current_module));
450458

451459
let module = directive.parent;
452-
self.current_module = module;
460+
self.set_current_module(module);
453461

454462
let target_module = match directive.target_module.get() {
455463
Some(module) => module,

0 commit comments

Comments
 (0)