about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyo Yoshida <low.ryoshida@gmail.com>2022-11-11 19:50:26 +0900
committerRyo Yoshida <low.ryoshida@gmail.com>2022-11-11 20:31:37 +0900
commitdea49d082644cb3dbc7755849b6e3c325a6f32d9 (patch)
tree3e8bd4e2e0b0624114bd713206c62c17e8dd608f
parent599142c34abad1442994947bd1200ce0bc973c54 (diff)
downloadrust-dea49d082644cb3dbc7755849b6e3c325a6f32d9.tar.gz
rust-dea49d082644cb3dbc7755849b6e3c325a6f32d9.zip
fix: check visibility of each segment in path resolution
-rw-r--r--crates/hir-def/src/nameres/collector.rs4
-rw-r--r--crates/hir-def/src/nameres/path_resolution.rs7
-rw-r--r--crates/hir-def/src/nameres/tests/globs.rs30
3 files changed, 41 insertions, 0 deletions
diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs
index 9ffc218818c..b0dd01f9dbe 100644
--- a/crates/hir-def/src/nameres/collector.rs
+++ b/crates/hir-def/src/nameres/collector.rs
@@ -212,6 +212,7 @@ impl Import {
 
 #[derive(Debug, Eq, PartialEq)]
 struct ImportDirective {
+    /// The module this import directive is in.
     module_id: LocalModuleId,
     import: Import,
     status: PartialResolvedImport,
@@ -963,8 +964,10 @@ impl DefCollector<'_> {
 
     fn update(
         &mut self,
+        // The module for which `resolutions` have been resolve
         module_id: LocalModuleId,
         resolutions: &[(Option<Name>, PerNs)],
+        // Visibility this import will have
         vis: Visibility,
         import_type: ImportType,
     ) {
@@ -974,6 +977,7 @@ impl DefCollector<'_> {
 
     fn update_recursive(
         &mut self,
+        // The module for which `resolutions` have been resolve
         module_id: LocalModuleId,
         resolutions: &[(Option<Name>, PerNs)],
         // All resolutions are imported with this visibility; the visibilities in
diff --git a/crates/hir-def/src/nameres/path_resolution.rs b/crates/hir-def/src/nameres/path_resolution.rs
index 8dfda6df64e..20d39ec6cb9 100644
--- a/crates/hir-def/src/nameres/path_resolution.rs
+++ b/crates/hir-def/src/nameres/path_resolution.rs
@@ -73,7 +73,10 @@ impl DefMap {
     pub(crate) fn resolve_visibility(
         &self,
         db: &dyn DefDatabase,
+        // module to import to
         original_module: LocalModuleId,
+        // pub(path)
+        //     ^^^^ this
         visibility: &RawVisibility,
     ) -> Option<Visibility> {
         let mut vis = match visibility {
@@ -115,6 +118,7 @@ impl DefMap {
         &self,
         db: &dyn DefDatabase,
         mode: ResolveMode,
+        // module to import to
         mut original_module: LocalModuleId,
         path: &ModPath,
         shadow: BuiltinShadowMode,
@@ -361,6 +365,9 @@ impl DefMap {
                     );
                 }
             };
+
+            curr_per_ns = curr_per_ns
+                .filter_visibility(|vis| vis.is_visible_from_def_map(db, self, original_module));
         }
 
         ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None, Some(self.krate))
diff --git a/crates/hir-def/src/nameres/tests/globs.rs b/crates/hir-def/src/nameres/tests/globs.rs
index b2a6a592cf3..84d14e3b926 100644
--- a/crates/hir-def/src/nameres/tests/globs.rs
+++ b/crates/hir-def/src/nameres/tests/globs.rs
@@ -336,3 +336,33 @@ mod d {
         "#]],
     );
 }
+
+#[test]
+fn glob_name_collision_check_visibility() {
+    check(
+        r#"
+mod event {
+    mod serenity {
+        pub fn Event() {}
+    }
+    use serenity::*;
+
+    pub struct Event {}
+}
+
+use event::Event;
+        "#,
+        expect![[r#"
+            crate
+            Event: t
+            event: t
+
+            crate::event
+            Event: t v
+            serenity: t
+
+            crate::event::serenity
+            Event: v
+        "#]],
+    );
+}