Skip to content

Commit 4971667

Browse files
committed
review comments
1 parent 8c07d78 commit 4971667

File tree

4 files changed

+36
-51
lines changed

4 files changed

+36
-51
lines changed

src/librustc/hir/map/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -515,20 +515,16 @@ impl<'hir> Map<'hir> {
515515
}
516516

517517
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId) {
518-
self.get_if_module(module).expect("not a module")
519-
}
520-
521-
pub fn get_if_module(&self, module: DefId) -> Option<(&'hir Mod, Span, HirId)> {
522518
let hir_id = self.as_local_hir_id(module).unwrap();
523519
self.read(hir_id);
524520
match self.find_entry(hir_id).unwrap().node {
525521
Node::Item(&Item {
526522
span,
527523
node: ItemKind::Mod(ref m),
528524
..
529-
}) => Some((m, span, hir_id)),
530-
Node::Crate => Some((&self.forest.krate.module, self.forest.krate.span, hir_id)),
531-
_ => None,
525+
}) => (m, span, hir_id),
526+
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, hir_id),
527+
node => panic!("not a module: {:?}", node),
532528
}
533529
}
534530

@@ -682,6 +678,16 @@ impl<'hir> Map<'hir> {
682678
}
683679
}
684680

681+
/// Wether `hir_id` corresponds to a `mod` or a crate.
682+
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
683+
match self.lookup(hir_id) {
684+
Some(Entry { node: Node::Item(Item { node: ItemKind::Mod(_), .. }), .. }) |
685+
Some(Entry { node: Node::Crate, .. }) => true,
686+
_ => false,
687+
}
688+
}
689+
690+
685691
/// If there is some error when walking the parents (e.g., a node does not
686692
/// have a parent in the map or a node can't be found), then we return the
687693
/// last good `HirId` we found. Note that reaching the crate root (`id == 0`),

src/librustc_privacy/lib.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -509,44 +509,28 @@ impl EmbargoVisitor<'tcx> {
509509
}
510510

511511
fn update_macro_reachable_mod(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) {
512-
let set_vis = |this: &mut Self, hir_id: hir::HirId| {
513-
let item_def_id = this.tcx.hir().local_def_id(hir_id);
514-
if let Some(def_kind) = this.tcx.def_kind(item_def_id) {
515-
let item = this.tcx.hir().expect_item(hir_id);
516-
let vis = ty::Visibility::from_hir(&item.vis, hir_id, this.tcx);
517-
this.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
518-
}
519-
};
520-
521512
let module_def_id = self.tcx.hir().local_def_id(reachable_mod);
522-
if let Some((module, _, _)) = self.tcx.hir().get_if_module(module_def_id) {
523-
for item_id in &module.item_ids {
524-
let hir_id = item_id.id;
525-
set_vis(self, hir_id);
513+
let module = self.tcx.hir().get_module(module_def_id).0;
514+
for item_id in &module.item_ids {
515+
let hir_id = item_id.id;
516+
let item_def_id = self.tcx.hir().local_def_id(hir_id);
517+
if let Some(def_kind) = self.tcx.def_kind(item_def_id) {
518+
let item = self.tcx.hir().expect_item(hir_id);
519+
let vis = ty::Visibility::from_hir(&item.vis, hir_id, self.tcx);
520+
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
526521
}
527-
if let Some(exports) = self.tcx.module_exports(module_def_id) {
528-
for export in exports {
529-
if export.vis.is_accessible_from(defining_mod, self.tcx) {
530-
if let Res::Def(def_kind, def_id) = export.res {
531-
let vis = def_id_visibility(self.tcx, def_id).0;
532-
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
533-
self.update_macro_reachable_def(
534-
hir_id,
535-
def_kind,
536-
vis,
537-
defining_mod,
538-
);
539-
}
522+
}
523+
if let Some(exports) = self.tcx.module_exports(module_def_id) {
524+
for export in exports {
525+
if export.vis.is_accessible_from(defining_mod, self.tcx) {
526+
if let Res::Def(def_kind, def_id) = export.res {
527+
let vis = def_id_visibility(self.tcx, def_id).0;
528+
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
529+
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
540530
}
541531
}
542532
}
543533
}
544-
} else if let Some(hir::Node::Item(hir::Item {
545-
hir_id,
546-
..
547-
})) = self.tcx.hir().get_if_local(module_def_id) { // #63164
548-
// `macro` defined inside of an item is only visible inside of that item's scope.
549-
set_vis(self, *hir_id);
550534
}
551535
}
552536

@@ -898,10 +882,14 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
898882
self.tcx.hir().local_def_id(md.hir_id)
899883
).unwrap();
900884
let mut module_id = self.tcx.hir().as_local_hir_id(macro_module_def_id).unwrap();
885+
if !self.tcx.hir().is_hir_id_module(module_id) {
886+
// `module_id` doesn't correspond to a `mod`, return early (#63164).
887+
return;
888+
}
901889
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
902890
let new_level = self.update(md.hir_id, level);
903891
if new_level.is_none() {
904-
return
892+
return;
905893
}
906894

907895
loop {

src/test/ui/macros/macro-in-fn.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
// run-pass
12
#![feature(decl_macro)]
23

34
pub fn moo() {
45
pub macro ABC() {{}}
56
}
67

7-
fn main() {
8-
ABC!(); //~ ERROR cannot find macro `ABC!` in this scope
9-
}
8+
fn main() {}

src/test/ui/macros/macro-in-fn.stderr

-8
This file was deleted.

0 commit comments

Comments
 (0)