Skip to content

Commit 92cc6c9

Browse files
committed
Add recursive limit in expression macro expansion
1 parent 92b561b commit 92cc6c9

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/ra_hir_def/src/body.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) struct Expander {
3030
hygiene: Hygiene,
3131
ast_id_map: Arc<AstIdMap>,
3232
module: ModuleId,
33+
recursive_limit: usize,
3334
}
3435

3536
impl Expander {
@@ -41,7 +42,7 @@ impl Expander {
4142
let crate_def_map = db.crate_def_map(module.krate);
4243
let hygiene = Hygiene::new(db.upcast(), current_file_id);
4344
let ast_id_map = db.ast_id_map(current_file_id);
44-
Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module }
45+
Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 }
4546
}
4647

4748
pub(crate) fn enter_expand<T: ast::AstNode>(
@@ -50,6 +51,10 @@ impl Expander {
5051
local_scope: Option<&ItemScope>,
5152
macro_call: ast::MacroCall,
5253
) -> Option<(Mark, T)> {
54+
if self.recursive_limit > 1024 {
55+
return None;
56+
}
57+
5358
let macro_call = InFile::new(self.current_file_id, &macro_call);
5459

5560
if let Some(call_id) = macro_call.as_call_id(db, |path| {
@@ -73,6 +78,7 @@ impl Expander {
7378
self.hygiene = Hygiene::new(db.upcast(), file_id);
7479
self.current_file_id = file_id;
7580
self.ast_id_map = db.ast_id_map(file_id);
81+
self.recursive_limit += 1;
7682

7783
return Some((mark, expr));
7884
}
@@ -88,6 +94,7 @@ impl Expander {
8894
self.hygiene = Hygiene::new(db.upcast(), mark.file_id);
8995
self.current_file_id = mark.file_id;
9096
self.ast_id_map = mem::take(&mut mark.ast_id_map);
97+
self.recursive_limit -= 1;
9198
mark.bomb.defuse();
9299
}
93100

0 commit comments

Comments
 (0)