Skip to content

Commit cdd7b18

Browse files
committed
Fix more path resolution for included submodules
Now with much more comprehensive testing! This adds tests for includes within modules.
1 parent 4afe0d5 commit cdd7b18

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use hir_expand::{
1616
name::{AsName, Name},
1717
proc_macro::CustomProcMacroExpander,
1818
ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
19+
MacroFileIdExt,
1920
};
2021
use intern::{sym, Interned};
2122
use itertools::{izip, Itertools};
@@ -1397,7 +1398,12 @@ impl DefCollector<'_> {
13971398
// Then, fetch and process the item tree. This will reuse the expansion result from above.
13981399
let item_tree = self.db.file_item_tree(file_id);
13991400

1400-
let mod_dir = self.mod_dirs[&module_id].clone();
1401+
let mod_dir = if macro_call_id.as_macro_file().is_include_macro(self.db.upcast()) {
1402+
ModDir::root()
1403+
} else {
1404+
self.mod_dirs[&module_id].clone()
1405+
};
1406+
14011407
ModCollector {
14021408
def_collector: &mut *self,
14031409
macro_depth: depth,

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,70 @@ pub mod ip_address {
13551355
);
13561356
}
13571357

1358+
#[test]
1359+
fn include_many_mods() {
1360+
check(
1361+
r#"
1362+
//- /lib.rs
1363+
#[rustc_builtin_macro]
1364+
macro_rules! include { () => {} }
1365+
1366+
mod nested {
1367+
include!("out_dir/includes.rs");
1368+
1369+
mod different_company {
1370+
include!("out_dir/different_company/mod.rs");
1371+
}
1372+
1373+
mod util;
1374+
}
1375+
1376+
//- /nested/util.rs
1377+
pub struct Helper {}
1378+
//- /out_dir/includes.rs
1379+
pub mod company_name {
1380+
pub mod network {
1381+
pub mod v1;
1382+
}
1383+
}
1384+
//- /out_dir/company_name/network/v1.rs
1385+
pub struct IpAddress {}
1386+
//- /out_dir/different_company/mod.rs
1387+
pub mod network;
1388+
//- /out_dir/different_company/network.rs
1389+
pub struct Url {}
1390+
1391+
"#,
1392+
expect![[r#"
1393+
crate
1394+
nested: t
1395+
1396+
crate::nested
1397+
company_name: t
1398+
different_company: t
1399+
util: t
1400+
1401+
crate::nested::company_name
1402+
network: t
1403+
1404+
crate::nested::company_name::network
1405+
v1: t
1406+
1407+
crate::nested::company_name::network::v1
1408+
IpAddress: t
1409+
1410+
crate::nested::different_company
1411+
network: t
1412+
1413+
crate::nested::different_company::network
1414+
Url: t
1415+
1416+
crate::nested::util
1417+
Helper: t
1418+
"#]],
1419+
);
1420+
}
1421+
13581422
#[test]
13591423
fn macro_use_imports_all_macro_types() {
13601424
let db = TestDB::with_files(

0 commit comments

Comments
 (0)