Skip to content

Commit 013e908

Browse files
Merge #3509
3509: Prevent include! macro include itself r=matklad a=edwin0cheng This PR prevent `include` macro including itself. Note: It **does not** prevent a cyclic include: ```rust // foo.rs include!("bar.rs") // bar.rs include!("foo.rs") ``` Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents 919747c + 18f88ad commit 013e908

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,12 @@ fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Opti
279279
let call_site = call_id.as_file().original_file(db);
280280
let path = RelativePath::new(&path);
281281

282-
db.resolve_relative_path(call_site, &path)
282+
let res = db.resolve_relative_path(call_site, &path)?;
283+
// Prevent include itself
284+
if res == call_site {
285+
return None;
286+
}
287+
Some(res)
283288
}
284289

285290
fn include_expand(

crates/ra_hir_ty/src/tests/macros.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,24 @@ fn bar() -> u32 {0}
510510
assert_eq!("{unknown}", type_at_pos(&db, pos));
511511
}
512512

513+
#[test]
514+
fn infer_builtin_macros_include_itself_should_failed() {
515+
let (db, pos) = TestDB::with_position(
516+
r#"
517+
//- /main.rs
518+
#[rustc_builtin_macro]
519+
macro_rules! include {() => {}}
520+
521+
include!("main.rs");
522+
523+
fn main() {
524+
0<|>
525+
}
526+
"#,
527+
);
528+
assert_eq!("i32", type_at_pos(&db, pos));
529+
}
530+
513531
#[test]
514532
fn infer_builtin_macros_concat_with_lazy() {
515533
assert_snapshot!(

0 commit comments

Comments
 (0)