Skip to content

resolve: More groundwork for item_like_imports (RFC 1560) #35776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add field current_vis to Resolver.
  • Loading branch information
jseyfried committed Aug 18, 2016
commit 89de52eff08d7416b9fd4ab0adc2e818590e84d0
7 changes: 6 additions & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl<'b> Resolver<'b> {
/// Constructs the reduced graph for one item.
fn build_reduced_graph_for_item(&mut self, item: &Item) {
let parent = self.current_module;
let parent_vis = self.current_vis;
let name = item.ident.name;
let sp = item.span;
let vis = self.resolve_visibility(&item.vis);
Expand Down Expand Up @@ -207,7 +208,10 @@ impl<'b> Resolver<'b> {
});
self.define(parent, name, TypeNS, (module, sp, vis));
self.module_map.insert(item.id, module);
self.current_module = module; // Descend into the module.

// Descend into the module.
self.current_module = module;
self.current_vis = ty::Visibility::Restricted(item.id);
}

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

visit::walk_item(&mut BuildReducedGraphVisitor { resolver: self }, item);
self.current_module = parent;
self.current_vis = parent_vis;
}

// Constructs the reduced graph for one variant. Variants exist in the
Expand Down
23 changes: 11 additions & 12 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,10 @@ pub struct Resolver<'a> {
// The module that represents the current item scope.
current_module: Module<'a>,

// The visibility of `pub(self)` items in the current scope.
// Equivalently, the visibility required for an item to be accessible from the current scope.
current_vis: ty::Visibility,

// The current set of local scopes, for values.
// FIXME #4948: Reuse ribs to avoid allocation.
value_ribs: Vec<Rib<'a>>,
Expand Down Expand Up @@ -1154,6 +1158,7 @@ impl<'a> Resolver<'a> {
indeterminate_imports: Vec::new(),

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

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

f(self);

self.current_module = orig_module;
self.current_vis = orig_vis;
self.value_ribs.pop();
self.type_ribs.pop();
} else {
Expand Down Expand Up @@ -2706,7 +2714,6 @@ impl<'a> Resolver<'a> {
fn with_empty_ribs<T, F>(&mut self, f: F) -> T
where F: FnOnce(&mut Resolver<'a>) -> T,
{
use ::std::mem::replace;
let value_ribs = replace(&mut self.value_ribs, Vec::new());
let type_ribs = replace(&mut self.type_ribs, Vec::new());
let label_ribs = replace(&mut self.label_ribs, Vec::new());
Expand Down Expand Up @@ -3264,13 +3271,7 @@ impl<'a> Resolver<'a> {
ast::Visibility::Public => return ty::Visibility::Public,
ast::Visibility::Crate(_) => return ty::Visibility::Restricted(ast::CRATE_NODE_ID),
ast::Visibility::Restricted { ref path, id } => (path, id),
ast::Visibility::Inherited => {
let current_module =
self.get_nearest_normal_module_parent_or_self(self.current_module);
let id =
self.definitions.as_local_node_id(current_module.def_id().unwrap()).unwrap();
return ty::Visibility::Restricted(id);
}
ast::Visibility::Inherited => return self.current_vis,
};

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

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

fn check_privacy(&mut self, name: Name, binding: &'a NameBinding<'a>, span: Span) {
Expand Down
10 changes: 9 additions & 1 deletion src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
// remain or unsuccessfully when no forward progress in resolving imports
// is made.

fn set_current_module(&mut self, module: Module<'b>) {
self.current_module = module;
self.current_vis = ty::Visibility::Restricted({
let normal_module = self.get_nearest_normal_module_parent_or_self(module);
self.definitions.as_local_node_id(normal_module.def_id().unwrap()).unwrap()
});
}

/// Resolves all imports for the crate. This method performs the fixed-
/// point iteration.
fn resolve_imports(&mut self) {
Expand Down Expand Up @@ -449,7 +457,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
module_to_string(self.current_module));

let module = directive.parent;
self.current_module = module;
self.set_current_module(module);

let target_module = match directive.target_module.get() {
Some(module) => module,
Expand Down