From 6a4cf48bffb739c3657443ae308fc0c91215d4d3 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Fri, 20 Sep 2019 23:20:43 +0800 Subject: [PATCH] fix module attr path --- crates/ra_hir/src/nameres/collector.rs | 8 ++++- crates/ra_hir/src/nameres/mod_resolution.rs | 3 +- .../src/nameres/tests/mod_resolution.rs | 31 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index a6b9b41d02..6b253ac409 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -1,5 +1,5 @@ use ra_db::FileId; -use ra_syntax::ast; +use ra_syntax::{ast, SmolStr}; use rustc_hash::FxHashMap; use test_utils::tested_by; @@ -98,6 +98,7 @@ where self.def_map.modules[module_id].definition = Some(file_id); ModCollector { def_collector: &mut *self, + attr_path: None, module_id, file_id: file_id.into(), raw_items: &raw_items, @@ -474,6 +475,7 @@ where ModCollector { def_collector: &mut *self, file_id, + attr_path: None, module_id, raw_items: &raw_items, parent_module: None, @@ -497,6 +499,7 @@ struct ModCollector<'a, D> { def_collector: D, module_id: CrateModuleId, file_id: HirFileId, + attr_path: Option<&'a SmolStr>, raw_items: &'a raw::RawItems, parent_module: Option>, } @@ -551,6 +554,7 @@ where ModCollector { def_collector: &mut *self.def_collector, module_id, + attr_path: attr_path.as_ref(), file_id: self.file_id, raw_items: self.raw_items, parent_module: Some(parent_module), @@ -567,6 +571,7 @@ where match resolve_submodule( self.def_collector.db, self.file_id, + self.attr_path, name, is_root, attr_path.as_ref(), @@ -578,6 +583,7 @@ where ModCollector { def_collector: &mut *self.def_collector, module_id, + attr_path: attr_path.as_ref(), file_id: file_id.into(), raw_items: &raw_items, parent_module: None, diff --git a/crates/ra_hir/src/nameres/mod_resolution.rs b/crates/ra_hir/src/nameres/mod_resolution.rs index a9e9eb9e65..3aa32bd66c 100644 --- a/crates/ra_hir/src/nameres/mod_resolution.rs +++ b/crates/ra_hir/src/nameres/mod_resolution.rs @@ -23,6 +23,7 @@ impl<'a> ParentModule<'a> { pub(super) fn resolve_submodule( db: &impl DefDatabase, file_id: HirFileId, + mod_attr_path: Option<&SmolStr>, name: &Name, is_root: bool, attr_path: Option<&SmolStr>, @@ -80,7 +81,7 @@ pub(super) fn resolve_submodule( ResolutionMode::OutOfLine(OutOfLineMode::WithAttributePath(path)) } (None, None) => { - let is_dir_owner = is_root || mod_name == "mod"; + let is_dir_owner = is_root || mod_name == "mod" || mod_attr_path.is_some(); if is_dir_owner { let file_mod = dir_path.join(format!("{}.rs", name)); let dir_mod = dir_path.join(format!("{}/mod.rs", name)); diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs index 4f8398460d..e3e6f1e959 100644 --- a/crates/ra_hir/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir/src/nameres/tests/mod_resolution.rs @@ -706,3 +706,34 @@ fn unresolved_module_diagnostics() { "### ); } + +#[test] +fn module_resolution_decl_inside_module_in_non_crate_root_2() { + let map = def_map_with_crate_graph( + r###" + //- /main.rs + #[path="module/m2.rs"] + mod module; + + //- /module/m2.rs + pub mod submod; + + //- /module/submod.rs + pub struct Baz; + "###, + crate_graph! { + "main": ("/main.rs", []), + }, + ); + + assert_snapshot!(map, @r###" + ⋮crate + ⋮module: t + ⋮ + ⋮crate::module + ⋮submod: t + ⋮ + ⋮crate::module::submod + ⋮Baz: t v + "###); +}