Arc CrateData::cfg_options

This commit is contained in:
Lukas Wirth 2024-04-06 13:55:10 +02:00
parent 336dee3415
commit f3567bb604
7 changed files with 29 additions and 31 deletions

View file

@ -285,10 +285,9 @@ pub struct CrateData {
/// For purposes of analysis, crates are anonymous (only names in /// For purposes of analysis, crates are anonymous (only names in
/// `Dependency` matters), this name should only be used for UI. /// `Dependency` matters), this name should only be used for UI.
pub display_name: Option<CrateDisplayName>, pub display_name: Option<CrateDisplayName>,
// FIXME: Arc this pub cfg_options: Arc<CfgOptions>,
pub cfg_options: CfgOptions,
/// The cfg options that could be used by the crate /// The cfg options that could be used by the crate
pub potential_cfg_options: Option<CfgOptions>, pub potential_cfg_options: Option<Arc<CfgOptions>>,
pub env: Env, pub env: Env,
pub dependencies: Vec<Dependency>, pub dependencies: Vec<Dependency>,
pub origin: CrateOrigin, pub origin: CrateOrigin,
@ -329,8 +328,8 @@ impl CrateGraph {
edition: Edition, edition: Edition,
display_name: Option<CrateDisplayName>, display_name: Option<CrateDisplayName>,
version: Option<String>, version: Option<String>,
cfg_options: CfgOptions, cfg_options: Arc<CfgOptions>,
potential_cfg_options: Option<CfgOptions>, potential_cfg_options: Option<Arc<CfgOptions>>,
env: Env, env: Env,
is_proc_macro: bool, is_proc_macro: bool,
origin: CrateOrigin, origin: CrateOrigin,

View file

@ -191,8 +191,6 @@ impl StructData {
let krate = loc.container.krate; let krate = loc.container.krate;
let item_tree = loc.id.item_tree(db); let item_tree = loc.id.item_tree(db);
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into()); 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 attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into());
let mut flags = StructFlags::NO_FLAGS; let mut flags = StructFlags::NO_FLAGS;
@ -219,7 +217,7 @@ impl StructData {
loc.id.file_id(), loc.id.file_id(),
loc.container.local_id, loc.container.local_id,
&item_tree, &item_tree,
&cfg_options, &db.crate_graph()[krate].cfg_options,
&strukt.fields, &strukt.fields,
None, None,
); );
@ -248,8 +246,6 @@ impl StructData {
let krate = loc.container.krate; let krate = loc.container.krate;
let item_tree = loc.id.item_tree(db); let item_tree = loc.id.item_tree(db);
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into()); 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 attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into());
let mut flags = StructFlags::NO_FLAGS; let mut flags = StructFlags::NO_FLAGS;
if attrs.by_key("rustc_has_incoherent_inherent_impls").exists() { if attrs.by_key("rustc_has_incoherent_inherent_impls").exists() {
@ -266,7 +262,7 @@ impl StructData {
loc.id.file_id(), loc.id.file_id(),
loc.container.local_id, loc.container.local_id,
&item_tree, &item_tree,
&cfg_options, &db.crate_graph()[krate].cfg_options,
&union.fields, &union.fields,
None, None,
); );
@ -338,7 +334,6 @@ impl EnumVariantData {
let container = loc.parent.lookup(db).container; let container = loc.parent.lookup(db).container;
let krate = container.krate; let krate = container.krate;
let item_tree = loc.id.item_tree(db); 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 variant = &item_tree[loc.id.value];
let (var_data, diagnostics) = lower_fields( let (var_data, diagnostics) = lower_fields(
@ -347,7 +342,7 @@ impl EnumVariantData {
loc.id.file_id(), loc.id.file_id(),
container.local_id, container.local_id,
&item_tree, &item_tree,
&cfg_options, &db.crate_graph()[krate].cfg_options,
&variant.fields, &variant.fields,
Some(item_tree[loc.parent.lookup(db).id.value].visibility), Some(item_tree[loc.parent.lookup(db).id.value].visibility),
); );

View file

@ -11,6 +11,7 @@ use hir_expand::{
}; };
use limit::Limit; use limit::Limit;
use syntax::{ast, Parse}; use syntax::{ast, Parse};
use triomphe::Arc;
use crate::{ use crate::{
attr::Attrs, db::DefDatabase, lower::LowerCtx, path::Path, AsMacroCall, MacroId, ModuleId, attr::Attrs, db::DefDatabase, lower::LowerCtx, path::Path, AsMacroCall, MacroId, ModuleId,
@ -19,7 +20,7 @@ use crate::{
#[derive(Debug)] #[derive(Debug)]
pub struct Expander { pub struct Expander {
cfg_options: CfgOptions, cfg_options: Arc<CfgOptions>,
span_map: OnceCell<SpanMap>, span_map: OnceCell<SpanMap>,
current_file_id: HirFileId, current_file_id: HirFileId,
pub(crate) module: ModuleId, pub(crate) module: ModuleId,

View file

@ -260,11 +260,11 @@ impl Crate {
doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") 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<CfgOptions> {
db.crate_graph()[self.id].cfg_options.clone() 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<CfgOptions> {
let data = &db.crate_graph()[self.id]; let data = &db.crate_graph()[self.id];
data.potential_cfg_options.clone().unwrap_or_else(|| data.cfg_options.clone()) data.potential_cfg_options.clone().unwrap_or_else(|| data.cfg_options.clone())
} }

View file

@ -252,7 +252,7 @@ impl Analysis {
Edition::CURRENT, Edition::CURRENT,
None, None,
None, None,
cfg_options.clone(), Arc::new(cfg_options),
None, None,
Env::default(), Env::default(),
false, false,

View file

@ -913,12 +913,14 @@ fn project_json_to_crate_graph(
*edition, *edition,
display_name.clone(), display_name.clone(),
version.clone(), version.clone(),
Arc::new(
target_cfgs target_cfgs
.iter() .iter()
.chain(cfg.iter()) .chain(cfg.iter())
.chain(iter::once(&r_a_cfg_flag)) .chain(iter::once(&r_a_cfg_flag))
.cloned() .cloned()
.collect(), .collect(),
),
None, None,
env, env,
*is_proc_macro, *is_proc_macro,
@ -1179,6 +1181,7 @@ fn detached_files_to_crate_graph(
let mut cfg_options = create_cfg_options(rustc_cfg); let mut cfg_options = create_cfg_options(rustc_cfg);
cfg_options.insert_atom("rust_analyzer".into()); cfg_options.insert_atom("rust_analyzer".into());
let cfg_options = Arc::new(cfg_options);
for detached_file in detached_files { for detached_file in detached_files {
let file_id = match load(detached_file) { let file_id = match load(detached_file) {
@ -1380,8 +1383,8 @@ fn add_target_crate_root(
edition, edition,
Some(display_name), Some(display_name),
Some(pkg.version.to_string()), Some(pkg.version.to_string()),
cfg_options, Arc::new(cfg_options),
potential_cfg_options, potential_cfg_options.map(Arc::new),
env, env,
matches!(kind, TargetKind::Lib { is_proc_macro: true }), matches!(kind, TargetKind::Lib { is_proc_macro: true }),
origin, origin,
@ -1437,7 +1440,7 @@ fn sysroot_to_crate_graph(
let diff = CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap(); let diff = CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap();
for (cid, c) in cg.iter_mut() { for (cid, c) in cg.iter_mut() {
// uninject `test` flag so `core` keeps working. // 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 // patch the origin
if c.origin.is_local() { if c.origin.is_local() {
let lang_crate = LangCrateOrigin::from( let lang_crate = LangCrateOrigin::from(
@ -1486,7 +1489,7 @@ fn sysroot_to_crate_graph(
(SysrootPublicDeps { deps: pub_deps }, libproc_macro) (SysrootPublicDeps { deps: pub_deps }, libproc_macro)
} }
SysrootMode::Stitched(stitched) => { 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<SysrootCrate, CrateId> = stitched let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = stitched
.crates() .crates()
.filter_map(|krate| { .filter_map(|krate| {

View file

@ -189,8 +189,8 @@ impl ChangeFixture {
meta.edition, meta.edition,
Some(crate_name.clone().into()), Some(crate_name.clone().into()),
version, version,
meta.cfg.clone(), From::from(meta.cfg.clone()),
Some(meta.cfg), Some(From::from(meta.cfg)),
meta.env, meta.env,
false, false,
origin, origin,
@ -227,8 +227,8 @@ impl ChangeFixture {
Edition::CURRENT, Edition::CURRENT,
Some(CrateName::new("test").unwrap().into()), Some(CrateName::new("test").unwrap().into()),
None, None,
default_cfg.clone(), From::from(default_cfg.clone()),
Some(default_cfg), Some(From::from(default_cfg)),
default_env, default_env,
false, false,
CrateOrigin::Local { repo: None, name: None }, CrateOrigin::Local { repo: None, name: None },