Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryanskiy committed Sep 19, 2022
1 parent a9e21a3 commit 0d2e73a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_middle/src/middle/privacy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A pass that checks to make sure private fields and methods aren't used
//! outside their scopes. This pass will also generate a set of exported items
//! which are available for use externally when compiled as a library.
use crate::ty::{Visibility, DefIdTree};
use crate::ty::{DefIdTree, Visibility};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_macros::HashStable;
Expand Down Expand Up @@ -60,7 +60,12 @@ impl EffectiveVisibility {
}

pub fn nearest_available(&self, tag: AccessLevel) -> Option<Visibility> {
for level in [AccessLevel::ReachableFromImplTrait, AccessLevel::Reachable, AccessLevel::Exported, AccessLevel::Public] {
for level in [
AccessLevel::ReachableFromImplTrait,
AccessLevel::Reachable,
AccessLevel::Exported,
AccessLevel::Public,
] {
if (level <= tag) && self.get(tag).is_some() {
return self.get(tag).cloned();
}
Expand Down
51 changes: 38 additions & 13 deletions compiler/rustc_resolve/src/access_levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,31 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
if this.r.opt_local_def_id(import.id).is_some() {
let vis = match binding.vis {
Visibility::Public => Visibility::Public,
Visibility::Restricted(id) => Visibility::Restricted(id.expect_local())
Visibility::Restricted(id) => Visibility::Restricted(id.expect_local()),
};
this.update_effective_vis(this.r.local_def_id(import.id), vis, parent_id, AccessLevel::Exported);
this.update_effective_vis(
this.r.local_def_id(import.id),
vis,
parent_id,
AccessLevel::Exported,
);
if let ImportKind::Single { additional_ids, .. } = import.kind {

if let Some(id) = this.r.opt_local_def_id(additional_ids.0) {
this.update_effective_vis(id, vis, parent_id, AccessLevel::Exported);
this.update_effective_vis(
id,
vis,
parent_id,
AccessLevel::Exported,
);
}

if let Some(id) = this.r.opt_local_def_id(additional_ids.1) {
this.update_effective_vis(id, vis, parent_id, AccessLevel::Exported);
this.update_effective_vis(
id,
vis,
parent_id,
AccessLevel::Exported,
);
}
}

Expand All @@ -76,8 +90,8 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
}
};

let module = self.r.get_module(module_id.to_def_id()).unwrap();
let resolutions = self.r.resolutions(module);
let module = self.r.get_module(module_id.to_def_id()).unwrap();
let resolutions = self.r.resolutions(module);

for (.., name_resolution) in resolutions.borrow().iter() {
if let Some(binding) = name_resolution.borrow().binding() {
Expand All @@ -87,8 +101,8 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
set_import_binding_access_level(self, binding, module_id);
}
AccessLevel::Exported
},
false => AccessLevel::Public
}
false => AccessLevel::Public,
};

if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) && !binding.is_ambiguity(){
Expand All @@ -110,7 +124,8 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
tag: AccessLevel,
) {
if let Some(inherited_effective_vis) = self.r.access_levels.get_effective_vis(module_id) {
let mut current_effective_vis = self.r.access_levels.get_effective_vis(current_id).copied().unwrap_or_default();
let mut current_effective_vis =
self.r.access_levels.get_effective_vis(current_id).copied().unwrap_or_default();
let current_effective_vis_copy = current_effective_vis.clone();
for level in [
AccessLevel::Public,
Expand All @@ -119,11 +134,16 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
AccessLevel::ReachableFromImplTrait,
] {
if level <= tag {
let nearest_available_vis = inherited_effective_vis.nearest_available(level).unwrap();
let nearest_available_vis =
inherited_effective_vis.nearest_available(level).unwrap();
let calculated_effective_vis = match current_vis {
Visibility::Public => nearest_available_vis,
Visibility::Restricted(_) => {
if current_vis.is_at_least(nearest_available_vis, &*self.r) {nearest_available_vis} else {current_vis}
if current_vis.is_at_least(nearest_available_vis, &*self.r) {
nearest_available_vis
} else {
current_vis
}
}
};
current_effective_vis.update(calculated_effective_vis, level, &*self.r);
Expand Down Expand Up @@ -169,7 +189,12 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
// Foreign modules inherit level from parents.
ast::ItemKind::ForeignMod(..) => {
let parent_id = self.r.local_parent(def_id);
self.update_effective_vis(def_id, Visibility::Public, parent_id, AccessLevel::Public);
self.update_effective_vis(
def_id,
Visibility::Public,
parent_id,
AccessLevel::Public,
);
}

// Only exported `macro_rules!` items are public, but they always are
Expand Down

0 comments on commit 0d2e73a

Please sign in to comment.