Skip to content

When declaring a declarative macro in an item it's only accessible inside it #63624

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 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ impl<'hir> Map<'hir> {
&self.forest.krate.attrs
}

pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId)
{
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId) {
let hir_id = self.as_local_hir_id(module).unwrap();
self.read(hir_id);
match self.find_entry(hir_id).unwrap().node {
Expand All @@ -525,7 +524,7 @@ impl<'hir> Map<'hir> {
..
}) => (m, span, hir_id),
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, hir_id),
_ => panic!("not a module")
node => panic!("not a module: {:?}", node),
}
}

Expand Down Expand Up @@ -679,6 +678,16 @@ impl<'hir> Map<'hir> {
}
}

/// Wether `hir_id` corresponds to a `mod` or a crate.
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
match self.lookup(hir_id) {
Some(Entry { node: Node::Item(Item { node: ItemKind::Mod(_), .. }), .. }) |
Some(Entry { node: Node::Crate, .. }) => true,
_ => false,
}
}


/// If there is some error when walking the parents (e.g., a node does not
/// have a parent in the map or a node can't be found), then we return the
/// last good `HirId` we found. Note that reaching the crate root (`id == 0`),
Expand Down
20 changes: 7 additions & 13 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,7 @@ impl EmbargoVisitor<'tcx> {
}
}

fn update_macro_reachable_mod(
&mut self,
reachable_mod: hir::HirId,
defining_mod: DefId,
) {
fn update_macro_reachable_mod(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) {
let module_def_id = self.tcx.hir().local_def_id(reachable_mod);
let module = self.tcx.hir().get_module(module_def_id).0;
for item_id in &module.item_ids {
Expand All @@ -524,19 +520,13 @@ impl EmbargoVisitor<'tcx> {
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
}
}

if let Some(exports) = self.tcx.module_exports(module_def_id) {
for export in exports {
if export.vis.is_accessible_from(defining_mod, self.tcx) {
if let Res::Def(def_kind, def_id) = export.res {
let vis = def_id_visibility(self.tcx, def_id).0;
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
self.update_macro_reachable_def(
hir_id,
def_kind,
vis,
defining_mod,
);
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
}
}
}
Expand Down Expand Up @@ -892,10 +882,14 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
self.tcx.hir().local_def_id(md.hir_id)
).unwrap();
let mut module_id = self.tcx.hir().as_local_hir_id(macro_module_def_id).unwrap();
if !self.tcx.hir().is_hir_id_module(module_id) {
// `module_id` doesn't correspond to a `mod`, return early (#63164).
return;
}
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
let new_level = self.update(md.hir_id, level);
if new_level.is_none() {
return
return;
}

loop {
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/macros/macro-in-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-pass
#![feature(decl_macro)]

pub fn moo() {
pub macro ABC() {{}}
}

fn main() {}