Skip to content

Commit 8b1dcf4

Browse files
committed
Auto merge of #33232 - jseyfried:resolve_ast_groundwork, r=nrc
resolve: cleanup and groundwork for resolving the AST Cleanup `resolve` and refactor away uses of the hir map (incorrectly named `ast_map`). r? @nrc
2 parents e07e34c + ac26419 commit 8b1dcf4

File tree

4 files changed

+132
-188
lines changed

4 files changed

+132
-188
lines changed

src/librustc/ty/mod.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,24 @@ pub enum Visibility {
283283
PrivateExternal,
284284
}
285285

286+
pub trait NodeIdTree {
287+
fn is_descendant_of(&self, node: NodeId, ancestor: NodeId) -> bool;
288+
}
289+
290+
impl<'a> NodeIdTree for ast_map::Map<'a> {
291+
fn is_descendant_of(&self, node: NodeId, ancestor: NodeId) -> bool {
292+
let mut node_ancestor = node;
293+
while node_ancestor != ancestor {
294+
let node_ancestor_parent = self.get_module_parent(node_ancestor);
295+
if node_ancestor_parent == node_ancestor {
296+
return false;
297+
}
298+
node_ancestor = node_ancestor_parent;
299+
}
300+
true
301+
}
302+
}
303+
286304
impl Visibility {
287305
pub fn from_hir(visibility: &hir::Visibility, id: NodeId, tcx: &TyCtxt) -> Self {
288306
match *visibility {
@@ -301,7 +319,7 @@ impl Visibility {
301319
}
302320

303321
/// Returns true if an item with this visibility is accessible from the given block.
304-
pub fn is_accessible_from(self, block: NodeId, map: &ast_map::Map) -> bool {
322+
pub fn is_accessible_from<T: NodeIdTree>(self, block: NodeId, tree: &T) -> bool {
305323
let restriction = match self {
306324
// Public items are visible everywhere.
307325
Visibility::Public => return true,
@@ -311,24 +329,18 @@ impl Visibility {
311329
Visibility::Restricted(module) => module,
312330
};
313331

314-
let mut block_ancestor = block;
315-
loop {
316-
if block_ancestor == restriction { return true }
317-
let block_ancestor_parent = map.get_module_parent(block_ancestor);
318-
if block_ancestor_parent == block_ancestor { return false }
319-
block_ancestor = block_ancestor_parent;
320-
}
332+
tree.is_descendant_of(block, restriction)
321333
}
322334

323335
/// Returns true if this visibility is at least as accessible as the given visibility
324-
pub fn is_at_least(self, vis: Visibility, map: &ast_map::Map) -> bool {
336+
pub fn is_at_least<T: NodeIdTree>(self, vis: Visibility, tree: &T) -> bool {
325337
let vis_restriction = match vis {
326338
Visibility::Public => return self == Visibility::Public,
327339
Visibility::PrivateExternal => return true,
328340
Visibility::Restricted(module) => module,
329341
};
330342

331-
self.is_accessible_from(vis_restriction, map)
343+
self.is_accessible_from(vis_restriction, tree)
332344
}
333345
}
334346

src/librustc_resolve/build_reduced_graph.rs

+28-23
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ trait ToNameBinding<'a> {
4646
fn to_name_binding(self) -> NameBinding<'a>;
4747
}
4848

49-
impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
49+
impl<'a> ToNameBinding<'a> for (Module<'a>, Span, ty::Visibility) {
5050
fn to_name_binding(self) -> NameBinding<'a> {
51-
NameBinding::create_from_module(self.0, Some(self.1))
51+
NameBinding { kind: NameBindingKind::Module(self.0), span: self.1, vis: self.2 }
5252
}
5353
}
5454

5555
impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
5656
fn to_name_binding(self) -> NameBinding<'a> {
57-
NameBinding { kind: NameBindingKind::Def(self.0), span: Some(self.1), vis: self.2 }
57+
NameBinding { kind: NameBindingKind::Def(self.0), span: self.1, vis: self.2 }
5858
}
5959
}
6060

@@ -247,8 +247,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
247247
};
248248
let parent_link = ModuleParentLink(parent, name);
249249
let def = Def::Mod(def_id);
250-
let module = self.new_extern_crate_module(parent_link, def, vis, item.id);
251-
self.define(parent, name, TypeNS, (module, sp));
250+
let module = self.new_extern_crate_module(parent_link, def, item.id);
251+
self.define(parent, name, TypeNS, (module, sp, vis));
252252

253253
self.build_reduced_graph_for_external_crate(module);
254254
}
@@ -257,8 +257,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
257257
ItemMod(..) => {
258258
let parent_link = ModuleParentLink(parent, name);
259259
let def = Def::Mod(self.ast_map.local_def_id(item.id));
260-
let module = self.new_module(parent_link, Some(def), false, vis);
261-
self.define(parent, name, TypeNS, (module, sp));
260+
let module = self.new_module(parent_link, Some(def), false);
261+
self.define(parent, name, TypeNS, (module, sp, vis));
262262
self.module_map.insert(item.id, module);
263263
*parent_ref = module;
264264
}
@@ -289,12 +289,12 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
289289
ItemEnum(ref enum_definition, _) => {
290290
let parent_link = ModuleParentLink(parent, name);
291291
let def = Def::Enum(self.ast_map.local_def_id(item.id));
292-
let module = self.new_module(parent_link, Some(def), false, vis);
293-
self.define(parent, name, TypeNS, (module, sp));
292+
let module = self.new_module(parent_link, Some(def), false);
293+
self.define(parent, name, TypeNS, (module, sp, vis));
294294

295295
for variant in &(*enum_definition).variants {
296296
let item_def_id = self.ast_map.local_def_id(item.id);
297-
self.build_reduced_graph_for_variant(variant, item_def_id, module);
297+
self.build_reduced_graph_for_variant(variant, item_def_id, module, vis);
298298
}
299299
}
300300

@@ -328,21 +328,25 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
328328
// Add all the items within to a new module.
329329
let parent_link = ModuleParentLink(parent, name);
330330
let def = Def::Trait(def_id);
331-
let module_parent = self.new_module(parent_link, Some(def), false, vis);
332-
self.define(parent, name, TypeNS, (module_parent, sp));
331+
let module_parent = self.new_module(parent_link, Some(def), false);
332+
self.define(parent, name, TypeNS, (module_parent, sp, vis));
333333

334334
// Add the names of all the items to the trait info.
335335
for item in items {
336336
let item_def_id = self.ast_map.local_def_id(item.id);
337+
let mut is_static_method = false;
337338
let (def, ns) = match item.node {
338339
hir::ConstTraitItem(..) => (Def::AssociatedConst(item_def_id), ValueNS),
339-
hir::MethodTraitItem(..) => (Def::Method(item_def_id), ValueNS),
340+
hir::MethodTraitItem(ref sig, _) => {
341+
is_static_method = sig.explicit_self.node == hir::SelfStatic;
342+
(Def::Method(item_def_id), ValueNS)
343+
}
340344
hir::TypeTraitItem(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
341345
};
342346

343347
self.define(module_parent, item.name, ns, (def, item.span, vis));
344348

345-
self.trait_item_map.insert((item.name, def_id), item_def_id);
349+
self.trait_item_map.insert((item.name, def_id), is_static_method);
346350
}
347351
}
348352
}
@@ -353,7 +357,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
353357
fn build_reduced_graph_for_variant(&mut self,
354358
variant: &Variant,
355359
item_id: DefId,
356-
parent: Module<'b>) {
360+
parent: Module<'b>,
361+
vis: ty::Visibility) {
357362
let name = variant.node.name;
358363
if variant.node.data.is_struct() {
359364
// Not adding fields for variants as they are not accessed with a self receiver
@@ -364,8 +369,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
364369
// Variants are always treated as importable to allow them to be glob used.
365370
// All variants are defined in both type and value namespaces as future-proofing.
366371
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));
367-
self.define(parent, name, ValueNS, (def, variant.span, parent.vis));
368-
self.define(parent, name, TypeNS, (def, variant.span, parent.vis));
372+
self.define(parent, name, ValueNS, (def, variant.span, vis));
373+
self.define(parent, name, TypeNS, (def, variant.span, vis));
369374
}
370375

371376
/// Constructs the reduced graph for one foreign item.
@@ -396,7 +401,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
396401
block_id);
397402

398403
let parent_link = BlockParentLink(parent, block_id);
399-
let new_module = self.new_module(parent_link, None, false, parent.vis);
404+
let new_module = self.new_module(parent_link, None, false);
400405
self.module_map.insert(block_id, new_module);
401406
*parent = new_module;
402407
}
@@ -425,8 +430,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
425430
debug!("(building reduced graph for external crate) building module {} {:?}",
426431
name, vis);
427432
let parent_link = ModuleParentLink(parent, name);
428-
let module = self.new_module(parent_link, Some(def), true, vis);
429-
self.try_define(parent, name, TypeNS, (module, DUMMY_SP));
433+
let module = self.new_module(parent_link, Some(def), true);
434+
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
430435
}
431436
Def::Variant(_, variant_id) => {
432437
debug!("(building reduced graph for external crate) building variant {}", name);
@@ -463,12 +468,12 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
463468
'{}'",
464469
trait_item_name);
465470

466-
self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());
471+
self.trait_item_map.insert((trait_item_name, def_id), false);
467472
}
468473

469474
let parent_link = ModuleParentLink(parent, name);
470-
let module = self.new_module(parent_link, Some(def), true, vis);
471-
self.try_define(parent, name, TypeNS, (module, DUMMY_SP));
475+
let module = self.new_module(parent_link, Some(def), true);
476+
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
472477
}
473478
Def::TyAlias(..) | Def::AssociatedTy(..) => {
474479
debug!("(building reduced graph for external crate) building type {}", name);

0 commit comments

Comments
 (0)