Skip to content

Commit f3567bb

Browse files
committed
Arc CrateData::cfg_options
1 parent 336dee3 commit f3567bb

File tree

7 files changed

+29
-31
lines changed

7 files changed

+29
-31
lines changed

crates/base-db/src/input.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,9 @@ pub struct CrateData {
285285
/// For purposes of analysis, crates are anonymous (only names in
286286
/// `Dependency` matters), this name should only be used for UI.
287287
pub display_name: Option<CrateDisplayName>,
288-
// FIXME: Arc this
289-
pub cfg_options: CfgOptions,
288+
pub cfg_options: Arc<CfgOptions>,
290289
/// The cfg options that could be used by the crate
291-
pub potential_cfg_options: Option<CfgOptions>,
290+
pub potential_cfg_options: Option<Arc<CfgOptions>>,
292291
pub env: Env,
293292
pub dependencies: Vec<Dependency>,
294293
pub origin: CrateOrigin,
@@ -329,8 +328,8 @@ impl CrateGraph {
329328
edition: Edition,
330329
display_name: Option<CrateDisplayName>,
331330
version: Option<String>,
332-
cfg_options: CfgOptions,
333-
potential_cfg_options: Option<CfgOptions>,
331+
cfg_options: Arc<CfgOptions>,
332+
potential_cfg_options: Option<Arc<CfgOptions>>,
334333
env: Env,
335334
is_proc_macro: bool,
336335
origin: CrateOrigin,

crates/hir-def/src/data/adt.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ impl StructData {
191191
let krate = loc.container.krate;
192192
let item_tree = loc.id.item_tree(db);
193193
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into());
194-
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
195-
196194
let attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into());
197195

198196
let mut flags = StructFlags::NO_FLAGS;
@@ -219,7 +217,7 @@ impl StructData {
219217
loc.id.file_id(),
220218
loc.container.local_id,
221219
&item_tree,
222-
&cfg_options,
220+
&db.crate_graph()[krate].cfg_options,
223221
&strukt.fields,
224222
None,
225223
);
@@ -248,8 +246,6 @@ impl StructData {
248246
let krate = loc.container.krate;
249247
let item_tree = loc.id.item_tree(db);
250248
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into());
251-
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
252-
253249
let attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into());
254250
let mut flags = StructFlags::NO_FLAGS;
255251
if attrs.by_key("rustc_has_incoherent_inherent_impls").exists() {
@@ -266,7 +262,7 @@ impl StructData {
266262
loc.id.file_id(),
267263
loc.container.local_id,
268264
&item_tree,
269-
&cfg_options,
265+
&db.crate_graph()[krate].cfg_options,
270266
&union.fields,
271267
None,
272268
);
@@ -338,7 +334,6 @@ impl EnumVariantData {
338334
let container = loc.parent.lookup(db).container;
339335
let krate = container.krate;
340336
let item_tree = loc.id.item_tree(db);
341-
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
342337
let variant = &item_tree[loc.id.value];
343338

344339
let (var_data, diagnostics) = lower_fields(
@@ -347,7 +342,7 @@ impl EnumVariantData {
347342
loc.id.file_id(),
348343
container.local_id,
349344
&item_tree,
350-
&cfg_options,
345+
&db.crate_graph()[krate].cfg_options,
351346
&variant.fields,
352347
Some(item_tree[loc.parent.lookup(db).id.value].visibility),
353348
);

crates/hir-def/src/expander.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use hir_expand::{
1111
};
1212
use limit::Limit;
1313
use syntax::{ast, Parse};
14+
use triomphe::Arc;
1415

1516
use crate::{
1617
attr::Attrs, db::DefDatabase, lower::LowerCtx, path::Path, AsMacroCall, MacroId, ModuleId,
@@ -19,7 +20,7 @@ use crate::{
1920

2021
#[derive(Debug)]
2122
pub struct Expander {
22-
cfg_options: CfgOptions,
23+
cfg_options: Arc<CfgOptions>,
2324
span_map: OnceCell<SpanMap>,
2425
current_file_id: HirFileId,
2526
pub(crate) module: ModuleId,

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ impl Crate {
260260
doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
261261
}
262262

263-
pub fn cfg(&self, db: &dyn HirDatabase) -> CfgOptions {
263+
pub fn cfg(&self, db: &dyn HirDatabase) -> Arc<CfgOptions> {
264264
db.crate_graph()[self.id].cfg_options.clone()
265265
}
266266

267-
pub fn potential_cfg(&self, db: &dyn HirDatabase) -> CfgOptions {
267+
pub fn potential_cfg(&self, db: &dyn HirDatabase) -> Arc<CfgOptions> {
268268
let data = &db.crate_graph()[self.id];
269269
data.potential_cfg_options.clone().unwrap_or_else(|| data.cfg_options.clone())
270270
}

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl Analysis {
252252
Edition::CURRENT,
253253
None,
254254
None,
255-
cfg_options.clone(),
255+
Arc::new(cfg_options),
256256
None,
257257
Env::default(),
258258
false,

crates/project-model/src/workspace.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -913,12 +913,14 @@ fn project_json_to_crate_graph(
913913
*edition,
914914
display_name.clone(),
915915
version.clone(),
916-
target_cfgs
917-
.iter()
918-
.chain(cfg.iter())
919-
.chain(iter::once(&r_a_cfg_flag))
920-
.cloned()
921-
.collect(),
916+
Arc::new(
917+
target_cfgs
918+
.iter()
919+
.chain(cfg.iter())
920+
.chain(iter::once(&r_a_cfg_flag))
921+
.cloned()
922+
.collect(),
923+
),
922924
None,
923925
env,
924926
*is_proc_macro,
@@ -1179,6 +1181,7 @@ fn detached_files_to_crate_graph(
11791181

11801182
let mut cfg_options = create_cfg_options(rustc_cfg);
11811183
cfg_options.insert_atom("rust_analyzer".into());
1184+
let cfg_options = Arc::new(cfg_options);
11821185

11831186
for detached_file in detached_files {
11841187
let file_id = match load(detached_file) {
@@ -1380,8 +1383,8 @@ fn add_target_crate_root(
13801383
edition,
13811384
Some(display_name),
13821385
Some(pkg.version.to_string()),
1383-
cfg_options,
1384-
potential_cfg_options,
1386+
Arc::new(cfg_options),
1387+
potential_cfg_options.map(Arc::new),
13851388
env,
13861389
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
13871390
origin,
@@ -1437,7 +1440,7 @@ fn sysroot_to_crate_graph(
14371440
let diff = CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap();
14381441
for (cid, c) in cg.iter_mut() {
14391442
// uninject `test` flag so `core` keeps working.
1440-
c.cfg_options.apply_diff(diff.clone());
1443+
Arc::make_mut(&mut c.cfg_options).apply_diff(diff.clone());
14411444
// patch the origin
14421445
if c.origin.is_local() {
14431446
let lang_crate = LangCrateOrigin::from(
@@ -1486,7 +1489,7 @@ fn sysroot_to_crate_graph(
14861489
(SysrootPublicDeps { deps: pub_deps }, libproc_macro)
14871490
}
14881491
SysrootMode::Stitched(stitched) => {
1489-
let cfg_options = create_cfg_options(rustc_cfg);
1492+
let cfg_options = Arc::new(create_cfg_options(rustc_cfg));
14901493
let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = stitched
14911494
.crates()
14921495
.filter_map(|krate| {

crates/test-fixture/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ impl ChangeFixture {
189189
meta.edition,
190190
Some(crate_name.clone().into()),
191191
version,
192-
meta.cfg.clone(),
193-
Some(meta.cfg),
192+
From::from(meta.cfg.clone()),
193+
Some(From::from(meta.cfg)),
194194
meta.env,
195195
false,
196196
origin,
@@ -227,8 +227,8 @@ impl ChangeFixture {
227227
Edition::CURRENT,
228228
Some(CrateName::new("test").unwrap().into()),
229229
None,
230-
default_cfg.clone(),
231-
Some(default_cfg),
230+
From::from(default_cfg.clone()),
231+
Some(From::from(default_cfg)),
232232
default_env,
233233
false,
234234
CrateOrigin::Local { repo: None, name: None },

0 commit comments

Comments
 (0)