Skip to content

Commit 449d4c4

Browse files
committed
Auto merge of rust-lang#17650 - ObsidianMinor:fix/17645, r=Veykril
Fix path resolution for child mods of those expanded by `include!` Child modules wouldn't use the correct candidate paths due to a branch that doesn't seem to be doing what it's intended to do. Removing the branch fixes the problem and all existing test cases pass. Having no knowledge of how any of this works, I believe this fixes rust-lang#17645. Using another test that writes the included mod directly into `lib.rs` instead, I found the difference can be traced to the candidate files we use to look up mods. A separate branch for if the file comes from an `include!` macro doesn't take into account the original mod we're contained within: ```rust None if file_id.macro_file().map_or(false, |it| it.is_include_macro(db.upcast())) => { candidate_files.push(format!("{}.rs", name.display(db.upcast()))); candidate_files.push(format!("{}/mod.rs", name.display(db.upcast()))); } ``` I'm not sure why this branch exists. Tracing the branch back takes us to 3bb9efb but it doesn't say *why* the branch was added. The test case that was added in this commit passes with the branch removed, so I think it's just superfluous at this point.
2 parents ea7f367 + e43b74a commit 449d4c4

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/tools/rust-analyzer/crates/hir-def/src/nameres/mod_resolution.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This module resolves `mod foo;` declaration to file.
22
use arrayvec::ArrayVec;
33
use base_db::AnchoredPath;
4-
use hir_expand::{name::Name, HirFileIdExt, MacroFileIdExt};
4+
use hir_expand::{name::Name, HirFileIdExt};
55
use limit::Limit;
66
use span::EditionedFileId;
77
use syntax::ToSmolStr as _;
@@ -73,10 +73,6 @@ impl ModDir {
7373
Some(attr_path) => {
7474
candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner))
7575
}
76-
None if file_id.macro_file().map_or(false, |it| it.is_include_macro(db.upcast())) => {
77-
candidate_files.push(format!("{}.rs", name.display(db.upcast())));
78-
candidate_files.push(format!("{}/mod.rs", name.display(db.upcast())));
79-
}
8076
None => {
8177
candidate_files.push(format!(
8278
"{}{}.rs",

src/tools/rust-analyzer/crates/hir-def/src/nameres/tests/macros.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,52 @@ pub mod ip_address {
13091309
);
13101310
}
13111311

1312+
#[test]
1313+
fn include_with_submod_file() {
1314+
check(
1315+
r#"
1316+
//- minicore: include
1317+
//- /lib.rs
1318+
include!("out_dir/includes.rs");
1319+
1320+
//- /out_dir/includes.rs
1321+
pub mod company_name {
1322+
pub mod network {
1323+
pub mod v1;
1324+
}
1325+
}
1326+
//- /out_dir/company_name/network/v1.rs
1327+
pub struct IpAddress {
1328+
pub ip_type: &'static str,
1329+
}
1330+
/// Nested message and enum types in `IpAddress`.
1331+
pub mod ip_address {
1332+
pub enum IpType {
1333+
IpV4(u32),
1334+
}
1335+
}
1336+
1337+
"#,
1338+
expect![[r#"
1339+
crate
1340+
company_name: t
1341+
1342+
crate::company_name
1343+
network: t
1344+
1345+
crate::company_name::network
1346+
v1: t
1347+
1348+
crate::company_name::network::v1
1349+
IpAddress: t
1350+
ip_address: t
1351+
1352+
crate::company_name::network::v1::ip_address
1353+
IpType: t
1354+
"#]],
1355+
);
1356+
}
1357+
13121358
#[test]
13131359
fn macro_use_imports_all_macro_types() {
13141360
let db = TestDB::with_files(

0 commit comments

Comments
 (0)