Skip to content

Commit

Permalink
effective visibility: Fix private visibility calculation for modules
Browse files Browse the repository at this point in the history
Optimizations removed in the previous commit required this function to behave incorrectly, but now those optimizations are gone so we can fix the bug.

Fixes rust-lang#104249
  • Loading branch information
petrochenkov committed Nov 23, 2022
1 parent f0843b8 commit a45a302
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_resolve/src/effective_visibilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_hir::def_id::LocalDefId;
use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility};
use rustc_middle::middle::privacy::{IntoDefIdTree, Level};
use rustc_middle::ty::Visibility;
use rustc_middle::ty::{DefIdTree, Visibility};

type ImportId<'a> = Interned<'a, NameBinding<'a>>;

Expand Down Expand Up @@ -54,10 +54,12 @@ impl Resolver<'_> {
}

fn private_vis_def(&mut self, def_id: LocalDefId) -> Visibility {
if def_id == CRATE_DEF_ID {
Visibility::Public
// For mod items `nearest_normal_mod` returns its argument, but we actually need its parent.
let normal_mod_id = self.nearest_normal_mod(def_id);
if normal_mod_id == def_id {
self.opt_local_parent(def_id).map_or(Visibility::Public, Visibility::Restricted)
} else {
Visibility::Restricted(self.nearest_normal_mod(def_id))
Visibility::Restricted(normal_mod_id)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/privacy/effective_visibilities_invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pub mod m {} //~ ERROR module has missing stability attribute

pub mod m { //~ ERROR the name `m` is defined multiple times
// mod inner {} - ICE
mod inner {}
type Inner = u8;
}

Expand Down

0 comments on commit a45a302

Please sign in to comment.