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

Lines changed: 157 additions & 124 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 5 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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,

0 commit comments

Comments
 (0)