Skip to content

Commit 8497061

Browse files
committed
Hygienize librustc_privacy.
1 parent bfa2ef6 commit 8497061

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/librustc/hir/lowering.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,18 +1515,23 @@ impl<'a> LoweringContext<'a> {
15151515

15161516
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
15171517
let mut name = i.ident.name;
1518+
let mut vis = self.lower_visibility(&i.vis, None);
15181519
let attrs = self.lower_attrs(&i.attrs);
15191520
if let ItemKind::MacroDef(ref def) = i.node {
15201521
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
1521-
let (body, legacy) = (def.stream(), def.legacy);
15221522
self.exported_macros.push(hir::MacroDef {
1523-
name: name, attrs: attrs, id: i.id, span: i.span, body: body, legacy: legacy,
1523+
name: name,
1524+
vis: vis,
1525+
attrs: attrs,
1526+
id: i.id,
1527+
span: i.span,
1528+
body: def.stream(),
1529+
legacy: def.legacy,
15241530
});
15251531
}
15261532
return None;
15271533
}
15281534

1529-
let mut vis = self.lower_visibility(&i.vis, None);
15301535
let node = self.with_parent_def(i.id, |this| {
15311536
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
15321537
});

src/librustc/hir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ impl Crate {
532532
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
533533
pub struct MacroDef {
534534
pub name: Name,
535+
pub vis: Visibility,
535536
pub attrs: HirVec<Attribute>,
536537
pub id: NodeId,
537538
pub span: Span,

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ impl_stable_hash_for!(enum hir::QPath {
329329

330330
impl_stable_hash_for!(struct hir::MacroDef {
331331
name,
332+
vis,
332333
attrs,
333334
id,
334335
span,

src/librustc_privacy/lib.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern crate syntax_pos;
2828

2929
use rustc::hir::{self, PatKind};
3030
use rustc::hir::def::Def;
31-
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
31+
use rustc::hir::def_id::{LOCAL_CRATE, CrateNum, DefId};
3232
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
3333
use rustc::hir::itemlikevisit::DeepVisitor;
3434
use rustc::lint;
@@ -37,7 +37,8 @@ use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
3737
use rustc::ty::fold::TypeVisitor;
3838
use rustc::ty::maps::Providers;
3939
use rustc::util::nodemap::NodeSet;
40-
use syntax::ast;
40+
use syntax::ast::{self, CRATE_NODE_ID, Ident};
41+
use syntax::symbol::keywords;
4142
use syntax_pos::Span;
4243

4344
use std::cmp;
@@ -344,7 +345,35 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
344345
}
345346

346347
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
347-
self.update(md.id, Some(AccessLevel::Public));
348+
if md.legacy {
349+
self.update(md.id, Some(AccessLevel::Public));
350+
return
351+
}
352+
353+
let module_did = ty::DefIdTree::parent(self.tcx, self.tcx.hir.local_def_id(md.id)).unwrap();
354+
let mut module_id = self.tcx.hir.as_local_node_id(module_did).unwrap();
355+
let level = if md.vis == hir::Public { self.get(module_id) } else { None };
356+
let level = self.update(md.id, level);
357+
if level.is_none() {
358+
return
359+
}
360+
361+
loop {
362+
let module = if module_id == ast::CRATE_NODE_ID {
363+
&self.tcx.hir.krate().module
364+
} else if let hir::ItemMod(ref module) = self.tcx.hir.expect_item(module_id).node {
365+
module
366+
} else {
367+
unreachable!()
368+
};
369+
for id in &module.item_ids {
370+
self.update(id.id, level);
371+
}
372+
if module_id == ast::CRATE_NODE_ID {
373+
break
374+
}
375+
module_id = self.tcx.hir.get_parent_node(module_id);
376+
}
348377
}
349378

350379
fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
@@ -425,13 +454,15 @@ impl<'b, 'a, 'tcx> TypeVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'b
425454
struct NamePrivacyVisitor<'a, 'tcx: 'a> {
426455
tcx: TyCtxt<'a, 'tcx, 'tcx>,
427456
tables: &'a ty::TypeckTables<'tcx>,
428-
current_item: DefId,
457+
current_item: ast::NodeId,
429458
}
430459

431460
impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
432461
// Checks that a field is accessible.
433462
fn check_field(&mut self, span: Span, def: &'tcx ty::AdtDef, field: &'tcx ty::FieldDef) {
434-
if !def.is_enum() && !field.vis.is_accessible_from(self.current_item, self.tcx) {
463+
let ident = Ident { ctxt: span.ctxt.modern(), ..keywords::Invalid.ident() };
464+
let def_id = self.tcx.adjust_ident(ident, def.did, self.current_item).1;
465+
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
435466
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
436467
field.name, def.variant_descr(), self.tcx.item_path_str(def.did))
437468
.span_label(span, format!("field `{}` is private", field.name))
@@ -455,7 +486,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
455486
}
456487

457488
fn visit_item(&mut self, item: &'tcx hir::Item) {
458-
let orig_current_item = replace(&mut self.current_item, self.tcx.hir.local_def_id(item.id));
489+
let orig_current_item = replace(&mut self.current_item, item.id);
459490
intravisit::walk_item(self, item);
460491
self.current_item = orig_current_item;
461492
}
@@ -1182,7 +1213,7 @@ fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11821213
let mut visitor = NamePrivacyVisitor {
11831214
tcx: tcx,
11841215
tables: &ty::TypeckTables::empty(),
1185-
current_item: DefId::local(CRATE_DEF_INDEX),
1216+
current_item: CRATE_NODE_ID,
11861217
};
11871218
intravisit::walk_crate(&mut visitor, krate);
11881219

0 commit comments

Comments
 (0)