Skip to content

Commit 32fa60f

Browse files
authored
Merge pull request #19412 from Veykril/push-krktmvxmlxmt
chore: Remove some unnecessary usage of `Semantics`
2 parents 4339a79 + 3086e5f commit 32fa60f

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

crates/hir/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,18 @@ impl Module {
599599
.collect()
600600
}
601601

602+
pub fn resolve_mod_path(
603+
&self,
604+
db: &dyn HirDatabase,
605+
segments: impl IntoIterator<Item = Name>,
606+
) -> Option<impl Iterator<Item = ItemInNs>> {
607+
let items = self.id.resolver(db.upcast()).resolve_module_path_in_items(
608+
db.upcast(),
609+
&ModPath::from_segments(hir_def::path::PathKind::Plain, segments),
610+
);
611+
Some(items.iter_items().map(|(item, _)| item.into()))
612+
}
613+
602614
/// Fills `acc` with the module's diagnostics.
603615
pub fn diagnostics(
604616
self,

crates/hir/src/semantics.rs

-12
Original file line numberDiff line numberDiff line change
@@ -1651,18 +1651,6 @@ impl<'db> SemanticsImpl<'db> {
16511651
Some(items.iter_items().map(|(item, _)| item.into()))
16521652
}
16531653

1654-
pub fn resolve_mod_path_relative(
1655-
&self,
1656-
to: Module,
1657-
segments: impl IntoIterator<Item = Name>,
1658-
) -> Option<impl Iterator<Item = ItemInNs>> {
1659-
let items = to.id.resolver(self.db.upcast()).resolve_module_path_in_items(
1660-
self.db.upcast(),
1661-
&ModPath::from_segments(hir_def::path::PathKind::Plain, segments),
1662-
);
1663-
Some(items.iter_items().map(|(item, _)| item.into()))
1664-
}
1665-
16661654
fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
16671655
self.analyze(record_lit.syntax())?.resolve_variant(record_lit)
16681656
}

crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) fn replace_derive_with_manual_impl(
7373
let current_edition = current_crate.edition(ctx.db());
7474

7575
let found_traits = items_locator::items_with_name(
76-
&ctx.sema,
76+
ctx.db(),
7777
current_crate,
7878
NameToImport::exact_case_sensitive(path.segments().last()?.to_string()),
7979
items_locator::AssocSearchMode::Exclude,

crates/ide-completion/src/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ impl<'a> CompletionContext<'a> {
823823
exclude_flyimport
824824
.extend(exclude_traits.iter().map(|&t| (t.into(), AutoImportExclusionType::Always)));
825825

826+
// FIXME: This should be part of `CompletionAnalysis` / `expand_and_analyze`
826827
let complete_semicolon = if config.add_semicolon_to_unit {
827828
let inside_closure_ret = token.parent_ancestors().try_for_each(|ancestor| {
828829
match_ast! {

crates/ide-db/src/imports/import_assets.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,13 @@ impl ImportAssets {
273273
Some(it) => it,
274274
None => return <FxIndexSet<_>>::default().into_iter(),
275275
};
276+
let db = sema.db;
276277
let krate = self.module_with_candidate.krate();
277278
let scope_definitions = self.scope_definitions(sema);
278279
let mod_path = |item| {
279280
get_mod_path(
280-
sema.db,
281-
item_for_path_search(sema.db, item)?,
281+
db,
282+
item_for_path_search(db, item)?,
282283
&self.module_with_candidate,
283284
prefixed,
284285
cfg,
@@ -288,7 +289,7 @@ impl ImportAssets {
288289

289290
match &self.import_candidate {
290291
ImportCandidate::Path(path_candidate) => path_applicable_imports(
291-
sema,
292+
db,
292293
&scope,
293294
krate,
294295
path_candidate,
@@ -297,7 +298,7 @@ impl ImportAssets {
297298
),
298299
ImportCandidate::TraitAssocItem(trait_candidate)
299300
| ImportCandidate::TraitMethod(trait_candidate) => trait_applicable_items(
300-
sema,
301+
db,
301302
krate,
302303
&scope,
303304
trait_candidate,
@@ -325,7 +326,7 @@ impl ImportAssets {
325326
}
326327

327328
fn path_applicable_imports(
328-
sema: &Semantics<'_, RootDatabase>,
329+
db: &RootDatabase,
329330
scope: &SemanticsScope<'_>,
330331
current_crate: Crate,
331332
path_candidate: &PathImportCandidate,
@@ -337,7 +338,7 @@ fn path_applicable_imports(
337338
match &*path_candidate.qualifier {
338339
[] => {
339340
items_locator::items_with_name(
340-
sema,
341+
db,
341342
current_crate,
342343
path_candidate.name.clone(),
343344
// FIXME: we could look up assoc items by the input and propose those in completion,
@@ -365,7 +366,7 @@ fn path_applicable_imports(
365366
// what follows
366367
// FIXME: This doesn't handle visibility
367368
[first_qsegment, qualifier_rest @ ..] => items_locator::items_with_name(
368-
sema,
369+
db,
369370
current_crate,
370371
NameToImport::Exact(first_qsegment.as_str().to_owned(), true),
371372
AssocSearchMode::Exclude,
@@ -374,7 +375,7 @@ fn path_applicable_imports(
374375
// we found imports for `first_qsegment`, now we need to filter these imports by whether
375376
// they result in resolving the rest of the path successfully
376377
validate_resolvable(
377-
sema,
378+
db,
378379
scope,
379380
mod_path,
380381
scope_filter,
@@ -391,7 +392,7 @@ fn path_applicable_imports(
391392
/// Validates and builds an import for `resolved_qualifier` if the `unresolved_qualifier` appended
392393
/// to it resolves and there is a validate `candidate` after that.
393394
fn validate_resolvable(
394-
sema: &Semantics<'_, RootDatabase>,
395+
db: &RootDatabase,
395396
scope: &SemanticsScope<'_>,
396397
mod_path: impl Fn(ItemInNs) -> Option<ModPath>,
397398
scope_filter: impl Fn(ItemInNs) -> bool,
@@ -406,8 +407,8 @@ fn validate_resolvable(
406407
if !unresolved_qualifier.is_empty() {
407408
match resolved_qualifier {
408409
ItemInNs::Types(ModuleDef::Module(module)) => {
409-
adjusted_resolved_qualifier = sema
410-
.resolve_mod_path_relative(module, unresolved_qualifier.iter().cloned())?
410+
adjusted_resolved_qualifier = module
411+
.resolve_mod_path(db, unresolved_qualifier.iter().cloned())?
411412
.next()?;
412413
}
413414
// can't resolve multiple segments for non-module item path bases
@@ -424,7 +425,7 @@ fn validate_resolvable(
424425
let ty = match qualifier {
425426
ModuleDef::Module(module) => {
426427
return items_locator::items_with_name_in_module(
427-
sema,
428+
db,
428429
module,
429430
candidate.clone(),
430431
AssocSearchMode::Exclude,
@@ -439,17 +440,17 @@ fn validate_resolvable(
439440
ModuleDef::Trait(_) => return None,
440441
// FIXME
441442
ModuleDef::TraitAlias(_) => return None,
442-
ModuleDef::TypeAlias(alias) => alias.ty(sema.db),
443-
ModuleDef::BuiltinType(builtin) => builtin.ty(sema.db),
444-
ModuleDef::Adt(adt) => adt.ty(sema.db),
443+
ModuleDef::TypeAlias(alias) => alias.ty(db),
444+
ModuleDef::BuiltinType(builtin) => builtin.ty(db),
445+
ModuleDef::Adt(adt) => adt.ty(db),
445446
_ => return None,
446447
};
447-
ty.iterate_path_candidates(sema.db, scope, &FxHashSet::default(), None, None, |assoc| {
448+
ty.iterate_path_candidates(db, scope, &FxHashSet::default(), None, None, |assoc| {
448449
// FIXME: Support extra trait imports
449-
if assoc.container_or_implemented_trait(sema.db).is_some() {
450+
if assoc.container_or_implemented_trait(db).is_some() {
450451
return None;
451452
}
452-
let name = assoc.name(sema.db)?;
453+
let name = assoc.name(db)?;
453454
let is_match = match candidate {
454455
NameToImport::Prefix(text, true) => name.as_str().starts_with(text),
455456
NameToImport::Prefix(text, false) => {
@@ -495,7 +496,7 @@ fn item_for_path_search_assoc(db: &RootDatabase, assoc_item: AssocItem) -> Optio
495496
}
496497

497498
fn trait_applicable_items(
498-
sema: &Semantics<'_, RootDatabase>,
499+
db: &RootDatabase,
499500
current_crate: Crate,
500501
scope: &SemanticsScope<'_>,
501502
trait_candidate: &TraitImportCandidate,
@@ -505,15 +506,13 @@ fn trait_applicable_items(
505506
) -> FxIndexSet<LocatedImport> {
506507
let _p = tracing::info_span!("ImportAssets::trait_applicable_items").entered();
507508

508-
let db = sema.db;
509-
510509
let inherent_traits = trait_candidate.receiver_ty.applicable_inherent_traits(db);
511510
let env_traits = trait_candidate.receiver_ty.env_traits(db);
512511
let related_traits = inherent_traits.chain(env_traits).collect::<FxHashSet<_>>();
513512

514513
let mut required_assoc_items = FxHashSet::default();
515514
let mut trait_candidates: FxHashSet<_> = items_locator::items_with_name(
516-
sema,
515+
db,
517516
current_crate,
518517
trait_candidate.assoc_item_name.clone(),
519518
AssocSearchMode::AssocItemsOnly,

crates/ide-db/src/items_locator.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::ops::ControlFlow;
66

77
use either::Either;
8-
use hir::{Crate, ItemInNs, Module, Semantics, import_map};
8+
use hir::{Crate, ItemInNs, Module, import_map};
99

1010
use crate::{
1111
RootDatabase,
@@ -20,13 +20,13 @@ pub use import_map::AssocSearchMode;
2020

2121
// FIXME: Do callbacks instead to avoid allocations.
2222
/// Searches for importable items with the given name in the crate and its dependencies.
23-
pub fn items_with_name<'a>(
24-
sema: &'a Semantics<'_, RootDatabase>,
23+
pub fn items_with_name(
24+
db: &RootDatabase,
2525
krate: Crate,
2626
name: NameToImport,
2727
assoc_item_search: AssocSearchMode,
28-
) -> impl Iterator<Item = ItemInNs> + 'a {
29-
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()))
28+
) -> impl Iterator<Item = ItemInNs> {
29+
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()))
3030
.entered();
3131

3232
let prefix = matches!(name, NameToImport::Prefix(..));
@@ -68,12 +68,12 @@ pub fn items_with_name<'a>(
6868
}
6969
};
7070

71-
find_items(sema, krate, local_query, external_query)
71+
find_items(db, krate, local_query, external_query)
7272
}
7373

7474
/// Searches for importable items with the given name in the crate and its dependencies.
7575
pub fn items_with_name_in_module<T>(
76-
sema: &Semantics<'_, RootDatabase>,
76+
db: &RootDatabase,
7777
module: Module,
7878
name: NameToImport,
7979
assoc_item_search: AssocSearchMode,
@@ -110,22 +110,21 @@ pub fn items_with_name_in_module<T>(
110110
local_query
111111
}
112112
};
113-
local_query.search(&[sema.db.module_symbols(module)], |local_candidate| {
113+
local_query.search(&[db.module_symbols(module)], |local_candidate| {
114114
cb(match local_candidate.def {
115115
hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),
116116
def => ItemInNs::from(def),
117117
})
118118
})
119119
}
120120

121-
fn find_items<'a>(
122-
sema: &'a Semantics<'_, RootDatabase>,
121+
fn find_items(
122+
db: &RootDatabase,
123123
krate: Crate,
124124
local_query: symbol_index::Query,
125125
external_query: import_map::Query,
126-
) -> impl Iterator<Item = ItemInNs> + 'a {
126+
) -> impl Iterator<Item = ItemInNs> {
127127
let _p = tracing::info_span!("find_items").entered();
128-
let db = sema.db;
129128

130129
// NOTE: `external_query` includes `assoc_item_search`, so we don't need to
131130
// filter on our own.

0 commit comments

Comments
 (0)