Skip to content

Commit 8a26ba7

Browse files
authored
Rollup merge of #63624 - estebank:unreachable-macro, r=petrochenkov
When declaring a declarative macro in an item it's only accessible inside it Fix #63164. r? @petrochenkov
2 parents 201e52e + 4971667 commit 8a26ba7

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/librustc/hir/map/mod.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,7 @@ impl<'hir> Map<'hir> {
514514
&self.forest.krate.attrs
515515
}
516516

517-
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId)
518-
{
517+
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId) {
519518
let hir_id = self.as_local_hir_id(module).unwrap();
520519
self.read(hir_id);
521520
match self.find_entry(hir_id).unwrap().node {
@@ -525,7 +524,7 @@ impl<'hir> Map<'hir> {
525524
..
526525
}) => (m, span, hir_id),
527526
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, hir_id),
528-
_ => panic!("not a module")
527+
node => panic!("not a module: {:?}", node),
529528
}
530529
}
531530

@@ -679,6 +678,16 @@ impl<'hir> Map<'hir> {
679678
}
680679
}
681680

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+
682691
/// If there is some error when walking the parents (e.g., a node does not
683692
/// have a parent in the map or a node can't be found), then we return the
684693
/// last good `HirId` we found. Note that reaching the crate root (`id == 0`),

src/librustc_privacy/lib.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,7 @@ impl EmbargoVisitor<'tcx> {
508508
}
509509
}
510510

511-
fn update_macro_reachable_mod(
512-
&mut self,
513-
reachable_mod: hir::HirId,
514-
defining_mod: DefId,
515-
) {
511+
fn update_macro_reachable_mod(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) {
516512
let module_def_id = self.tcx.hir().local_def_id(reachable_mod);
517513
let module = self.tcx.hir().get_module(module_def_id).0;
518514
for item_id in &module.item_ids {
@@ -524,19 +520,13 @@ impl EmbargoVisitor<'tcx> {
524520
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
525521
}
526522
}
527-
528523
if let Some(exports) = self.tcx.module_exports(module_def_id) {
529524
for export in exports {
530525
if export.vis.is_accessible_from(defining_mod, self.tcx) {
531526
if let Res::Def(def_kind, def_id) = export.res {
532527
let vis = def_id_visibility(self.tcx, def_id).0;
533528
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
534-
self.update_macro_reachable_def(
535-
hir_id,
536-
def_kind,
537-
vis,
538-
defining_mod,
539-
);
529+
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
540530
}
541531
}
542532
}
@@ -892,10 +882,14 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
892882
self.tcx.hir().local_def_id(md.hir_id)
893883
).unwrap();
894884
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+
}
895889
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
896890
let new_level = self.update(md.hir_id, level);
897891
if new_level.is_none() {
898-
return
892+
return;
899893
}
900894

901895
loop {

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-pass
2+
#![feature(decl_macro)]
3+
4+
pub fn moo() {
5+
pub macro ABC() {{}}
6+
}
7+
8+
fn main() {}

0 commit comments

Comments
 (0)