3673: Add recursive limit for macro expansion when expanding expression r=flodiebold a=edwin0cheng

cc @flodiebold 

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot] 2020-03-21 22:39:06 +00:00 committed by GitHub
commit 6fe956420f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ pub(crate) struct Expander {
hygiene: Hygiene,
ast_id_map: Arc<AstIdMap>,
module: ModuleId,
recursive_limit: usize,
}
impl Expander {
@ -41,7 +42,7 @@ impl Expander {
let crate_def_map = db.crate_def_map(module.krate);
let hygiene = Hygiene::new(db.upcast(), current_file_id);
let ast_id_map = db.ast_id_map(current_file_id);
Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module }
Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 }
}
pub(crate) fn enter_expand<T: ast::AstNode>(
@ -50,6 +51,10 @@ impl Expander {
local_scope: Option<&ItemScope>,
macro_call: ast::MacroCall,
) -> Option<(Mark, T)> {
if self.recursive_limit > 1024 {
return None;
}
let macro_call = InFile::new(self.current_file_id, &macro_call);
if let Some(call_id) = macro_call.as_call_id(db, |path| {
@ -73,6 +78,7 @@ impl Expander {
self.hygiene = Hygiene::new(db.upcast(), file_id);
self.current_file_id = file_id;
self.ast_id_map = db.ast_id_map(file_id);
self.recursive_limit += 1;
return Some((mark, expr));
}
@ -88,6 +94,7 @@ impl Expander {
self.hygiene = Hygiene::new(db.upcast(), mark.file_id);
self.current_file_id = mark.file_id;
self.ast_id_map = mem::take(&mut mark.ast_id_map);
self.recursive_limit -= 1;
mark.bomb.defuse();
}