Skip to content

Commit c1d0859

Browse files
committed
ItemTree's ItemVisibilities has no identity, so deduplicate
1 parent fa7a6c1 commit c1d0859

File tree

7 files changed

+39
-38
lines changed

7 files changed

+39
-38
lines changed

src/tools/rust-analyzer/crates/base-db/src/input.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ use span::Edition;
2020
use triomphe::Arc;
2121
use vfs::{AbsPathBuf, AnchoredPath, FileId, VfsPath, file_set::FileSet};
2222

23-
use crate::{CrateWorkspaceData, EditionedFileId, RootQueryDb};
23+
use crate::{CrateWorkspaceData, EditionedFileId, FxIndexSet, RootQueryDb};
2424

2525
pub type ProcMacroPaths = FxHashMap<CrateBuilderId, Result<(String, AbsPathBuf), String>>;
2626

27-
type FxIndexSet<T> = indexmap::IndexSet<T, FxBuildHasher>;
28-
2927
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
3028
pub struct SourceRootId(pub u32);
3129

src/tools/rust-analyzer/crates/base-db/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use syntax::{Parse, SyntaxError, ast};
2828
use triomphe::Arc;
2929
pub use vfs::{AnchoredPath, AnchoredPathBuf, FileId, VfsPath, file_set::FileSet};
3030

31+
pub type FxIndexSet<T> = indexmap::IndexSet<T, rustc_hash::FxBuildHasher>;
32+
3133
#[macro_export]
3234
macro_rules! impl_intern_key {
3335
($id:ident, $loc:ident) => {

src/tools/rust-analyzer/crates/hir-def/src/expr_store/lower.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod path;
77

88
use std::mem;
99

10+
use base_db::FxIndexSet;
1011
use cfg::CfgOptions;
1112
use either::Either;
1213
use hir_expand::{
@@ -66,8 +67,6 @@ use crate::{
6667

6768
pub use self::path::hir_segment_to_ast_segment;
6869

69-
type FxIndexSet<K> = indexmap::IndexSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
70-
7170
pub(super) fn lower_body(
7271
db: &dyn DefDatabase,
7372
owner: DefWithBodyId,

src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl ItemTree {
246246
macro_calls,
247247
macro_rules,
248248
macro_defs,
249-
vis,
249+
vis: _,
250250
} = &mut **data;
251251

252252
uses.shrink_to_fit();
@@ -266,36 +266,13 @@ impl ItemTree {
266266
macro_calls.shrink_to_fit();
267267
macro_rules.shrink_to_fit();
268268
macro_defs.shrink_to_fit();
269-
270-
vis.arena.shrink_to_fit();
271269
}
272270
}
273271
}
274272

275273
#[derive(Default, Debug, Eq, PartialEq)]
276274
struct ItemVisibilities {
277-
arena: Arena<RawVisibility>,
278-
}
279-
280-
impl ItemVisibilities {
281-
fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
282-
match &vis {
283-
RawVisibility::Public => RawVisibilityId::PUB,
284-
RawVisibility::Module(path, explicitiy) if path.segments().is_empty() => {
285-
match (path.kind, explicitiy) {
286-
(PathKind::SELF, VisibilityExplicitness::Explicit) => {
287-
RawVisibilityId::PRIV_EXPLICIT
288-
}
289-
(PathKind::SELF, VisibilityExplicitness::Implicit) => {
290-
RawVisibilityId::PRIV_IMPLICIT
291-
}
292-
(PathKind::Crate, _) => RawVisibilityId::PUB_CRATE,
293-
_ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
294-
}
295-
}
296-
_ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
297-
}
298-
}
275+
arena: Box<[RawVisibility]>,
299276
}
300277

301278
#[derive(Default, Debug, Eq, PartialEq)]
@@ -577,7 +554,7 @@ impl Index<RawVisibilityId> for ItemTree {
577554
VisibilityExplicitness::Explicit,
578555
)
579556
}),
580-
_ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
557+
_ => &self.data().vis.arena[index.0 as usize],
581558
}
582559
}
583560
}
@@ -702,7 +679,7 @@ pub enum FieldsShape {
702679
}
703680

704681
/// Visibility of an item, not yet resolved.
705-
#[derive(Debug, Clone, PartialEq, Eq)]
682+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
706683
pub enum RawVisibility {
707684
/// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
708685
/// equivalent to `pub(self)`.

src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::{cell::OnceCell, collections::hash_map::Entry};
44

5+
use base_db::FxIndexSet;
56
use hir_expand::{
67
HirFileId,
78
mod_path::PathKind,
@@ -37,6 +38,7 @@ pub(super) struct Ctx<'a> {
3738
source_ast_id_map: Arc<AstIdMap>,
3839
span_map: OnceCell<SpanMap>,
3940
file: HirFileId,
41+
visibilities: FxIndexSet<RawVisibility>,
4042
}
4143

4244
impl<'a> Ctx<'a> {
@@ -47,6 +49,7 @@ impl<'a> Ctx<'a> {
4749
source_ast_id_map: db.ast_id_map(file),
4850
file,
4951
span_map: OnceCell::new(),
52+
visibilities: FxIndexSet::default(),
5053
}
5154
}
5255

@@ -57,6 +60,9 @@ impl<'a> Ctx<'a> {
5760
pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree {
5861
self.tree.top_level =
5962
item_owner.items().flat_map(|item| self.lower_mod_item(&item)).collect();
63+
if let Some(data) = &mut self.tree.data {
64+
data.vis.arena = self.visibilities.into_iter().collect();
65+
}
6066
self.tree
6167
}
6268

@@ -90,6 +96,9 @@ impl<'a> Ctx<'a> {
9096
}
9197
}
9298

99+
if let Some(data) = &mut self.tree.data {
100+
data.vis.arena = self.visibilities.into_iter().collect();
101+
}
93102
self.tree
94103
}
95104

@@ -115,7 +124,9 @@ impl<'a> Ctx<'a> {
115124
}
116125
}
117126
}
118-
127+
if let Some(data) = &mut self.tree.data {
128+
data.vis.arena = self.visibilities.into_iter().collect();
129+
}
119130
self.tree
120131
}
121132

@@ -370,7 +381,22 @@ impl<'a> Ctx<'a> {
370381
let vis = visibility_from_ast(self.db, item.visibility(), &mut |range| {
371382
self.span_map().span_for_range(range).ctx
372383
});
373-
self.data().vis.alloc(vis)
384+
match &vis {
385+
RawVisibility::Public => RawVisibilityId::PUB,
386+
RawVisibility::Module(path, explicitiy) if path.segments().is_empty() => {
387+
match (path.kind, explicitiy) {
388+
(PathKind::SELF, VisibilityExplicitness::Explicit) => {
389+
RawVisibilityId::PRIV_EXPLICIT
390+
}
391+
(PathKind::SELF, VisibilityExplicitness::Implicit) => {
392+
RawVisibilityId::PRIV_IMPLICIT
393+
}
394+
(PathKind::Crate, _) => RawVisibilityId::PUB_CRATE,
395+
_ => RawVisibilityId(self.visibilities.insert_full(vis).0 as u32),
396+
}
397+
}
398+
_ => RawVisibilityId(self.visibilities.insert_full(vis).0 as u32),
399+
}
374400
}
375401
}
376402

src/tools/rust-analyzer/crates/hir/src/symbols.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! File symbol extraction.
22
3+
use base_db::FxIndexSet;
34
use either::Either;
45
use hir_def::{
56
AdtId, AssocItemId, Complete, DefWithBodyId, ExternCrateId, HasModule, ImplId, Lookup, MacroId,
@@ -21,8 +22,6 @@ use syntax::{AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast
2122

2223
use crate::{HasCrate, Module, ModuleDef, Semantics};
2324

24-
pub type FxIndexSet<T> = indexmap::IndexSet<T, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
25-
2625
/// The actual data that is stored in the index. It should be as compact as
2726
/// possible.
2827
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

src/tools/rust-analyzer/crates/ide/src/runnables.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use ast::HasName;
55
use cfg::{CfgAtom, CfgExpr};
66
use hir::{
77
AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, HasSource, ModPath, Name, PathKind, Semantics,
8-
Symbol, db::HirDatabase, sym, symbols::FxIndexSet,
8+
Symbol, db::HirDatabase, sym,
99
};
1010
use ide_assists::utils::{has_test_related_attribute, test_related_attribute_syn};
1111
use ide_db::{
12-
FilePosition, FxHashMap, FxIndexMap, RootDatabase, SymbolKind,
12+
FilePosition, FxHashMap, FxIndexMap, FxIndexSet, RootDatabase, SymbolKind,
1313
base_db::RootQueryDb,
1414
defs::Definition,
1515
documentation::docs_from_attrs,

0 commit comments

Comments
 (0)