Unnamed repository; edit this file 'description' to name the repository.
chore: Remove some unnecessary usage of `Semantics`
Lukas Wirth 2025-03-22
parent 9c01af0 · commit 3086e5f
-rw-r--r--crates/hir/src/lib.rs12
-rw-r--r--crates/hir/src/semantics.rs12
-rw-r--r--crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs2
-rw-r--r--crates/ide-completion/src/context.rs1
-rw-r--r--crates/ide-db/src/imports/import_assets.rs43
-rw-r--r--crates/ide-db/src/items_locator.rs23
6 files changed, 46 insertions, 47 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index ed0743def6..1e58c076f2 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -599,6 +599,18 @@ impl Module {
.collect()
}
+ pub fn resolve_mod_path(
+ &self,
+ db: &dyn HirDatabase,
+ segments: impl IntoIterator<Item = Name>,
+ ) -> Option<impl Iterator<Item = ItemInNs>> {
+ let items = self.id.resolver(db.upcast()).resolve_module_path_in_items(
+ db.upcast(),
+ &ModPath::from_segments(hir_def::path::PathKind::Plain, segments),
+ );
+ Some(items.iter_items().map(|(item, _)| item.into()))
+ }
+
/// Fills `acc` with the module's diagnostics.
pub fn diagnostics(
self,
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 526fcd6e06..e5df574199 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -1651,18 +1651,6 @@ impl<'db> SemanticsImpl<'db> {
Some(items.iter_items().map(|(item, _)| item.into()))
}
- pub fn resolve_mod_path_relative(
- &self,
- to: Module,
- segments: impl IntoIterator<Item = Name>,
- ) -> Option<impl Iterator<Item = ItemInNs>> {
- let items = to.id.resolver(self.db.upcast()).resolve_module_path_in_items(
- self.db.upcast(),
- &ModPath::from_segments(hir_def::path::PathKind::Plain, segments),
- );
- Some(items.iter_items().map(|(item, _)| item.into()))
- }
-
fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
self.analyze(record_lit.syntax())?.resolve_variant(record_lit)
}
diff --git a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
index e0693a7d5b..c5faf6d3e0 100644
--- a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
+++ b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
@@ -73,7 +73,7 @@ pub(crate) fn replace_derive_with_manual_impl(
let current_edition = current_crate.edition(ctx.db());
let found_traits = items_locator::items_with_name(
- &ctx.sema,
+ ctx.db(),
current_crate,
NameToImport::exact_case_sensitive(path.segments().last()?.to_string()),
items_locator::AssocSearchMode::Exclude,
diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs
index aa0b6060f6..3043796031 100644
--- a/crates/ide-completion/src/context.rs
+++ b/crates/ide-completion/src/context.rs
@@ -823,6 +823,7 @@ impl<'a> CompletionContext<'a> {
exclude_flyimport
.extend(exclude_traits.iter().map(|&t| (t.into(), AutoImportExclusionType::Always)));
+ // FIXME: This should be part of `CompletionAnalysis` / `expand_and_analyze`
let complete_semicolon = if config.add_semicolon_to_unit {
let inside_closure_ret = token.parent_ancestors().try_for_each(|ancestor| {
match_ast! {
diff --git a/crates/ide-db/src/imports/import_assets.rs b/crates/ide-db/src/imports/import_assets.rs
index 0fc287c57e..650c957ca4 100644
--- a/crates/ide-db/src/imports/import_assets.rs
+++ b/crates/ide-db/src/imports/import_assets.rs
@@ -273,12 +273,13 @@ impl ImportAssets {
Some(it) => it,
None => return <FxIndexSet<_>>::default().into_iter(),
};
+ let db = sema.db;
let krate = self.module_with_candidate.krate();
let scope_definitions = self.scope_definitions(sema);
let mod_path = |item| {
get_mod_path(
- sema.db,
- item_for_path_search(sema.db, item)?,
+ db,
+ item_for_path_search(db, item)?,
&self.module_with_candidate,
prefixed,
cfg,
@@ -288,7 +289,7 @@ impl ImportAssets {
match &self.import_candidate {
ImportCandidate::Path(path_candidate) => path_applicable_imports(
- sema,
+ db,
&scope,
krate,
path_candidate,
@@ -297,7 +298,7 @@ impl ImportAssets {
),
ImportCandidate::TraitAssocItem(trait_candidate)
| ImportCandidate::TraitMethod(trait_candidate) => trait_applicable_items(
- sema,
+ db,
krate,
&scope,
trait_candidate,
@@ -325,7 +326,7 @@ impl ImportAssets {
}
fn path_applicable_imports(
- sema: &Semantics<'_, RootDatabase>,
+ db: &RootDatabase,
scope: &SemanticsScope<'_>,
current_crate: Crate,
path_candidate: &PathImportCandidate,
@@ -337,7 +338,7 @@ fn path_applicable_imports(
match &*path_candidate.qualifier {
[] => {
items_locator::items_with_name(
- sema,
+ db,
current_crate,
path_candidate.name.clone(),
// FIXME: we could look up assoc items by the input and propose those in completion,
@@ -365,7 +366,7 @@ fn path_applicable_imports(
// what follows
// FIXME: This doesn't handle visibility
[first_qsegment, qualifier_rest @ ..] => items_locator::items_with_name(
- sema,
+ db,
current_crate,
NameToImport::Exact(first_qsegment.as_str().to_owned(), true),
AssocSearchMode::Exclude,
@@ -374,7 +375,7 @@ fn path_applicable_imports(
// we found imports for `first_qsegment`, now we need to filter these imports by whether
// they result in resolving the rest of the path successfully
validate_resolvable(
- sema,
+ db,
scope,
mod_path,
scope_filter,
@@ -391,7 +392,7 @@ fn path_applicable_imports(
/// Validates and builds an import for `resolved_qualifier` if the `unresolved_qualifier` appended
/// to it resolves and there is a validate `candidate` after that.
fn validate_resolvable(
- sema: &Semantics<'_, RootDatabase>,
+ db: &RootDatabase,
scope: &SemanticsScope<'_>,
mod_path: impl Fn(ItemInNs) -> Option<ModPath>,
scope_filter: impl Fn(ItemInNs) -> bool,
@@ -406,8 +407,8 @@ fn validate_resolvable(
if !unresolved_qualifier.is_empty() {
match resolved_qualifier {
ItemInNs::Types(ModuleDef::Module(module)) => {
- adjusted_resolved_qualifier = sema
- .resolve_mod_path_relative(module, unresolved_qualifier.iter().cloned())?
+ adjusted_resolved_qualifier = module
+ .resolve_mod_path(db, unresolved_qualifier.iter().cloned())?
.next()?;
}
// can't resolve multiple segments for non-module item path bases
@@ -424,7 +425,7 @@ fn validate_resolvable(
let ty = match qualifier {
ModuleDef::Module(module) => {
return items_locator::items_with_name_in_module(
- sema,
+ db,
module,
candidate.clone(),
AssocSearchMode::Exclude,
@@ -439,17 +440,17 @@ fn validate_resolvable(
ModuleDef::Trait(_) => return None,
// FIXME
ModuleDef::TraitAlias(_) => return None,
- ModuleDef::TypeAlias(alias) => alias.ty(sema.db),
- ModuleDef::BuiltinType(builtin) => builtin.ty(sema.db),
- ModuleDef::Adt(adt) => adt.ty(sema.db),
+ ModuleDef::TypeAlias(alias) => alias.ty(db),
+ ModuleDef::BuiltinType(builtin) => builtin.ty(db),
+ ModuleDef::Adt(adt) => adt.ty(db),
_ => return None,
};
- ty.iterate_path_candidates(sema.db, scope, &FxHashSet::default(), None, None, |assoc| {
+ ty.iterate_path_candidates(db, scope, &FxHashSet::default(), None, None, |assoc| {
// FIXME: Support extra trait imports
- if assoc.container_or_implemented_trait(sema.db).is_some() {
+ if assoc.container_or_implemented_trait(db).is_some() {
return None;
}
- let name = assoc.name(sema.db)?;
+ let name = assoc.name(db)?;
let is_match = match candidate {
NameToImport::Prefix(text, true) => name.as_str().starts_with(text),
NameToImport::Prefix(text, false) => {
@@ -495,7 +496,7 @@ fn item_for_path_search_assoc(db: &RootDatabase, assoc_item: AssocItem) -> Optio
}
fn trait_applicable_items(
- sema: &Semantics<'_, RootDatabase>,
+ db: &RootDatabase,
current_crate: Crate,
scope: &SemanticsScope<'_>,
trait_candidate: &TraitImportCandidate,
@@ -505,15 +506,13 @@ fn trait_applicable_items(
) -> FxIndexSet<LocatedImport> {
let _p = tracing::info_span!("ImportAssets::trait_applicable_items").entered();
- let db = sema.db;
-
let inherent_traits = trait_candidate.receiver_ty.applicable_inherent_traits(db);
let env_traits = trait_candidate.receiver_ty.env_traits(db);
let related_traits = inherent_traits.chain(env_traits).collect::<FxHashSet<_>>();
let mut required_assoc_items = FxHashSet::default();
let mut trait_candidates: FxHashSet<_> = items_locator::items_with_name(
- sema,
+ db,
current_crate,
trait_candidate.assoc_item_name.clone(),
AssocSearchMode::AssocItemsOnly,
diff --git a/crates/ide-db/src/items_locator.rs b/crates/ide-db/src/items_locator.rs
index 8088182980..7a543d64e2 100644
--- a/crates/ide-db/src/items_locator.rs
+++ b/crates/ide-db/src/items_locator.rs
@@ -5,7 +5,7 @@
use std::ops::ControlFlow;
use either::Either;
-use hir::{Crate, ItemInNs, Module, Semantics, import_map};
+use hir::{Crate, ItemInNs, Module, import_map};
use crate::{
RootDatabase,
@@ -20,13 +20,13 @@ pub use import_map::AssocSearchMode;
// FIXME: Do callbacks instead to avoid allocations.
/// Searches for importable items with the given name in the crate and its dependencies.
-pub fn items_with_name<'a>(
- sema: &'a Semantics<'_, RootDatabase>,
+pub fn items_with_name(
+ db: &RootDatabase,
krate: Crate,
name: NameToImport,
assoc_item_search: AssocSearchMode,
-) -> impl Iterator<Item = ItemInNs> + 'a {
- let _p = tracing::info_span!("items_with_name", name = name.text(), assoc_item_search = ?assoc_item_search, crate = ?krate.display_name(sema.db).map(|name| name.to_string()))
+) -> impl Iterator<Item = ItemInNs> {
+ let _p = tracing::info_span!("items_with_name", name = name.text(), assoc_item_search = ?assoc_item_search, crate = ?krate.display_name(db).map(|name| name.to_string()))
.entered();
let prefix = matches!(name, NameToImport::Prefix(..));
@@ -68,12 +68,12 @@ pub fn items_with_name<'a>(
}
};
- find_items(sema, krate, local_query, external_query)
+ find_items(db, krate, local_query, external_query)
}
/// Searches for importable items with the given name in the crate and its dependencies.
pub fn items_with_name_in_module<T>(
- sema: &Semantics<'_, RootDatabase>,
+ db: &RootDatabase,
module: Module,
name: NameToImport,
assoc_item_search: AssocSearchMode,
@@ -110,7 +110,7 @@ pub fn items_with_name_in_module<T>(
local_query
}
};
- local_query.search(&[sema.db.module_symbols(module)], |local_candidate| {
+ local_query.search(&[db.module_symbols(module)], |local_candidate| {
cb(match local_candidate.def {
hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),
def => ItemInNs::from(def),
@@ -118,14 +118,13 @@ pub fn items_with_name_in_module<T>(
})
}
-fn find_items<'a>(
- sema: &'a Semantics<'_, RootDatabase>,
+fn find_items(
+ db: &RootDatabase,
krate: Crate,
local_query: symbol_index::Query,
external_query: import_map::Query,
-) -> impl Iterator<Item = ItemInNs> + 'a {
+) -> impl Iterator<Item = ItemInNs> {
let _p = tracing::info_span!("find_items").entered();
- let db = sema.db;
// NOTE: `external_query` includes `assoc_item_search`, so we don't need to
// filter on our own.