8000 `ItemTree`'s `ItemVisibilities` has no identity, so deduplicate by Veykril · Pull Request #19980 · rust-lang/rust-analyzer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ItemTree's ItemVisibilities has no identity, so deduplicate #19980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ use span::Edition;
use triomphe::Arc;
use vfs::{AbsPathBuf, AnchoredPath, FileId, VfsPath, file_set::FileSet};

use crate::{CrateWorkspaceData, EditionedFileId, RootQueryDb};
use crate::{CrateWorkspaceData, EditionedFileId, FxIndexSet, RootQueryDb};

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

type FxIndexSet<T> = indexmap::IndexSet<T, FxBuildHasher>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SourceRootId(pub u32);

Expand Down
2 changes: 2 additions & 0 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use syntax::{Parse, SyntaxError, ast};
use triomphe::Arc;
pub use vfs::{AnchoredPath, AnchoredPathBuf, FileId, VfsPath, file_set::FileSet};

pub type FxIndexSet<T> = indexmap::IndexSet<T, rustc_hash::FxBuildHasher>;

#[macro_export]
macro_rules! impl_intern_key {
($id:ident, $loc:ident) => {
Expand Down
3 changes: 1 addition & 2 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod path;

use std::mem;

use base_db::FxIndexSet;
use cfg::CfgOptions;
use either::Either;
use hir_expand::{
Expand Down Expand Up @@ -66,8 +67,6 @@ use crate::{

pub use self::path::hir_segment_to_ast_segment;

type FxIndexSet<K> = indexmap::IndexSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

pub(super) fn lower_body(
db: &dyn DefDatabase,
owner: DefWithBodyId,
Expand Down
31 changes: 4 additions & 27 deletions crates/hir-def/src/item_tree.rs
< 10000 /div>
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl ItemTree {
macro_calls,
macro_rules,
macro_defs,
vis,
vis: _,
} = &mut **data;

uses.shrink_to_fit();
Expand All @@ -266,36 +266,13 @@ impl ItemTree {
macro_calls.shrink_to_fit();
macro_rules.shrink_to_fit();
macro_defs.shrink_to_fit();

vis.arena.shrink_to_fit();
}
}
}

#[derive(Default, Debug, Eq, PartialEq)]
struct ItemVisibilities {
arena: Arena<RawVisibility>,
}

impl ItemVisibilities {
fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
match &vis {
RawVisibility::Public => RawVisibilityId::PUB,
RawVisibility::Module(path, explicitiy) if path.segments().is_empty() => {
match (path.kind, explicitiy) {
(PathKind::SELF, VisibilityExplicitness::Explicit) => {
RawVisibilityId::PRIV_EXPLICIT
}
(PathKind::SELF, VisibilityExplicitness::Implicit) => {
RawVisibilityId::PRIV_IMPLICIT
}
(PathKind::Crate, _) => RawVisibilityId::PUB_CRATE,
_ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
}
}
_ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
}
}
arena: Box<[RawVisibility]>,
}

#[derive(Default, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -577,7 +554,7 @@ impl Index<RawVisibilityId> for ItemTree {
VisibilityExplicitness::Explicit,
)
}),
_ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
_ => &self.data().vis.arena[index.0 as usize],
}
}
}
Expand Down Expand Up @@ -702,7 +679,7 @@ pub enum FieldsShape {
}

/// Visibility of an item, not yet resolved.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RawVisibility {
/// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
/// equivalent to `pub(self)`.
Expand Down
30 changes: 28 additions & 2 deletions crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{cell::OnceCell, collections::hash_map::Entry};

use base_db::FxIndexSet;
use hir_expand::{
HirFileId,
mod_path::PathKind,
Expand Down Expand Up @@ -37,6 +38,7 @@ pub(super) struct Ctx<'a> {
source_ast_id_map: Arc<AstIdMap>,
span_map: OnceCell<SpanMap>,
file: HirFileId,
visibilities: FxIndexSet<RawVisibility>,
}

impl<'a> Ctx<'a> {
Expand All @@ -47,6 +49,7 @@ impl<'a> Ctx<'a> {
source_ast_id_map: db.ast_id_map(file),
file,
span_map: OnceCell::new(),
visibilities: FxIndexSet::default(),
}
}

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

Expand Down Expand Up @@ -90,6 +96,9 @@ impl<'a> Ctx<'a> {
}
}

if let Some(data) = &mut self.tree.data {
data.vis.arena = self.visibilities.into_iter().collect();
}
self.tree
}

Expand All @@ -115,7 +124,9 @@ impl<'a> Ctx<'a> {
}
}
}

if let Some(data) = &mut self.tree.data {
data.vis.arena = self.visibilities.into_iter().collect();
}
self.tree
}

Expand Down Expand Up @@ -370,7 +381,22 @@ impl<'a> Ctx<'a> {
let vis = visibility_from_ast(self.db, item.visibility(), &mut |range| {
self.span_map().span_for_range(range).ctx
});
self.data().vis.alloc(vis)
match &vis {
RawVisibility::Public => RawVisibilityId::PUB,
RawVisibility::Module(path, explicitiy) if path.segments().is_empty() => {
match (path.kind, explicitiy) {
(PathKind::SELF, VisibilityExplicitness::Explicit) => {
RawVisibilityId::PRIV_EXPLICIT
}
(PathKind::SELF, VisibilityExplicitness::Implicit) => {
RawVisibilityId::PRIV_IMPLICIT
}
(PathKind::Crate, _) => RawVisibilityId::PUB_CRATE,
_ => RawVisibilityId(self.visibilities.insert_full(vis).0 as u32),
}
}
_ => RawVisibilityId(self.visibilities.insert_full(vis).0 as u32),
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/hir/src/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! File symbol extraction.

use base_db::FxIndexSet;
use either::Either;
use hir_def::{
AdtId, AssocItemId, Complete, DefWithBodyId, ExternCrateId, HasModule, ImplId, Lookup, MacroId,
Expand All @@ -21,8 +22,6 @@ use syntax::{AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast

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

pub type FxIndexSet<T> = indexmap::IndexSet<T, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

/// The actual data that is stored in the index. It should be as compact as
/// possible.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/runnables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use ast::HasName;
use cfg::{CfgAtom, CfgExpr};
use hir::{
AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, HasSource, ModPath, Name, PathKind, Semantics,
Symbol, db::HirDatabase, sym, symbols::FxIndexSet,
Symbol, db::HirDatabase, sym,
};
use ide_assists::utils::{has_test_related_attribute, test_related_attribute_syn};
use ide_db::{
FilePosition, FxHashMap, FxIndexMap, RootDatabase, SymbolKind,
FilePosition, FxHashMap, FxIndexMap, FxIndexSet, RootDatabase, SymbolKind,
base_db::RootQueryDb,
defs::Definition,
documentation::docs_from_attrs,
Expand Down
0