Skip to content

Commit 5992af6

Browse files
committed
fix: Fix general find-path inconsistencies
1 parent 9f4b651 commit 5992af6

32 files changed

+242
-193
lines changed

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

+157-124
Large diffs are not rendered by default.

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! A map of all publicly exported items in a crate.
22
3-
use std::{fmt, hash::BuildHasherDefault};
3+
use std::fmt;
44

55
use base_db::CrateId;
66
use fst::{raw::IndexedValue, Automaton, Streamer};
77
use hir_expand::name::Name;
8-
use indexmap::IndexMap;
98
use itertools::Itertools;
10-
use rustc_hash::{FxHashSet, FxHasher};
9+
use rustc_hash::FxHashSet;
1110
use smallvec::SmallVec;
1211
use stdx::{format_to, TupleExt};
1312
use triomphe::Arc;
@@ -17,7 +16,7 @@ use crate::{
1716
item_scope::{ImportOrExternCrate, ItemInNs},
1817
nameres::DefMap,
1918
visibility::Visibility,
20-
AssocItemId, ModuleDefId, ModuleId, TraitId,
19+
AssocItemId, FxIndexMap, ModuleDefId, ModuleId, TraitId,
2120
};
2221

2322
/// Item import details stored in the `ImportMap`.
@@ -58,7 +57,6 @@ enum IsTraitAssocItem {
5857
No,
5958
}
6059

61-
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
6260
type ImportMapIndex = FxIndexMap<ItemInNs, (SmallVec<[ImportInfo; 1]>, IsTraitAssocItem)>;
6361

6462
impl ImportMap {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl ItemScope {
295295
pub(crate) fn names_of<T>(
296296
&self,
297297
item: ItemInNs,
298-
mut cb: impl FnMut(&Name, Visibility, bool) -> Option<T>,
298+
mut cb: impl FnMut(&Name, Visibility, /*declared*/ bool) -> Option<T>,
299299
) -> Option<T> {
300300
match item {
301301
ItemInNs::Macros(def) => self

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

+23
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ use crate::{
106106
},
107107
};
108108

109+
type FxIndexMap<K, V> =
110+
indexmap::IndexMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
111+
109112
#[derive(Debug)]
110113
pub struct ItemLoc<N: ItemTreeNode> {
111114
pub container: ModuleId,
@@ -455,6 +458,26 @@ impl ModuleId {
455458
pub fn is_block_module(self) -> bool {
456459
self.block.is_some() && self.local_id == DefMap::ROOT
457460
}
461+
462+
pub fn is_within_block(self) -> bool {
463+
self.block.is_some()
464+
}
465+
466+
pub fn as_crate_root(&self) -> Option<CrateRootModuleId> {
467+
if self.local_id == DefMap::ROOT && self.block.is_none() {
468+
Some(CrateRootModuleId { krate: self.krate })
469+
} else {
470+
None
471+
}
472+
}
473+
474+
pub fn derive_crate_root(&self) -> CrateRootModuleId {
475+
CrateRootModuleId { krate: self.krate }
476+
}
477+
478+
fn is_crate_root(&self) -> bool {
479+
self.local_id == DefMap::ROOT && self.block.is_none()
480+
}
458481
}
459482

460483
impl PartialEq<CrateRootModuleId> for ModuleId {

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use crate::{
8181
per_ns::PerNs,
8282
visibility::{Visibility, VisibilityExplicitness},
8383
AstId, BlockId, BlockLoc, CrateRootModuleId, EnumId, EnumVariantId, ExternCrateId, FunctionId,
84-
LocalModuleId, Lookup, MacroExpander, MacroId, ModuleId, ProcMacroId, UseId,
84+
FxIndexMap, LocalModuleId, Lookup, MacroExpander, MacroId, ModuleId, ProcMacroId, UseId,
8585
};
8686

8787
/// Contains the results of (early) name resolution.
@@ -129,7 +129,7 @@ pub struct DefMap {
129129
#[derive(Clone, Debug, PartialEq, Eq)]
130130
struct DefMapCrateData {
131131
/// The extern prelude which contains all root modules of external crates that are in scope.
132-
extern_prelude: FxHashMap<Name, (CrateRootModuleId, Option<ExternCrateId>)>,
132+
extern_prelude: FxIndexMap<Name, (CrateRootModuleId, Option<ExternCrateId>)>,
133133

134134
/// Side table for resolving derive helpers.
135135
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
@@ -155,7 +155,7 @@ struct DefMapCrateData {
155155
impl DefMapCrateData {
156156
fn new(edition: Edition) -> Self {
157157
Self {
158-
extern_prelude: FxHashMap::default(),
158+
extern_prelude: FxIndexMap::default(),
159159
exported_derives: FxHashMap::default(),
160160
fn_proc_macro_mapping: FxHashMap::default(),
161161
proc_macro_loading_error: None,
@@ -578,7 +578,8 @@ impl DefMap {
578578

579579
pub(crate) fn extern_prelude(
580580
&self,
581-
) -> impl Iterator<Item = (&Name, (CrateRootModuleId, Option<ExternCrateId>))> + '_ {
581+
) -> impl DoubleEndedIterator<Item = (&Name, (CrateRootModuleId, Option<ExternCrateId>))> + '_
582+
{
582583
self.data.extern_prelude.iter().map(|(name, &def)| (name, def))
583584
}
584585

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! Name resolution façade.
2-
use std::{fmt, hash::BuildHasherDefault, iter, mem};
2+
use std::{fmt, iter, mem};
33

44
use base_db::CrateId;
55
use hir_expand::{
66
name::{name, Name},
77
MacroDefId,
88
};
9-
use indexmap::IndexMap;
109
use intern::Interned;
1110
use rustc_hash::FxHashSet;
1211
use smallvec::{smallvec, SmallVec};
@@ -27,10 +26,10 @@ use crate::{
2726
type_ref::LifetimeRef,
2827
visibility::{RawVisibility, Visibility},
2928
AdtId, ConstId, ConstParamId, CrateRootModuleId, DefWithBodyId, EnumId, EnumVariantId,
30-
ExternBlockId, ExternCrateId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId,
31-
ItemContainerId, ItemTreeLoc, LifetimeParamId, LocalModuleId, Lookup, Macro2Id, MacroId,
32-
MacroRulesId, ModuleDefId, ModuleId, ProcMacroId, StaticId, StructId, TraitAliasId, TraitId,
33-
TypeAliasId, TypeOrConstParamId, TypeOwnerId, TypeParamId, UseId, VariantId,
29+
ExternBlockId, ExternCrateId, FunctionId, FxIndexMap, GenericDefId, GenericParamId, HasModule,
30+
ImplId, ItemContainerId, ItemTreeLoc, LifetimeParamId, LocalModuleId, Lookup, Macro2Id,
31+
MacroId, MacroRulesId, ModuleDefId, ModuleId, ProcMacroId, StaticId, StructId, TraitAliasId,
32+
TraitId, TypeAliasId, TypeOrConstParamId, TypeOwnerId, TypeParamId, UseId, VariantId,
3433
};
3534

3635
#[derive(Debug, Clone)]
@@ -957,7 +956,6 @@ fn to_type_ns(per_ns: PerNs) -> Option<(TypeNs, Option<ImportOrExternCrate>)> {
957956
Some((res, import))
958957
}
959958

960-
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<rustc_hash::FxHasher>>;
961959
#[derive(Default)]
962960
struct ScopeNames {
963961
map: FxIndexMap<Name, SmallVec<[ScopeDef; 1]>>,

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use either::Either;
1313
use hir_def::{
1414
data::adt::VariantData,
1515
db::DefDatabase,
16-
find_path,
16+
find_path::{self, PrefixKind},
1717
generics::{TypeOrConstParamData, TypeParamProvenance},
1818
item_scope::ItemInNs,
1919
lang_item::{LangItem, LangItemTarget},
@@ -999,6 +999,8 @@ impl HirDisplay for Ty {
999999
db.upcast(),
10001000
ItemInNs::Types((*def_id).into()),
10011001
module_id,
1002+
PrefixKind::Plain,
1003+
false,
10021004
false,
10031005
true,
10041006
) {

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ impl Module {
789789

790790
/// Finds a path that can be used to refer to the given item from within
791791
/// this module, if possible.
792-
pub fn find_use_path(
792+
pub fn find_path(
793793
self,
794794
db: &dyn DefDatabase,
795795
item: impl Into<ItemInNs>,
@@ -800,26 +800,29 @@ impl Module {
800800
db,
801801
item.into().into(),
802802
self.into(),
803+
PrefixKind::Plain,
804+
false,
803805
prefer_no_std,
804806
prefer_prelude,
805807
)
806808
}
807809

808810
/// Finds a path that can be used to refer to the given item from within
809811
/// this module, if possible. This is used for returning import paths for use-statements.
810-
pub fn find_use_path_prefixed(
812+
pub fn find_use_path(
811813
self,
812814
db: &dyn DefDatabase,
813815
item: impl Into<ItemInNs>,
814816
prefix_kind: PrefixKind,
815817
prefer_no_std: bool,
816818
prefer_prelude: bool,
817819
) -> Option<ModPath> {
818-
hir_def::find_path::find_path_prefixed(
820+
hir_def::find_path::find_path(
819821
db,
820822
item.into().into(),
821823
self.into(),
822824
prefix_kind,
825+
true,
823826
prefer_no_std,
824827
prefer_prelude,
825828
)

src/tools/rust-analyzer/crates/hir/src/term_search/expr.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,11 @@ fn mod_item_path(
3333
});
3434

3535
let m = sema_scope.module();
36-
match name_hit_count {
37-
Some(0..=1) | None => m.find_use_path(db.upcast(), *def, prefer_no_std, prefer_prelude),
38-
Some(_) => m.find_use_path_prefixed(
39-
db.upcast(),
40-
*def,
41-
PrefixKind::ByCrate,
42-
prefer_no_std,
43-
prefer_prelude,
44-
),
45-
}
36+
let prefix = match name_hit_count {
37+
Some(0..=1) | None => PrefixKind::Plain,
38+
Some(_) => PrefixKind::ByCrate,
39+
};
40+
m.find_use_path(db.upcast(), *def, prefix, prefer_no_std, prefer_prelude)
4641
}
4742

4843
/// Helper function to get path to `ModuleDef` as string

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ fn build_pat(
462462
) -> Option<ast::Pat> {
463463
match var {
464464
ExtendedVariant::Variant(var) => {
465-
let path = mod_path_to_ast(&module.find_use_path(
465+
let path = mod_path_to_ast(&module.find_path(
466466
db,
467467
ModuleDef::from(var),
468468
prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fn augment_references_with_imports(
341341

342342
let import_scope = ImportScope::find_insert_use_container(name.syntax(), &ctx.sema);
343343
let path = ref_module
344-
.find_use_path_prefixed(
344+
.find_use_path(
345345
ctx.sema.db,
346346
ModuleDef::Module(*target_module),
347347
ctx.config.insert_use.prefix_kind,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
5050
_ => return None,
5151
};
5252

53-
mod_path_to_ast(&module.find_use_path(
53+
mod_path_to_ast(&module.find_path(
5454
ctx.db(),
5555
src_type_def,
5656
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_return_type_to_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn augment_references_with_imports(
201201
let import_scope =
202202
ImportScope::find_insert_use_container(new_name.syntax(), &ctx.sema);
203203
let path = ref_module
204-
.find_use_path_prefixed(
204+
.find_use_path(
205205
ctx.sema.db,
206206
ModuleDef::Module(*target_module),
207207
ctx.config.insert_use.prefix_kind,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
9090
let module = ctx.sema.scope(ident_pat.syntax())?.module();
9191
let struct_def = hir::ModuleDef::from(struct_type);
9292
let kind = struct_type.kind(ctx.db());
93-
let struct_def_path = module.find_use_path(
93+
let struct_def_path = module.find_path(
9494
ctx.db(),
9595
struct_def,
9696
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
209209
FamousDefs(&ctx.sema, module.krate()).core_ops_ControlFlow();
210210

211211
if let Some(control_flow_enum) = control_flow_enum {
212-
let mod_path = module.find_use_path_prefixed(
212+
let mod_path = module.find_use_path(
213213
ctx.sema.db,
214214
ModuleDef::from(control_flow_enum),
215215
ctx.config.insert_use.prefix_kind,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ fn process_references(
386386
let segment = builder.make_mut(segment);
387387
let scope_node = builder.make_syntax_mut(scope_node);
388388
if !visited_modules.contains(&module) {
389-
let mod_path = module.find_use_path_prefixed(
389+
let mod_path = module.find_use_path(
390390
ctx.sema.db,
391391
*enum_module_def,
392392
ctx.config.insert_use.prefix_kind,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_deref.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
5858

5959
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
6060
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
61-
let trait_path = module.find_use_path(
61+
let trait_path = module.find_path(
6262
ctx.db(),
6363
ModuleDef::Trait(trait_),
6464
ctx.config.prefer_no_std,
@@ -103,7 +103,7 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
103103

104104
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
105105
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
106-
let trait_path = module.find_use_path(
106+
let trait_path = module.find_path(
107107
ctx.db(),
108108
ModuleDef::Trait(trait_),
109109
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_new.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
5858

5959
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
6060

61-
let type_path = current_module.find_use_path(
61+
let type_path = current_module.find_path(
6262
ctx.sema.db,
6363
item_for_path_search(ctx.sema.db, item_in_ns)?,
6464
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_method_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4444
let current_module = ctx.sema.scope(call.syntax())?.module();
4545
let target_module_def = ModuleDef::from(resolved_call);
4646
let item_in_ns = ItemInNs::from(target_module_def);
47-
let receiver_path = current_module.find_use_path(
47+
let receiver_path = current_module.find_path(
4848
ctx.sema.db,
4949
item_for_path_search(ctx.sema.db, item_in_ns)?,
5050
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub(crate) fn replace_derive_with_manual_impl(
8383
})
8484
.flat_map(|trait_| {
8585
current_module
86-
.find_use_path(
86+
.find_path(
8787
ctx.sema.db,
8888
hir::ModuleDef::Trait(trait_),
8989
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) fn replace_qualified_name_with_use(
6363
);
6464
let path_to_qualifier = starts_with_name_ref
6565
.then(|| {
66-
ctx.sema.scope(path.syntax())?.module().find_use_path_prefixed(
66+
ctx.sema.scope(path.syntax())?.module().find_use_path(
6767
ctx.sema.db,
6868
module,
6969
ctx.config.insert_use.prefix_kind,

src/tools/rust-analyzer/crates/ide-completion/src/completions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ fn enum_variants_with_paths(
633633
}
634634

635635
for variant in variants {
636-
if let Some(path) = ctx.module.find_use_path(
636+
if let Some(path) = ctx.module.find_path(
637637
ctx.db,
638638
hir::ModuleDef::from(variant),
639639
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub(crate) fn complete_expr_path(
171171
hir::Adt::Struct(strukt) => {
172172
let path = ctx
173173
.module
174-
.find_use_path(
174+
.find_path(
175175
ctx.db,
176176
hir::ModuleDef::from(strukt),
177177
ctx.config.prefer_no_std,
@@ -194,7 +194,7 @@ pub(crate) fn complete_expr_path(
194194
hir::Adt::Union(un) => {
195195
let path = ctx
196196
.module
197-
.find_use_path(
197+
.find_path(
198198
ctx.db,
199199
hir::ModuleDef::from(un),
200200
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) fn complete_postfix(
6363
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
6464
if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) {
6565
if let Some(drop_fn) = ctx.famous_defs().core_mem_drop() {
66-
if let Some(path) = ctx.module.find_use_path(
66+
if let Some(path) = ctx.module.find_path(
6767
ctx.db,
6868
ItemInNs::Values(drop_fn.into()),
6969
ctx.config.prefer_no_std,

src/tools/rust-analyzer/crates/ide-completion/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub fn resolve_completion_edits(
260260
);
261261
let import = items_with_name
262262
.filter_map(|candidate| {
263-
current_module.find_use_path_prefixed(
263+
current_module.find_use_path(
264264
db,
265265
candidate,
266266
config.insert_use.prefix_kind,

0 commit comments

Comments
 (0)