From 26a2a2433c7bae1533d07a38a6003e6f40fa97d9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 4 Feb 2021 13:44:54 +0100 Subject: Don't keep the parent DefMap alive via Arc This seems like it could easily leak a lot of memory since we don't currently run GC --- crates/hir_def/src/nameres/collector.rs | 9 +++++---- crates/hir_def/src/nameres/path_resolution.rs | 14 ++++++++------ crates/hir_def/src/nameres/tests.rs | 13 ++++++++++--- 3 files changed, 23 insertions(+), 13 deletions(-) (limited to 'crates/hir_def/src/nameres') diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 6e86cc4a7..f904a97de 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1449,10 +1449,11 @@ impl ModCollector<'_, '_> { if let Some(macro_call_id) = ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| { path.as_ident().and_then(|name| { - self.def_collector - .def_map - .ancestor_maps(self.module_id) - .find_map(|(map, module)| map[module].scope.get_legacy_macro(&name)) + self.def_collector.def_map.with_ancestor_maps( + self.def_collector.db, + self.module_id, + &mut |map, module| map[module].scope.get_legacy_macro(&name), + ) }) }) { diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index ecf75c777..2a3ac5d7b 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -110,6 +110,7 @@ impl DefMap { let mut result = ResolvePathResult::empty(ReachedFixedPoint::No); result.segment_index = Some(usize::max_value()); + let mut arc; let mut current_map = self; loop { let new = current_map.resolve_path_fp_with_macro_single( @@ -131,8 +132,9 @@ impl DefMap { match ¤t_map.block { Some(block) => { - current_map = &block.parent; - original_module = block.parent_module; + original_module = block.parent.local_id; + arc = block.parent.def_map(db); + current_map = &*arc; } None => return result, } @@ -152,7 +154,7 @@ impl DefMap { PathKind::DollarCrate(krate) => { if krate == self.krate { mark::hit!(macro_dollar_crate_self); - PerNs::types(self.crate_root().into(), Visibility::Public) + PerNs::types(self.crate_root(db).into(), Visibility::Public) } else { let def_map = db.crate_def_map(krate); let module = def_map.module_id(def_map.root); @@ -160,7 +162,7 @@ impl DefMap { PerNs::types(module.into(), Visibility::Public) } } - PathKind::Crate => PerNs::types(self.crate_root().into(), Visibility::Public), + PathKind::Crate => PerNs::types(self.crate_root(db).into(), Visibility::Public), // plain import or absolute path in 2015: crate-relative with // fallback to extern prelude (with the simplification in // rust-lang/rust#57745) @@ -206,10 +208,10 @@ impl DefMap { segments: path.segments.clone(), }; log::debug!("`super` path: {} -> {} in parent map", path, new_path); - return block.parent.resolve_path_fp_with_macro( + return block.parent.def_map(db).resolve_path_fp_with_macro( db, mode, - block.parent_module, + block.parent.local_id, &new_path, shadow, ); diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index 723481c36..bd3e2701b 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs @@ -11,7 +11,9 @@ use base_db::{fixture::WithFixture, SourceDatabase}; use expect_test::{expect, Expect}; use test_utils::mark; -use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; +use crate::{db::DefDatabase, test_db::TestDB}; + +use super::DefMap; fn compute_crate_def_map(ra_fixture: &str) -> Arc { let db = TestDB::with_files(ra_fixture); @@ -19,9 +21,14 @@ fn compute_crate_def_map(ra_fixture: &str) -> Arc { db.crate_def_map(krate) } +fn render_crate_def_map(ra_fixture: &str) -> String { + let db = TestDB::with_files(ra_fixture); + let krate = db.crate_graph().iter().next().unwrap(); + db.crate_def_map(krate).dump(&db) +} + fn check(ra_fixture: &str, expect: Expect) { - let def_map = compute_crate_def_map(ra_fixture); - let actual = def_map.dump(); + let actual = render_crate_def_map(ra_fixture); expect.assert_eq(&actual); } -- cgit v1.2.3