Skip to content

Commit 6e4c29f

Browse files
authored
Merge pull request #19018 from Veykril/push-wxqqunxwrply
internal: Record the use tree index in glob imports
2 parents a62e2f5 + 433888c commit 6e4c29f

File tree

5 files changed

+112
-95
lines changed

5 files changed

+112
-95
lines changed

crates/hir-def/src/import_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ impl ImportMap {
167167
let attr_id = if let Some(import) = import {
168168
match import {
169169
ImportOrExternCrate::ExternCrate(id) => Some(id.into()),
170-
ImportOrExternCrate::Import(id) => Some(id.import.into()),
171-
ImportOrExternCrate::Glob(id) => Some(id.into()),
170+
ImportOrExternCrate::Import(id) => Some(id.use_.into()),
171+
ImportOrExternCrate::Glob(id) => Some(id.use_.into()),
172172
}
173173
} else {
174174
match item {

crates/hir-def/src/item_scope.rs

+62-60
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct PerNsGlobImports {
3131

3232
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3333
pub enum ImportOrExternCrate {
34-
Glob(UseId),
34+
Glob(GlobId),
3535
Import(ImportId),
3636
ExternCrate(ExternCrateId),
3737
}
@@ -45,29 +45,41 @@ impl From<ImportOrGlob> for ImportOrExternCrate {
4545
}
4646
}
4747

48-
impl From<ImportType> for ImportOrExternCrate {
49-
fn from(value: ImportType) -> Self {
50-
match value {
51-
ImportType::Glob(it) => ImportOrExternCrate::Glob(it),
52-
ImportType::Import(it) => ImportOrExternCrate::Import(it),
53-
ImportType::ExternCrate(it) => ImportOrExternCrate::ExternCrate(it),
54-
}
55-
}
56-
}
57-
5848
impl ImportOrExternCrate {
59-
pub fn into_import(self) -> Option<ImportOrGlob> {
49+
pub fn import_or_glob(self) -> Option<ImportOrGlob> {
6050
match self {
6151
ImportOrExternCrate::Import(it) => Some(ImportOrGlob::Import(it)),
6252
ImportOrExternCrate::Glob(it) => Some(ImportOrGlob::Glob(it)),
6353
_ => None,
6454
}
6555
}
56+
57+
pub fn import(self) -> Option<ImportId> {
58+
match self {
59+
ImportOrExternCrate::Import(it) => Some(it),
60+
_ => None,
61+
}
62+
}
63+
64+
pub fn glob(self) -> Option<GlobId> {
65+
match self {
66+
ImportOrExternCrate::Glob(id) => Some(id),
67+
_ => None,
68+
}
69+
}
70+
71+
pub fn use_(self) -> Option<UseId> {
72+
match self {
73+
ImportOrExternCrate::Glob(id) => Some(id.use_),
74+
ImportOrExternCrate::Import(id) => Some(id.use_),
75+
_ => None,
76+
}
77+
}
6678
}
6779

6880
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
6981
pub enum ImportOrGlob {
70-
Glob(UseId),
82+
Glob(GlobId),
7183
Import(ImportId),
7284
}
7385

@@ -79,17 +91,11 @@ impl ImportOrGlob {
7991
}
8092
}
8193
}
82-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
83-
pub(crate) enum ImportType {
84-
Import(ImportId),
85-
Glob(UseId),
86-
ExternCrate(ExternCrateId),
87-
}
8894

8995
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9096
pub enum ImportOrDef {
9197
Import(ImportId),
92-
Glob(UseId),
98+
Glob(GlobId),
9399
ExternCrate(ExternCrateId),
94100
Def(ModuleDefId),
95101
}
@@ -115,7 +121,13 @@ impl From<ImportOrGlob> for ImportOrDef {
115121

116122
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
117123
pub struct ImportId {
118-
pub import: UseId,
124+
pub use_: UseId,
125+
pub idx: Idx<ast::UseTree>,
126+
}
127+
128+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
129+
pub struct GlobId {
130+
pub use_: UseId,
119131
pub idx: Idx<ast::UseTree>,
120132
}
121133

@@ -236,7 +248,7 @@ impl ItemScope {
236248
self.use_imports_types
237249
.keys()
238250
.copied()
239-
.filter_map(ImportOrExternCrate::into_import)
251+
.filter_map(ImportOrExternCrate::import_or_glob)
240252
.chain(self.use_imports_values.keys().copied())
241253
.chain(self.use_imports_macros.keys().copied())
242254
.filter_map(ImportOrGlob::into_import)
@@ -252,7 +264,7 @@ impl ItemScope {
252264
while let Some(&m) = scope.use_imports_macros.get(&ImportOrGlob::Import(import)) {
253265
match m {
254266
ImportOrDef::Import(i) => {
255-
let module_id = i.import.lookup(db).container;
267+
let module_id = i.use_.lookup(db).container;
256268
def_map = module_id.def_map(db);
257269
scope = &def_map[module_id.local_id].scope;
258270
import = i;
@@ -268,7 +280,7 @@ impl ItemScope {
268280
while let Some(&m) = scope.use_imports_types.get(&ImportOrExternCrate::Import(import)) {
269281
match m {
270282
ImportOrDef::Import(i) => {
271-
let module_id = i.import.lookup(db).container;
283+
let module_id = i.use_.lookup(db).container;
272284
def_map = module_id.def_map(db);
273285
scope = &def_map[module_id.local_id].scope;
274286
import = i;
@@ -284,7 +296,7 @@ impl ItemScope {
284296
while let Some(&m) = scope.use_imports_values.get(&ImportOrGlob::Import(import)) {
285297
match m {
286298
ImportOrDef::Import(i) => {
287-
let module_id = i.import.lookup(db).container;
299+
let module_id = i.use_.lookup(db).container;
288300
def_map = module_id.def_map(db);
289301
scope = &def_map[module_id.local_id].scope;
290302
import = i;
@@ -545,17 +557,21 @@ impl ItemScope {
545557
self.unnamed_trait_imports.get(&tr).map(|trait_| trait_.vis)
546558
}
547559

548-
pub(crate) fn push_unnamed_trait(&mut self, tr: TraitId, vis: Visibility) {
549-
// FIXME: import
550-
self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import: None });
560+
pub(crate) fn push_unnamed_trait(
561+
&mut self,
562+
tr: TraitId,
563+
vis: Visibility,
564+
import: Option<ImportId>,
565+
) {
566+
self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import });
551567
}
552568

553569
pub(crate) fn push_res_with_import(
554570
&mut self,
555571
glob_imports: &mut PerNsGlobImports,
556572
lookup: (LocalModuleId, Name),
557573
def: PerNs,
558-
import: Option<ImportType>,
574+
import: Option<ImportOrExternCrate>,
559575
) -> bool {
560576
let mut changed = false;
561577

@@ -566,12 +582,11 @@ impl ItemScope {
566582
match existing {
567583
Entry::Vacant(entry) => {
568584
match import {
569-
Some(ImportType::Glob(_)) => {
585+
Some(ImportOrExternCrate::Glob(_)) => {
570586
glob_imports.types.insert(lookup.clone());
571587
}
572588
_ => _ = glob_imports.types.remove(&lookup),
573589
}
574-
let import = import.map(Into::into);
575590
let prev = std::mem::replace(&mut fld.import, import);
576591
if let Some(import) = import {
577592
self.use_imports_types
@@ -582,7 +597,7 @@ impl ItemScope {
582597
}
583598
Entry::Occupied(mut entry) => {
584599
match import {
585-
Some(ImportType::Glob(..)) => {
600+
Some(ImportOrExternCrate::Glob(..)) => {
586601
// Multiple globs may import the same item and they may
587602
// override visibility from previously resolved globs. This is
588603
// currently handled by `DefCollector`, because we need to
@@ -591,7 +606,6 @@ impl ItemScope {
591606
}
592607
_ => {
593608
if glob_imports.types.remove(&lookup) {
594-
let import = import.map(Into::into);
595609
let prev = std::mem::replace(&mut fld.import, import);
596610
if let Some(import) = import {
597611
self.use_imports_types.insert(
@@ -614,16 +628,12 @@ impl ItemScope {
614628
match existing {
615629
Entry::Vacant(entry) => {
616630
match import {
617-
Some(ImportType::Glob(_)) => {
631+
Some(ImportOrExternCrate::Glob(_)) => {
618632
glob_imports.values.insert(lookup.clone());
619633
}
620634
_ => _ = glob_imports.values.remove(&lookup),
621635
}
622-
let import = match import {
623-
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
624-
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
625-
_ => None,
626-
};
636+
let import = import.and_then(ImportOrExternCrate::import_or_glob);
627637
let prev = std::mem::replace(&mut fld.import, import);
628638
if let Some(import) = import {
629639
self.use_imports_values
@@ -632,15 +642,13 @@ impl ItemScope {
632642
entry.insert(fld);
633643
changed = true;
634644
}
635-
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
645+
Entry::Occupied(mut entry)
646+
if !matches!(import, Some(ImportOrExternCrate::Glob(..))) =>
647+
{
636648
if glob_imports.values.remove(&lookup) {
637649
cov_mark::hit!(import_shadowed);
638650

639-
let import = match import {
640-
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
641-
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
642-
_ => None,
643-
};
651+
let import = import.and_then(ImportOrExternCrate::import_or_glob);
644652
let prev = std::mem::replace(&mut fld.import, import);
645653
if let Some(import) = import {
646654
self.use_imports_values
@@ -659,16 +667,12 @@ impl ItemScope {
659667
match existing {
660668
Entry::Vacant(entry) => {
661669
match import {
662-
Some(ImportType::Glob(_)) => {
670+
Some(ImportOrExternCrate::Glob(_)) => {
663671
glob_imports.macros.insert(lookup.clone());
664672
}
665673
_ => _ = glob_imports.macros.remove(&lookup),
666674
}
667-
let import = match import {
668-
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
669-
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
670-
_ => None,
671-
};
675+
let import = import.and_then(ImportOrExternCrate::import_or_glob);
672676
let prev = std::mem::replace(&mut fld.import, import);
673677
if let Some(import) = import {
674678
self.use_imports_macros.insert(
@@ -679,14 +683,12 @@ impl ItemScope {
679683
entry.insert(fld);
680684
changed = true;
681685
}
682-
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
686+
Entry::Occupied(mut entry)
687+
if !matches!(import, Some(ImportOrExternCrate::Glob(..))) =>
688+
{
683689
if glob_imports.macros.remove(&lookup) {
684690
cov_mark::hit!(import_shadowed);
685-
let import = match import {
686-
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
687-
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
688-
_ => None,
689-
};
691+
let import = import.and_then(ImportOrExternCrate::import_or_glob);
690692
let prev = std::mem::replace(&mut fld.import, import);
691693
if let Some(import) = import {
692694
self.use_imports_macros.insert(
@@ -856,7 +858,7 @@ impl PerNs {
856858
match def {
857859
ModuleDefId::ModuleId(_) => PerNs::types(def, v, import),
858860
ModuleDefId::FunctionId(_) => {
859-
PerNs::values(def, v, import.and_then(ImportOrExternCrate::into_import))
861+
PerNs::values(def, v, import.and_then(ImportOrExternCrate::import_or_glob))
860862
}
861863
ModuleDefId::AdtId(adt) => match adt {
862864
AdtId::UnionId(_) => PerNs::types(def, v, import),
@@ -871,14 +873,14 @@ impl PerNs {
871873
},
872874
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v, import),
873875
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => {
874-
PerNs::values(def, v, import.and_then(ImportOrExternCrate::into_import))
876+
PerNs::values(def, v, import.and_then(ImportOrExternCrate::import_or_glob))
875877
}
876878
ModuleDefId::TraitId(_) => PerNs::types(def, v, import),
877879
ModuleDefId::TraitAliasId(_) => PerNs::types(def, v, import),
878880
ModuleDefId::TypeAliasId(_) => PerNs::types(def, v, import),
879881
ModuleDefId::BuiltinType(_) => PerNs::types(def, v, import),
880882
ModuleDefId::MacroId(mac) => {
881-
PerNs::macros(mac, v, import.and_then(ImportOrExternCrate::into_import))
883+
PerNs::macros(mac, v, import.and_then(ImportOrExternCrate::import_or_glob))
882884
}
883885
}
884886
}

0 commit comments

Comments
 (0)