Skip to content

Commit a45a302

Browse files
committed
effective visibility: Fix private visibility calculation for modules
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 #104249
1 parent f0843b8 commit a45a302

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

compiler/rustc_resolve/src/effective_visibilities.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::def_id::LocalDefId;
99
use rustc_hir::def_id::CRATE_DEF_ID;
1010
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility};
1111
use rustc_middle::middle::privacy::{IntoDefIdTree, Level};
12-
use rustc_middle::ty::Visibility;
12+
use rustc_middle::ty::{DefIdTree, Visibility};
1313

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

@@ -54,10 +54,12 @@ impl Resolver<'_> {
5454
}
5555

5656
fn private_vis_def(&mut self, def_id: LocalDefId) -> Visibility {
57-
if def_id == CRATE_DEF_ID {
58-
Visibility::Public
57+
// For mod items `nearest_normal_mod` returns its argument, but we actually need its parent.
58+
let normal_mod_id = self.nearest_normal_mod(def_id);
59+
if normal_mod_id == def_id {
60+
self.opt_local_parent(def_id).map_or(Visibility::Public, Visibility::Restricted)
5961
} else {
60-
Visibility::Restricted(self.nearest_normal_mod(def_id))
62+
Visibility::Restricted(normal_mod_id)
6163
}
6264
}
6365
}

src/test/ui/privacy/effective_visibilities_invariants.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pub mod m {} //~ ERROR module has missing stability attribute
66

77
pub mod m { //~ ERROR the name `m` is defined multiple times
8-
// mod inner {} - ICE
8+
mod inner {}
99
type Inner = u8;
1010
}
1111

0 commit comments

Comments
 (0)