Skip to content

Commit a0c3ce3

Browse files
committed
resolve: use the Restricted variant of ty::Visibility when privacy checking
1 parent a4196cd commit a0c3ce3

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/librustc_resolve/lib.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -907,15 +907,6 @@ impl<'a> ModuleS<'a> {
907907
_ => false,
908908
}
909909
}
910-
911-
fn is_ancestor_of(&self, module: Module<'a>) -> bool {
912-
if self.def_id() == module.def_id() { return true }
913-
match module.parent_link {
914-
ParentLink::BlockParentLink(parent, _) |
915-
ParentLink::ModuleParentLink(parent, _) => self.is_ancestor_of(parent),
916-
_ => false,
917-
}
918-
}
919910
}
920911

921912
impl<'a> fmt::Debug for ModuleS<'a> {
@@ -1330,7 +1321,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13301321
// Check to see whether there are type bindings, and, if
13311322
// so, whether there is a module within.
13321323
if let Some(module_def) = binding.module() {
1333-
self.check_privacy(search_module, name, binding, span);
1324+
self.check_privacy(name, binding, span);
13341325
search_module = module_def;
13351326
} else {
13361327
let msg = format!("Not a module `{}`", name);
@@ -1461,7 +1452,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14611452
}
14621453

14631454
/// Returns the nearest normal module parent of the given module.
1464-
fn get_nearest_normal_module_parent(&mut self, module_: Module<'a>) -> Option<Module<'a>> {
1455+
fn get_nearest_normal_module_parent(&self, module_: Module<'a>) -> Option<Module<'a>> {
14651456
let mut module_ = module_;
14661457
loop {
14671458
match module_.parent_link {
@@ -1480,7 +1471,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14801471

14811472
/// Returns the nearest normal module parent of the given module, or the
14821473
/// module itself if it is a normal module.
1483-
fn get_nearest_normal_module_parent_or_self(&mut self, module_: Module<'a>) -> Module<'a> {
1474+
fn get_nearest_normal_module_parent_or_self(&self, module_: Module<'a>) -> Module<'a> {
14841475
if module_.is_normal() {
14851476
return module_;
14861477
}
@@ -2768,7 +2759,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
27682759
let name = segments.last().unwrap().identifier.name;
27692760
let result = self.resolve_name_in_module(containing_module, name, namespace, false, true);
27702761
result.success().map(|binding| {
2771-
self.check_privacy(containing_module, name, binding, span);
2762+
self.check_privacy(name, binding, span);
27722763
binding.def().unwrap()
27732764
}).ok_or(false)
27742765
}
@@ -2818,7 +2809,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28182809
let name = segments.last().unwrap().identifier.name;
28192810
let result = self.resolve_name_in_module(containing_module, name, namespace, false, true);
28202811
result.success().map(|binding| {
2821-
self.check_privacy(containing_module, name, binding, span);
2812+
self.check_privacy(name, binding, span);
28222813
binding.def().unwrap()
28232814
}).ok_or(false)
28242815
}
@@ -3412,16 +3403,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34123403
vis
34133404
}
34143405

3415-
fn is_visible(&self, binding: &'a NameBinding<'a>, parent: Module<'a>) -> bool {
3416-
binding.is_public() || parent.is_ancestor_of(self.current_module)
3406+
fn is_accessible(&self, vis: ty::Visibility) -> bool {
3407+
let current_module = self.get_nearest_normal_module_parent_or_self(self.current_module);
3408+
let node_id = self.ast_map.as_local_node_id(current_module.def_id().unwrap()).unwrap();
3409+
vis.is_accessible_from(node_id, &self.ast_map)
34173410
}
34183411

3419-
fn check_privacy(&mut self,
3420-
module: Module<'a>,
3421-
name: Name,
3422-
binding: &'a NameBinding<'a>,
3423-
span: Span) {
3424-
if !self.is_visible(binding, module) {
3412+
fn check_privacy(&mut self, name: Name, binding: &'a NameBinding<'a>, span: Span) {
3413+
if !self.is_accessible(binding.vis) {
34253414
self.privacy_errors.push(PrivacyError(span, name, binding));
34263415
}
34273416
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
521521
span_err!(self.resolver.session, directive.span, E0253, "{}", &msg);
522522
}
523523

524-
let privacy_error = if !self.resolver.is_visible(binding, target_module) {
524+
let privacy_error = if !self.resolver.is_accessible(binding.vis) {
525525
Some(Box::new(PrivacyError(directive.span, source, binding)))
526526
} else {
527527
None
@@ -567,10 +567,10 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
567567
_ => (),
568568
}
569569

570+
let ast_map = self.resolver.ast_map;
570571
match (&value_result, &type_result) {
571-
(&Success(name_binding), _) if !name_binding.is_import() &&
572-
directive.vis == ty::Visibility::Public &&
573-
!name_binding.is_public() => {
572+
(&Success(binding), _) if !binding.vis.is_at_least(directive.vis, ast_map) &&
573+
self.resolver.is_accessible(binding.vis) => {
574574
let msg = format!("`{}` is private, and cannot be reexported", source);
575575
let note_msg = format!("consider marking `{}` as `pub` in the imported module",
576576
source);
@@ -579,10 +579,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
579579
.emit();
580580
}
581581

582-
(_, &Success(name_binding)) if !name_binding.is_import() &&
583-
directive.vis == ty::Visibility::Public &&
584-
!name_binding.is_public() => {
585-
if name_binding.is_extern_crate() {
582+
(_, &Success(binding)) if !binding.vis.is_at_least(directive.vis, ast_map) &&
583+
self.resolver.is_accessible(binding.vis) => {
584+
if binding.is_extern_crate() {
586585
let msg = format!("extern crate `{}` is private, and cannot be reexported \
587586
(error E0364), consider declaring with `pub`",
588587
source);

0 commit comments

Comments
 (0)