From f3567bb604eef8f05ec14259f2f65a21cc776c97 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 6 Apr 2024 13:55:10 +0200 Subject: [PATCH] Arc CrateData::cfg_options --- crates/base-db/src/input.rs | 9 ++++----- crates/hir-def/src/data/adt.rs | 11 +++-------- crates/hir-def/src/expander.rs | 3 ++- crates/hir/src/lib.rs | 4 ++-- crates/ide/src/lib.rs | 2 +- crates/project-model/src/workspace.rs | 23 +++++++++++++---------- crates/test-fixture/src/lib.rs | 8 ++++---- 7 files changed, 29 insertions(+), 31 deletions(-) diff --git a/crates/base-db/src/input.rs b/crates/base-db/src/input.rs index 6ea1e676a1..1924ce578a 100644 --- a/crates/base-db/src/input.rs +++ b/crates/base-db/src/input.rs @@ -285,10 +285,9 @@ pub struct CrateData { /// For purposes of analysis, crates are anonymous (only names in /// `Dependency` matters), this name should only be used for UI. pub display_name: Option, - // FIXME: Arc this - pub cfg_options: CfgOptions, + pub cfg_options: Arc, /// The cfg options that could be used by the crate - pub potential_cfg_options: Option, + pub potential_cfg_options: Option>, pub env: Env, pub dependencies: Vec, pub origin: CrateOrigin, @@ -329,8 +328,8 @@ impl CrateGraph { edition: Edition, display_name: Option, version: Option, - cfg_options: CfgOptions, - potential_cfg_options: Option, + cfg_options: Arc, + potential_cfg_options: Option>, env: Env, is_proc_macro: bool, origin: CrateOrigin, diff --git a/crates/hir-def/src/data/adt.rs b/crates/hir-def/src/data/adt.rs index a7461b78af..5330db7c80 100644 --- a/crates/hir-def/src/data/adt.rs +++ b/crates/hir-def/src/data/adt.rs @@ -191,8 +191,6 @@ impl StructData { let krate = loc.container.krate; let item_tree = loc.id.item_tree(db); let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into()); - let cfg_options = db.crate_graph()[krate].cfg_options.clone(); - let attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()); let mut flags = StructFlags::NO_FLAGS; @@ -219,7 +217,7 @@ impl StructData { loc.id.file_id(), loc.container.local_id, &item_tree, - &cfg_options, + &db.crate_graph()[krate].cfg_options, &strukt.fields, None, ); @@ -248,8 +246,6 @@ impl StructData { let krate = loc.container.krate; let item_tree = loc.id.item_tree(db); let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into()); - let cfg_options = db.crate_graph()[krate].cfg_options.clone(); - let attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()); let mut flags = StructFlags::NO_FLAGS; if attrs.by_key("rustc_has_incoherent_inherent_impls").exists() { @@ -266,7 +262,7 @@ impl StructData { loc.id.file_id(), loc.container.local_id, &item_tree, - &cfg_options, + &db.crate_graph()[krate].cfg_options, &union.fields, None, ); @@ -338,7 +334,6 @@ impl EnumVariantData { let container = loc.parent.lookup(db).container; let krate = container.krate; let item_tree = loc.id.item_tree(db); - let cfg_options = db.crate_graph()[krate].cfg_options.clone(); let variant = &item_tree[loc.id.value]; let (var_data, diagnostics) = lower_fields( @@ -347,7 +342,7 @@ impl EnumVariantData { loc.id.file_id(), container.local_id, &item_tree, - &cfg_options, + &db.crate_graph()[krate].cfg_options, &variant.fields, Some(item_tree[loc.parent.lookup(db).id.value].visibility), ); diff --git a/crates/hir-def/src/expander.rs b/crates/hir-def/src/expander.rs index 3084e06c1b..73ce942c58 100644 --- a/crates/hir-def/src/expander.rs +++ b/crates/hir-def/src/expander.rs @@ -11,6 +11,7 @@ use hir_expand::{ }; use limit::Limit; use syntax::{ast, Parse}; +use triomphe::Arc; use crate::{ attr::Attrs, db::DefDatabase, lower::LowerCtx, path::Path, AsMacroCall, MacroId, ModuleId, @@ -19,7 +20,7 @@ use crate::{ #[derive(Debug)] pub struct Expander { - cfg_options: CfgOptions, + cfg_options: Arc, span_map: OnceCell, current_file_id: HirFileId, pub(crate) module: ModuleId, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 9b9f710d9a..c4e051bff5 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -260,11 +260,11 @@ impl Crate { doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") } - pub fn cfg(&self, db: &dyn HirDatabase) -> CfgOptions { + pub fn cfg(&self, db: &dyn HirDatabase) -> Arc { db.crate_graph()[self.id].cfg_options.clone() } - pub fn potential_cfg(&self, db: &dyn HirDatabase) -> CfgOptions { + pub fn potential_cfg(&self, db: &dyn HirDatabase) -> Arc { let data = &db.crate_graph()[self.id]; data.potential_cfg_options.clone().unwrap_or_else(|| data.cfg_options.clone()) } diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index ad48d80389..e13060e4d7 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -252,7 +252,7 @@ impl Analysis { Edition::CURRENT, None, None, - cfg_options.clone(), + Arc::new(cfg_options), None, Env::default(), false, diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs index b59c0d59e4..00e9c12988 100644 --- a/crates/project-model/src/workspace.rs +++ b/crates/project-model/src/workspace.rs @@ -913,12 +913,14 @@ fn project_json_to_crate_graph( *edition, display_name.clone(), version.clone(), - target_cfgs - .iter() - .chain(cfg.iter()) - .chain(iter::once(&r_a_cfg_flag)) - .cloned() - .collect(), + Arc::new( + target_cfgs + .iter() + .chain(cfg.iter()) + .chain(iter::once(&r_a_cfg_flag)) + .cloned() + .collect(), + ), None, env, *is_proc_macro, @@ -1179,6 +1181,7 @@ fn detached_files_to_crate_graph( let mut cfg_options = create_cfg_options(rustc_cfg); cfg_options.insert_atom("rust_analyzer".into()); + let cfg_options = Arc::new(cfg_options); for detached_file in detached_files { let file_id = match load(detached_file) { @@ -1380,8 +1383,8 @@ fn add_target_crate_root( edition, Some(display_name), Some(pkg.version.to_string()), - cfg_options, - potential_cfg_options, + Arc::new(cfg_options), + potential_cfg_options.map(Arc::new), env, matches!(kind, TargetKind::Lib { is_proc_macro: true }), origin, @@ -1437,7 +1440,7 @@ fn sysroot_to_crate_graph( let diff = CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap(); for (cid, c) in cg.iter_mut() { // uninject `test` flag so `core` keeps working. - c.cfg_options.apply_diff(diff.clone()); + Arc::make_mut(&mut c.cfg_options).apply_diff(diff.clone()); // patch the origin if c.origin.is_local() { let lang_crate = LangCrateOrigin::from( @@ -1486,7 +1489,7 @@ fn sysroot_to_crate_graph( (SysrootPublicDeps { deps: pub_deps }, libproc_macro) } SysrootMode::Stitched(stitched) => { - let cfg_options = create_cfg_options(rustc_cfg); + let cfg_options = Arc::new(create_cfg_options(rustc_cfg)); let sysroot_crates: FxHashMap = stitched .crates() .filter_map(|krate| { diff --git a/crates/test-fixture/src/lib.rs b/crates/test-fixture/src/lib.rs index c8d785f83e..7ce83078cb 100644 --- a/crates/test-fixture/src/lib.rs +++ b/crates/test-fixture/src/lib.rs @@ -189,8 +189,8 @@ impl ChangeFixture { meta.edition, Some(crate_name.clone().into()), version, - meta.cfg.clone(), - Some(meta.cfg), + From::from(meta.cfg.clone()), + Some(From::from(meta.cfg)), meta.env, false, origin, @@ -227,8 +227,8 @@ impl ChangeFixture { Edition::CURRENT, Some(CrateName::new("test").unwrap().into()), None, - default_cfg.clone(), - Some(default_cfg), + From::from(default_cfg.clone()), + Some(From::from(default_cfg)), default_env, false, CrateOrigin::Local { repo: None, name: None },