Skip to content

Commit dea49d0

Browse files
committed
fix: check visibility of each segment in path resolution
1 parent 599142c commit dea49d0

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

crates/hir-def/src/nameres/collector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ impl Import {
212212

213213
#[derive(Debug, Eq, PartialEq)]
214214
struct ImportDirective {
215+
/// The module this import directive is in.
215216
module_id: LocalModuleId,
216217
import: Import,
217218
status: PartialResolvedImport,
@@ -963,8 +964,10 @@ impl DefCollector<'_> {
963964

964965
fn update(
965966
&mut self,
967+
// The module for which `resolutions` have been resolve
966968
module_id: LocalModuleId,
967969
resolutions: &[(Option<Name>, PerNs)],
970+
// Visibility this import will have
968971
vis: Visibility,
969972
import_type: ImportType,
970973
) {
@@ -974,6 +977,7 @@ impl DefCollector<'_> {
974977

975978
fn update_recursive(
976979
&mut self,
980+
// The module for which `resolutions` have been resolve
977981
module_id: LocalModuleId,
978982
resolutions: &[(Option<Name>, PerNs)],
979983
// All resolutions are imported with this visibility; the visibilities in

crates/hir-def/src/nameres/path_resolution.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ impl DefMap {
7373
pub(crate) fn resolve_visibility(
7474
&self,
7575
db: &dyn DefDatabase,
76+
// module to import to
7677
original_module: LocalModuleId,
78+
// pub(path)
79+
// ^^^^ this
7780
visibility: &RawVisibility,
7881
) -> Option<Visibility> {
7982
let mut vis = match visibility {
@@ -115,6 +118,7 @@ impl DefMap {
115118
&self,
116119
db: &dyn DefDatabase,
117120
mode: ResolveMode,
121+
// module to import to
118122
mut original_module: LocalModuleId,
119123
path: &ModPath,
120124
shadow: BuiltinShadowMode,
@@ -361,6 +365,9 @@ impl DefMap {
361365
);
362366
}
363367
};
368+
369+
curr_per_ns = curr_per_ns
370+
.filter_visibility(|vis| vis.is_visible_from_def_map(db, self, original_module));
364371
}
365372

366373
ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None, Some(self.krate))

crates/hir-def/src/nameres/tests/globs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,33 @@ mod d {
336336
"#]],
337337
);
338338
}
339+
340+
#[test]
341+
fn glob_name_collision_check_visibility() {
342+
check(
343+
r#"
344+
mod event {
345+
mod serenity {
346+
pub fn Event() {}
347+
}
348+
use serenity::*;
349+
350+
pub struct Event {}
351+
}
352+
353+
use event::Event;
354+
"#,
355+
expect![[r#"
356+
crate
357+
Event: t
358+
event: t
359+
360+
crate::event
361+
Event: t v
362+
serenity: t
363+
364+
crate::event::serenity
365+
Event: v
366+
"#]],
367+
);
368+
}

0 commit comments

Comments
 (0)