Skip to content

Commit 9d8889c

Browse files
committed
Auto merge of rust-lang#16348 - Veykril:nested-includes, r=Veykril
fix: Fix nested includes resolving from the wrong base file Fixes rust-lang/rust-analyzer#16109
2 parents 3e1ae6b + 215ede8 commit 9d8889c

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,54 @@ struct A;
12641264
);
12651265
}
12661266

1267+
#[test]
1268+
fn nested_include() {
1269+
check(
1270+
r#"
1271+
//- minicore: include
1272+
//- /lib.rs
1273+
include!("out_dir/includes.rs");
1274+
1275+
//- /out_dir/includes.rs
1276+
pub mod company_name {
1277+
pub mod network {
1278+
pub mod v1 {
1279+
include!("company_name.network.v1.rs");
1280+
}
1281+
}
1282+
}
1283+
//- /out_dir/company_name.network.v1.rs
1284+
pub struct IpAddress {
1285+
pub ip_type: &'static str,
1286+
}
1287+
/// Nested message and enum types in `IpAddress`.
1288+
pub mod ip_address {
1289+
pub enum IpType {
1290+
IpV4(u32),
1291+
}
1292+
}
1293+
1294+
"#,
1295+
expect![[r#"
1296+
crate
1297+
company_name: t
1298+
1299+
crate::company_name
1300+
network: t
1301+
1302+
crate::company_name::network
1303+
v1: t
1304+
1305+
crate::company_name::network::v1
1306+
IpAddress: t
1307+
ip_address: t
1308+
1309+
crate::company_name::network::v1::ip_address
1310+
IpType: t
1311+
"#]],
1312+
);
1313+
}
1314+
12671315
#[test]
12681316
fn macro_use_imports_all_macro_types() {
12691317
let db = TestDB::with_files(

crates/hir-expand/src/builtin_fn_macro.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
quote,
1616
quote::dollar_crate,
1717
tt::{self, DelimSpan},
18-
ExpandError, ExpandResult, HirFileIdExt, MacroCallId,
18+
ExpandError, ExpandResult, HirFileIdExt, MacroCallId, MacroFileIdExt,
1919
};
2020

2121
macro_rules! register_builtin {
@@ -609,7 +609,7 @@ fn relative_file(
609609
path_str: &str,
610610
allow_recursion: bool,
611611
) -> Result<FileId, ExpandError> {
612-
let call_site = call_id.as_file().original_file(db);
612+
let call_site = call_id.as_macro_file().parent(db).original_file_respecting_includes(db);
613613
let path = AnchoredPath { anchor: call_site, path: path_str };
614614
let res = db
615615
.resolve_path(path)

crates/hir-expand/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ pub trait MacroFileIdExt {
318318
fn expansion_level(self, db: &dyn ExpandDatabase) -> u32;
319319
/// If this is a macro call, returns the syntax node of the call.
320320
fn call_node(self, db: &dyn ExpandDatabase) -> InFile<SyntaxNode>;
321+
fn parent(self, db: &dyn ExpandDatabase) -> HirFileId;
321322

322323
fn expansion_info(self, db: &dyn ExpandDatabase) -> ExpansionInfo;
323324

@@ -353,6 +354,9 @@ impl MacroFileIdExt for MacroFileId {
353354
};
354355
}
355356
}
357+
fn parent(self, db: &dyn ExpandDatabase) -> HirFileId {
358+
self.macro_call_id.lookup(db).kind.file_id()
359+
}
356360

357361
/// Return expansion information if it is a macro-expansion file
358362
fn expansion_info(self, db: &dyn ExpandDatabase) -> ExpansionInfo {

0 commit comments

Comments
 (0)