From 6b9c72567363edcff5f4e703646b7246fdf5c671 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jul 2020 17:18:19 +0200 Subject: Cap macro expansion depth for IDE features closes #4453 --- crates/ra_hir/src/source_analyzer.rs | 2 +- crates/ra_hir_expand/src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index d76345525..ecb54f653 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -352,7 +352,7 @@ impl SourceAnalyzer { let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| { self.resolver.resolve_path_as_macro(db.upcast(), &path) })?; - Some(macro_call_id.as_file()) + Some(macro_call_id.as_file()).filter(|it| it.expansion_level(db.upcast()) < 64) } pub(crate) fn resolve_variant( diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 5eac2605b..1cf6c1ba9 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -88,6 +88,25 @@ impl HirFileId { } } + pub fn expansion_level(self, db: &dyn db::AstDatabase) -> u32 { + let mut level = 0; + let mut curr = self; + while let HirFileIdRepr::MacroFile(macro_file) = curr.0 { + level += 1; + curr = match macro_file.macro_call_id { + MacroCallId::LazyMacro(id) => { + let loc = db.lookup_intern_macro(id); + loc.kind.file_id() + } + MacroCallId::EagerMacro(id) => { + let loc = db.lookup_intern_eager_expansion(id); + loc.file_id + } + }; + } + level + } + /// If this is a macro call, returns the syntax node of the call. pub fn call_node(self, db: &dyn db::AstDatabase) -> Option> { match self.0 { -- cgit v1.2.3