Skip to content

Commit c4e9b5a

Browse files
committed
Add import info to item scope dumps
1 parent af80482 commit c4e9b5a

File tree

8 files changed

+360
-211
lines changed

8 files changed

+360
-211
lines changed

crates/hir-def/src/body/tests/block.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ fn outer() {
3838
"#,
3939
expect![[r#"
4040
block scope
41-
CrateStruct: t
42-
PlainStruct: t v
43-
SelfStruct: t
41+
CrateStruct: ti
42+
PlainStruct: ti vi
43+
SelfStruct: ti
4444
Struct: v
4545
SuperStruct: _
4646
@@ -66,7 +66,7 @@ fn outer() {
6666
"#,
6767
expect![[r#"
6868
block scope
69-
imported: t v
69+
imported: ti vi
7070
name: v
7171
7272
crate
@@ -92,9 +92,9 @@ fn outer() {
9292
"#,
9393
expect![[r#"
9494
block scope
95-
inner1: t
95+
inner1: ti
9696
inner2: v
97-
outer: v
97+
outer: vi
9898
9999
block scope
100100
inner: v
@@ -121,7 +121,7 @@ struct Struct {}
121121
"#,
122122
expect![[r#"
123123
block scope
124-
Struct: t
124+
Struct: ti
125125
126126
crate
127127
Struct: t
@@ -153,7 +153,7 @@ fn outer() {
153153
"#,
154154
expect![[r#"
155155
block scope
156-
ResolveMe: t
156+
ResolveMe: ti
157157
158158
block scope
159159
m2: t
@@ -214,7 +214,7 @@ fn f() {
214214
"#,
215215
expect![[r#"
216216
block scope
217-
ResolveMe: t
217+
ResolveMe: ti
218218
219219
block scope
220220
h: v
@@ -292,7 +292,7 @@ pub mod cov_mark {
292292
nested: v
293293
294294
crate
295-
cov_mark: t
295+
cov_mark: ti
296296
f: v
297297
"#]],
298298
);

crates/hir-def/src/item_scope.rs

Lines changed: 189 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ use crate::{
2020
UseId,
2121
};
2222

23-
#[derive(Copy, Clone, Debug)]
24-
pub(crate) enum ImportType {
25-
Glob,
26-
Named,
27-
}
28-
2923
#[derive(Debug, Default)]
3024
pub struct PerNsGlobImports {
3125
types: FxHashSet<(LocalModuleId, Name)>,
@@ -35,6 +29,12 @@ pub struct PerNsGlobImports {
3529

3630
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3731
pub enum ImportOrExternCrate {
32+
Import(ImportId),
33+
ExternCrate(ExternCrateId),
34+
}
35+
36+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
37+
pub enum ImportType {
3838
Import(ImportId),
3939
Glob(UseId),
4040
ExternCrate(ExternCrateId),
@@ -47,13 +47,6 @@ impl ImportOrExternCrate {
4747
_ => None,
4848
}
4949
}
50-
51-
pub fn into_glob(self) -> Option<UseId> {
52-
match self {
53-
ImportOrExternCrate::Glob(it) => Some(it),
54-
_ => None,
55-
}
56-
}
5750
}
5851

5952
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -89,9 +82,9 @@ pub struct ItemScope {
8982
unnamed_trait_imports: FxHashMap<TraitId, (Visibility, Option<ImportId>)>,
9083

9184
// the resolutions of the imports of this scope
92-
use_imports_types: FxHashMap<UseId, ImportOrDef>,
93-
use_imports_values: FxHashMap<UseId, ImportOrDef>,
94-
use_imports_macros: FxHashMap<UseId, ImportOrDef>,
85+
use_imports_types: FxHashMap<ImportOrExternCrate, ImportOrDef>,
86+
use_imports_values: FxHashMap<ImportId, ImportOrDef>,
87+
use_imports_macros: FxHashMap<ImportId, ImportOrDef>,
9588

9689
use_decls: Vec<UseId>,
9790
extern_crate_decls: Vec<ExternCrateId>,
@@ -347,51 +340,185 @@ impl ItemScope {
347340
glob_imports: &mut PerNsGlobImports,
348341
lookup: (LocalModuleId, Name),
349342
def: PerNs,
350-
def_import_type: ImportType,
343+
import: Option<ImportType>,
351344
) -> bool {
352345
let mut changed = false;
353346

354-
macro_rules! check_changed {
355-
(
356-
$changed:ident,
357-
( $this:ident / $def:ident ) . $field:ident,
358-
$glob_imports:ident [ $lookup:ident ],
359-
$def_import_type:ident
360-
) => {{
361-
if let Some(fld) = $def.$field {
362-
let existing = $this.$field.entry($lookup.1.clone());
363-
match existing {
364-
Entry::Vacant(entry) => {
365-
match $def_import_type {
366-
ImportType::Glob => {
367-
$glob_imports.$field.insert($lookup.clone());
347+
if let Some(mut fld) = def.types {
348+
let existing = self.types.entry(lookup.1.clone());
349+
match existing {
350+
Entry::Vacant(entry) => {
351+
match import {
352+
Some(ImportType::Glob(_)) => {
353+
glob_imports.types.insert(lookup.clone());
354+
}
355+
_ => _ = glob_imports.types.remove(&lookup),
356+
}
357+
let import = match import {
358+
Some(ImportType::ExternCrate(extern_crate)) => {
359+
Some(ImportOrExternCrate::ExternCrate(extern_crate))
360+
}
361+
Some(ImportType::Import(import)) => {
362+
Some(ImportOrExternCrate::Import(import))
363+
}
364+
None | Some(ImportType::Glob(_)) => None,
365+
};
366+
let prev = std::mem::replace(&mut fld.2, import);
367+
if let Some(ImportOrExternCrate::Import(import)) = import {
368+
self.use_imports_values.insert(
369+
import,
370+
match prev {
371+
Some(ImportOrExternCrate::Import(import)) => {
372+
ImportOrDef::Import(import)
368373
}
369-
ImportType::Named => {
370-
$glob_imports.$field.remove(&$lookup);
374+
Some(ImportOrExternCrate::ExternCrate(import)) => {
375+
ImportOrDef::ExternCrate(import)
371376
}
377+
None => ImportOrDef::Def(fld.0),
378+
},
379+
);
380+
}
381+
entry.insert(fld);
382+
changed = true;
383+
}
384+
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
385+
if glob_imports.types.remove(&lookup) {
386+
let import = match import {
387+
Some(ImportType::ExternCrate(extern_crate)) => {
388+
Some(ImportOrExternCrate::ExternCrate(extern_crate))
372389
}
390+
Some(ImportType::Import(import)) => {
391+
Some(ImportOrExternCrate::Import(import))
392+
}
393+
None | Some(ImportType::Glob(_)) => None,
394+
};
395+
let prev = std::mem::replace(&mut fld.2, import);
396+
if let Some(ImportOrExternCrate::Import(import)) = import {
397+
self.use_imports_values.insert(
398+
import,
399+
match prev {
400+
Some(ImportOrExternCrate::Import(import)) => {
401+
ImportOrDef::Import(import)
402+
}
403+
Some(ImportOrExternCrate::ExternCrate(import)) => {
404+
ImportOrDef::ExternCrate(import)
405+
}
406+
None => ImportOrDef::Def(fld.0),
407+
},
408+
);
409+
}
410+
cov_mark::hit!(import_shadowed);
411+
entry.insert(fld);
412+
changed = true;
413+
}
414+
}
415+
_ => {}
416+
}
417+
}
373418

374-
entry.insert(fld);
375-
$changed = true;
419+
if let Some(mut fld) = def.values {
420+
let existing = self.values.entry(lookup.1.clone());
421+
match existing {
422+
Entry::Vacant(entry) => {
423+
match import {
424+
Some(ImportType::Glob(_)) => {
425+
glob_imports.values.insert(lookup.clone());
376426
}
377-
Entry::Occupied(mut entry)
378-
if matches!($def_import_type, ImportType::Named) =>
379-
{
380-
if $glob_imports.$field.remove(&$lookup) {
381-
cov_mark::hit!(import_shadowed);
382-
entry.insert(fld);
383-
$changed = true;
384-
}
427+
_ => _ = glob_imports.values.remove(&lookup),
428+
}
429+
let import = match import {
430+
Some(ImportType::Import(import)) => Some(import),
431+
_ => None,
432+
};
433+
let prev = std::mem::replace(&mut fld.2, import);
434+
if let Some(import) = import {
435+
self.use_imports_values.insert(
436+
import,
437+
match prev {
438+
Some(import) => ImportOrDef::Import(import),
439+
None => ImportOrDef::Def(fld.0),
440+
},
441+
);
442+
}
443+
entry.insert(fld);
444+
changed = true;
445+
}
446+
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
447+
if glob_imports.values.remove(&lookup) {
448+
cov_mark::hit!(import_shadowed);
449+
let import = match import {
450+
Some(ImportType::Import(import)) => Some(import),
451+
_ => None,
452+
};
453+
let prev = std::mem::replace(&mut fld.2, import);
454+
if let Some(import) = import {
455+
self.use_imports_values.insert(
456+
import,
457+
match prev {
458+
Some(import) => ImportOrDef::Import(import),
459+
None => ImportOrDef::Def(fld.0),
460+
},
461+
);
385462
}
386-
_ => {}
463+
entry.insert(fld);
464+
changed = true;
387465
}
388466
}
389-
}};
467+
_ => {}
468+
}
390469
}
391470

392-
check_changed!(changed, (self / def).types, glob_imports[lookup], def_import_type);
393-
check_changed!(changed, (self / def).values, glob_imports[lookup], def_import_type);
394-
check_changed!(changed, (self / def).macros, glob_imports[lookup], def_import_type);
471+
if let Some(mut fld) = def.macros {
472+
let existing = self.macros.entry(lookup.1.clone());
473+
match existing {
474+
Entry::Vacant(entry) => {
475+
match import {
476+
Some(ImportType::Glob(_)) => {
477+
glob_imports.macros.insert(lookup.clone());
478+
}
479+
_ => _ = glob_imports.macros.remove(&lookup),
480+
}
481+
let import = match import {
482+
Some(ImportType::Import(import)) => Some(import),
483+
_ => None,
484+
};
485+
let prev = std::mem::replace(&mut fld.2, import);
486+
if let Some(import) = import {
487+
self.use_imports_macros.insert(
488+
import,
489+
match prev {
490+
Some(import) => ImportOrDef::Import(import),
491+
None => ImportOrDef::Def(fld.0.into()),
492+
},
493+
);
494+
}
495+
entry.insert(fld);
496+
changed = true;
497+
}
498+
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
499+
if glob_imports.macros.remove(&lookup) {
500+
cov_mark::hit!(import_shadowed);
501+
let import = match import {
502+
Some(ImportType::Import(import)) => Some(import),
503+
_ => None,
504+
};
505+
let prev = std::mem::replace(&mut fld.2, import);
506+
if let Some(import) = import {
507+
self.use_imports_macros.insert(
508+
import,
509+
match prev {
510+
Some(import) => ImportOrDef::Import(import),
511+
None => ImportOrDef::Def(fld.0.into()),
512+
},
513+
);
514+
}
515+
entry.insert(fld);
516+
changed = true;
517+
}
518+
}
519+
_ => {}
520+
}
521+
}
395522

396523
if def.is_none() && self.unresolved.insert(lookup.1) {
397524
changed = true;
@@ -430,14 +557,25 @@ impl ItemScope {
430557
name.map_or("_".to_string(), |name| name.display(db).to_string())
431558
);
432559

433-
if def.types.is_some() {
560+
if let Some((.., i)) = def.types {
434561
buf.push_str(" t");
562+
match i {
563+
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
564+
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
565+
None => (),
566+
}
435567
}
436-
if def.values.is_some() {
568+
if let Some((.., i)) = def.values {
437569
buf.push_str(" v");
570+
if i.is_some() {
571+
buf.push('i');
572+
}
438573
}
439-
if def.macros.is_some() {
574+
if let Some((.., i)) = def.macros {
440575
buf.push_str(" m");
576+
if i.is_some() {
577+
buf.push('i');
578+
}
441579
}
442580
if def.is_none() {
443581
buf.push_str(" _");

0 commit comments

Comments
 (0)