Skip to content

Commit fc36e0c

Browse files
committed
Auto merge of #17907 - ChayimFriedman2:no-once_cell, r=Veykril
internal: Replace once_cell with std's recently stabilized OnceCell/Lock and LazyCell/Lock This doesn't get rid of the once_cell dependency, unfortunately, since we have dependencies that use it, but it's a nice to do cleanup. And when our deps will eventually get rid of once_cell we will get rid of it for free.
2 parents 91aa3f4 + 955e609 commit fc36e0c

File tree

19 files changed

+49
-54
lines changed

19 files changed

+49
-54
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fst = { version = "0.4.7", default-features = false }
2323
indexmap.workspace = true
2424
itertools.workspace = true
2525
la-arena.workspace = true
26-
once_cell = "1.17.0"
2726
rustc-hash.workspace = true
2827
tracing.workspace = true
2928
smallvec.workspace = true

crates/hir-def/src/generics.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use hir_expand::{
1212
};
1313
use intern::Interned;
1414
use la_arena::{Arena, RawIdx};
15-
use once_cell::unsync::Lazy;
1615
use stdx::impl_from;
1716
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
1817
use triomphe::Arc;
@@ -394,11 +393,16 @@ impl GenericParams {
394393

395394
// Don't create an `Expander` if not needed since this
396395
// could cause a reparse after the `ItemTree` has been created due to the spanmap.
397-
let mut expander = Lazy::new(|| {
398-
(module.def_map(db), Expander::new(db, loc.id.file_id(), module))
399-
});
396+
let mut expander = None;
400397
for param in func_data.params.iter() {
401-
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
398+
generic_params.fill_implicit_impl_trait_args(
399+
db,
400+
&mut expander,
401+
&mut || {
402+
(module.def_map(db), Expander::new(db, loc.id.file_id(), module))
403+
},
404+
param,
405+
);
402406
}
403407
Interned::new(generic_params.finish())
404408
}
@@ -597,7 +601,9 @@ impl GenericParamsCollector {
597601
fn fill_implicit_impl_trait_args(
598602
&mut self,
599603
db: &dyn DefDatabase,
600-
exp: &mut Lazy<(Arc<DefMap>, Expander), impl FnOnce() -> (Arc<DefMap>, Expander)>,
604+
// FIXME: Change this back to `LazyCell` if https://github.com/rust-lang/libs-team/issues/429 is accepted.
605+
exp: &mut Option<(Arc<DefMap>, Expander)>,
606+
exp_fill: &mut dyn FnMut() -> (Arc<DefMap>, Expander),
601607
type_ref: &TypeRef,
602608
) {
603609
type_ref.walk(&mut |type_ref| {
@@ -617,7 +623,7 @@ impl GenericParamsCollector {
617623
}
618624
if let TypeRef::Macro(mc) = type_ref {
619625
let macro_call = mc.to_node(db.upcast());
620-
let (def_map, expander) = &mut **exp;
626+
let (def_map, expander) = exp.get_or_insert_with(&mut *exp_fill);
621627

622628
let module = expander.module.local_id;
623629
let resolver = |path: &_| {
@@ -637,8 +643,8 @@ impl GenericParamsCollector {
637643
{
638644
let ctx = expander.ctx(db);
639645
let type_ref = TypeRef::from_ast(&ctx, expanded.tree());
640-
self.fill_implicit_impl_trait_args(db, &mut *exp, &type_ref);
641-
exp.1.exit(mark);
646+
self.fill_implicit_impl_trait_args(db, &mut *exp, exp_fill, &type_ref);
647+
exp.get_or_insert_with(&mut *exp_fill).1.exit(mark);
642648
}
643649
}
644650
});

crates/hir-def/src/item_scope.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Describes items defined or visible (ie, imported) in a certain scope.
22
//! This is shared between modules and blocks.
33
4+
use std::sync::LazyLock;
5+
46
use base_db::CrateId;
57
use hir_expand::{attrs::AttrId, db::ExpandDatabase, name::Name, AstId, MacroCallId};
68
use indexmap::map::Entry;
79
use itertools::Itertools;
810
use la_arena::Idx;
9-
use once_cell::sync::Lazy;
1011
use rustc_hash::{FxHashMap, FxHashSet};
1112
use smallvec::{smallvec, SmallVec};
1213
use stdx::format_to;
@@ -129,7 +130,7 @@ struct DeriveMacroInvocation {
129130
derive_call_ids: SmallVec<[Option<MacroCallId>; 1]>,
130131
}
131132

132-
pub(crate) static BUILTIN_SCOPE: Lazy<FxIndexMap<Name, PerNs>> = Lazy::new(|| {
133+
pub(crate) static BUILTIN_SCOPE: LazyLock<FxIndexMap<Name, PerNs>> = LazyLock::new(|| {
133134
BuiltinType::all_builtin_types()
134135
.iter()
135136
.map(|(name, ty)| (name.clone(), PerNs::types((*ty).into(), Visibility::Public, None)))

crates/hir-def/src/item_tree.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::{
4040
fmt::{self, Debug},
4141
hash::{Hash, Hasher},
4242
ops::{Index, Range},
43+
sync::OnceLock,
4344
};
4445

4546
use ast::{AstNode, StructKind};
@@ -48,7 +49,6 @@ use either::Either;
4849
use hir_expand::{attrs::RawAttrs, name::Name, ExpandTo, HirFileId, InFile};
4950
use intern::{Interned, Symbol};
5051
use la_arena::{Arena, Idx, RawIdx};
51-
use once_cell::sync::OnceCell;
5252
use rustc_hash::FxHashMap;
5353
use smallvec::SmallVec;
5454
use span::{AstIdNode, FileAstId, SyntaxContextId};
@@ -101,7 +101,7 @@ pub struct ItemTree {
101101
impl ItemTree {
102102
pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
103103
let _p = tracing::info_span!("file_item_tree_query", ?file_id).entered();
104-
static EMPTY: OnceCell<Arc<ItemTree>> = OnceCell::new();
104+
static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new();
105105

106106
let syntax = db.parse_or_expand(file_id);
107107

@@ -152,7 +152,7 @@ impl ItemTree {
152152

153153
pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc<ItemTree> {
154154
let _p = tracing::info_span!("block_item_tree_query", ?block).entered();
155-
static EMPTY: OnceCell<Arc<ItemTree>> = OnceCell::new();
155+
static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new();
156156

157157
let loc = block.lookup(db);
158158
let block = loc.ast_id.to_node(db.upcast());
@@ -626,9 +626,9 @@ impl Index<RawVisibilityId> for ItemTree {
626626
type Output = RawVisibility;
627627
fn index(&self, index: RawVisibilityId) -> &Self::Output {
628628
static VIS_PUB: RawVisibility = RawVisibility::Public;
629-
static VIS_PRIV_IMPLICIT: OnceCell<RawVisibility> = OnceCell::new();
630-
static VIS_PRIV_EXPLICIT: OnceCell<RawVisibility> = OnceCell::new();
631-
static VIS_PUB_CRATE: OnceCell<RawVisibility> = OnceCell::new();
629+
static VIS_PRIV_IMPLICIT: OnceLock<RawVisibility> = OnceLock::new();
630+
static VIS_PRIV_EXPLICIT: OnceLock<RawVisibility> = OnceLock::new();
631+
static VIS_PUB_CRATE: OnceLock<RawVisibility> = OnceLock::new();
632632

633633
match index {
634634
RawVisibilityId::PRIV_IMPLICIT => VIS_PRIV_IMPLICIT.get_or_init(|| {

crates/hir-ty/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ chalk-ir.workspace = true
2929
chalk-recursive.workspace = true
3030
chalk-derive.workspace = true
3131
la-arena.workspace = true
32-
once_cell = "1.17.0"
3332
triomphe.workspace = true
3433
nohash-hasher.workspace = true
3534
typed-arena = "2.0.1"

crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Interface with `rustc_pattern_analysis`.
22
3+
use std::cell::LazyCell;
34
use std::fmt;
45

56
use hir_def::{DefWithBodyId, EnumId, EnumVariantId, HasModule, LocalFieldId, ModuleId, VariantId};
67
use intern::sym;
7-
use once_cell::unsync::Lazy;
88
use rustc_pattern_analysis::{
99
constructor::{Constructor, ConstructorSet, VariantVisibility},
1010
usefulness::{compute_match_usefulness, PlaceValidity, UsefulnessReport},
@@ -384,8 +384,9 @@ impl<'db> PatCx for MatchCheckCtx<'db> {
384384
let variant = Self::variant_id_for_adt(self.db, ctor, adt).unwrap();
385385

386386
// Whether we must not match the fields of this variant exhaustively.
387-
let is_non_exhaustive = Lazy::new(|| self.is_foreign_non_exhaustive(adt));
388-
let visibilities = Lazy::new(|| self.db.field_visibilities(variant));
387+
let is_non_exhaustive =
388+
LazyCell::new(|| self.is_foreign_non_exhaustive(adt));
389+
let visibilities = LazyCell::new(|| self.db.field_visibilities(variant));
389390

390391
self.list_variant_fields(ty, variant)
391392
.map(move |(fid, ty)| {

crates/hir-ty/src/infer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod pat;
2222
mod path;
2323
pub(crate) mod unify;
2424

25-
use std::{convert::identity, iter, ops::Index};
25+
use std::{cell::OnceCell, convert::identity, iter, ops::Index};
2626

2727
use chalk_ir::{
2828
cast::Cast,
@@ -49,7 +49,6 @@ use hir_expand::name::Name;
4949
use indexmap::IndexSet;
5050
use intern::sym;
5151
use la_arena::{ArenaMap, Entry};
52-
use once_cell::unsync::OnceCell;
5352
use rustc_hash::{FxHashMap, FxHashSet};
5453
use stdx::{always, never};
5554
use triomphe::Arc;

crates/hir-ty/src/lower.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! This usually involves resolving names, collecting generic arguments etc.
88
use std::{
9-
cell::{Cell, RefCell, RefMut},
9+
cell::{Cell, OnceCell, RefCell, RefMut},
1010
iter,
1111
ops::{self, Not as _},
1212
};
@@ -43,7 +43,6 @@ use hir_def::{
4343
use hir_expand::{name::Name, ExpandResult};
4444
use intern::Interned;
4545
use la_arena::{Arena, ArenaMap};
46-
use once_cell::unsync::OnceCell;
4746
use rustc_hash::FxHashSet;
4847
use rustc_pattern_analysis::Captures;
4948
use smallvec::SmallVec;

crates/hir-ty/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod traits;
1212
mod type_alias_impl_traits;
1313

1414
use std::env;
15+
use std::sync::LazyLock;
1516

1617
use base_db::SourceDatabaseFileInputExt as _;
1718
use expect_test::Expect;
@@ -25,7 +26,6 @@ use hir_def::{
2526
AssocItemId, DefWithBodyId, HasModule, LocalModuleId, Lookup, ModuleDefId,
2627
};
2728
use hir_expand::{db::ExpandDatabase, FileRange, InFile};
28-
use once_cell::race::OnceBool;
2929
use rustc_hash::FxHashMap;
3030
use stdx::format_to;
3131
use syntax::{
@@ -50,8 +50,8 @@ use crate::{
5050
// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
5151

5252
fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
53-
static ENABLE: OnceBool = OnceBool::new();
54-
if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
53+
static ENABLE: LazyLock<bool> = LazyLock::new(|| env::var("CHALK_DEBUG").is_ok());
54+
if !*ENABLE {
5555
return None;
5656
}
5757

crates/hir/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ itertools.workspace = true
2020
smallvec.workspace = true
2121
tracing.workspace = true
2222
triomphe.workspace = true
23-
once_cell = "1.17.1"
2423

2524
# local deps
2625
base-db.workspace = true

crates/ide-completion/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ cov-mark = "2.0.0-pre.1"
1717
itertools.workspace = true
1818
tracing.workspace = true
1919

20-
once_cell = "1.17.0"
2120
smallvec.workspace = true
2221

2322

crates/ide-completion/src/completions/attribute.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
44
5+
use std::sync::LazyLock;
6+
57
use ide_db::{
68
generated::lints::{
79
Lint, CLIPPY_LINTS, CLIPPY_LINT_GROUPS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS,
@@ -10,7 +12,6 @@ use ide_db::{
1012
FxHashMap, SymbolKind,
1113
};
1214
use itertools::Itertools;
13-
use once_cell::sync::Lazy;
1415
use syntax::{
1516
ast::{self, AttrKind},
1617
AstNode, SyntaxKind, T,
@@ -215,7 +216,7 @@ macro_rules! attrs {
215216
}
216217

217218
#[rustfmt::skip]
218-
static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
219+
static KIND_TO_ATTRIBUTES: LazyLock<FxHashMap<SyntaxKind, &[&str]>> = LazyLock::new(|| {
219220
use SyntaxKind::*;
220221
[
221222
(

crates/ide-db/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ tracing.workspace = true
1919
rayon.workspace = true
2020
fst = { version = "0.4.7", default-features = false }
2121
rustc-hash.workspace = true
22-
once_cell = "1.17.0"
2322
either.workspace = true
2423
itertools.workspace = true
2524
arrayvec.workspace = true

crates/ide-db/src/search.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! get a super-set of matches. Then, we confirm each match using precise
55
//! name resolution.
66
7+
use std::cell::LazyCell;
78
use std::mem;
89

910
use base_db::{salsa::Database, SourceDatabase, SourceRootDatabase};
@@ -12,7 +13,6 @@ use hir::{
1213
InFile, InRealFile, ModuleSource, PathResolution, Semantics, Visibility,
1314
};
1415
use memchr::memmem::Finder;
15-
use once_cell::unsync::Lazy;
1616
use parser::SyntaxKind;
1717
use rustc_hash::FxHashMap;
1818
use span::EditionedFileId;
@@ -543,7 +543,7 @@ impl<'a> FindUsages<'a> {
543543

544544
for (text, file_id, search_range) in scope_files(sema, &search_scope) {
545545
self.sema.db.unwind_if_cancelled();
546-
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
546+
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
547547

548548
// Search for occurrences of the items name
549549
for offset in match_indices(&text, finder, search_range) {
@@ -589,7 +589,7 @@ impl<'a> FindUsages<'a> {
589589

590590
for (text, file_id, search_range) in scope_files(sema, &scope) {
591591
self.sema.db.unwind_if_cancelled();
592-
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
592+
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
593593

594594
for offset in match_indices(&text, finder, search_range) {
595595
for name_ref in
@@ -641,7 +641,7 @@ impl<'a> FindUsages<'a> {
641641
let search_range =
642642
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));
643643

644-
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
644+
let tree = LazyCell::new(|| sema.parse(file_id).syntax().clone());
645645
let finder = &Finder::new("self");
646646

647647
for offset in match_indices(&text, finder, search_range) {

crates/ide-diagnostics/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ either.workspace = true
1818
itertools.workspace = true
1919
serde_json.workspace = true
2020
tracing.workspace = true
21-
once_cell = "1.17.0"
2221

2322
# local deps
2423
stdx.workspace = true

0 commit comments

Comments
 (0)