Fix ItemScope not recording glob imports

This caused us other code to incorrectly assume in dealing with a declaration when in fact it was dealing with a glob imported definition
This commit is contained in:
Lukas Wirth 2025-01-24 12:56:39 +01:00
parent c78cc2baa9
commit 3b88a2f134
11 changed files with 239 additions and 213 deletions

View file

@ -475,7 +475,7 @@ fn outer() {
block scope::tests
name: _
outer: v
outer: vg
crate
outer: v

View file

@ -169,6 +169,7 @@ impl ImportMap {
match import {
ImportOrExternCrate::ExternCrate(id) => Some(id.into()),
ImportOrExternCrate::Import(id) => Some(id.import.into()),
ImportOrExternCrate::Glob(id) => Some(id.into()),
}
} else {
match item {

View file

@ -31,10 +31,54 @@ pub struct PerNsGlobImports {
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrExternCrate {
Glob(UseId),
Import(ImportId),
ExternCrate(ExternCrateId),
}
impl From<ImportOrGlob> for ImportOrExternCrate {
fn from(value: ImportOrGlob) -> Self {
match value {
ImportOrGlob::Glob(it) => ImportOrExternCrate::Glob(it),
ImportOrGlob::Import(it) => ImportOrExternCrate::Import(it),
}
}
}
impl From<ImportType> for ImportOrExternCrate {
fn from(value: ImportType) -> Self {
match value {
ImportType::Glob(it) => ImportOrExternCrate::Glob(it),
ImportType::Import(it) => ImportOrExternCrate::Import(it),
ImportType::ExternCrate(it) => ImportOrExternCrate::ExternCrate(it),
}
}
}
impl ImportOrExternCrate {
pub fn into_import(self) -> Option<ImportOrGlob> {
match self {
ImportOrExternCrate::Import(it) => Some(ImportOrGlob::Import(it)),
ImportOrExternCrate::Glob(it) => Some(ImportOrGlob::Glob(it)),
_ => None,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrGlob {
Glob(UseId),
Import(ImportId),
}
impl ImportOrGlob {
pub fn into_import(self) -> Option<ImportId> {
match self {
ImportOrGlob::Import(it) => Some(it),
_ => None,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ImportType {
Import(ImportId),
@ -42,21 +86,33 @@ pub(crate) enum ImportType {
ExternCrate(ExternCrateId),
}
impl ImportOrExternCrate {
pub fn into_import(self) -> Option<ImportId> {
match self {
ImportOrExternCrate::Import(it) => Some(it),
_ => None,
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrDef {
Import(ImportId),
Glob(UseId),
ExternCrate(ExternCrateId),
Def(ModuleDefId),
}
impl From<ImportOrExternCrate> for ImportOrDef {
fn from(value: ImportOrExternCrate) -> Self {
match value {
ImportOrExternCrate::Import(it) => ImportOrDef::Import(it),
ImportOrExternCrate::Glob(it) => ImportOrDef::Glob(it),
ImportOrExternCrate::ExternCrate(it) => ImportOrDef::ExternCrate(it),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrDef {
Import(ImportId),
ExternCrate(ExternCrateId),
Def(ModuleDefId),
impl From<ImportOrGlob> for ImportOrDef {
fn from(value: ImportOrGlob) -> Self {
match value {
ImportOrGlob::Import(it) => ImportOrDef::Import(it),
ImportOrGlob::Glob(it) => ImportOrDef::Glob(it),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ImportId {
pub import: UseId,
@ -96,8 +152,8 @@ pub struct ItemScope {
// the resolutions of the imports of this scope
use_imports_types: FxHashMap<ImportOrExternCrate, ImportOrDef>,
use_imports_values: FxHashMap<ImportId, ImportOrDef>,
use_imports_macros: FxHashMap<ImportId, ImportOrDef>,
use_imports_values: FxHashMap<ImportOrGlob, ImportOrDef>,
use_imports_macros: FxHashMap<ImportOrGlob, ImportOrDef>,
use_decls: Vec<UseId>,
extern_crate_decls: Vec<ExternCrateId>,
@ -162,7 +218,7 @@ impl ItemScope {
.map(move |name| (name, self.get(name)))
}
pub fn values(&self) -> impl Iterator<Item = (&Name, Item<ModuleDefId, ImportId>)> + '_ {
pub fn values(&self) -> impl Iterator<Item = (&Name, Item<ModuleDefId, ImportOrGlob>)> + '_ {
self.values.iter().map(|(n, &i)| (n, i))
}
@ -172,7 +228,7 @@ impl ItemScope {
self.types.iter().map(|(n, &i)| (n, i))
}
pub fn macros(&self) -> impl Iterator<Item = (&Name, Item<MacroId, ImportId>)> + '_ {
pub fn macros(&self) -> impl Iterator<Item = (&Name, Item<MacroId, ImportOrGlob>)> + '_ {
self.macros.iter().map(|(n, &i)| (n, i))
}
@ -183,6 +239,7 @@ impl ItemScope {
.filter_map(ImportOrExternCrate::into_import)
.chain(self.use_imports_values.keys().copied())
.chain(self.use_imports_macros.keys().copied())
.filter_map(ImportOrGlob::into_import)
.sorted()
.dedup()
}
@ -192,7 +249,7 @@ impl ItemScope {
let mut def_map;
let mut scope = self;
while let Some(&m) = scope.use_imports_macros.get(&import) {
while let Some(&m) = scope.use_imports_macros.get(&ImportOrGlob::Import(import)) {
match m {
ImportOrDef::Import(i) => {
let module_id = i.import.lookup(db).container;
@ -224,7 +281,7 @@ impl ItemScope {
}
}
let mut scope = self;
while let Some(&m) = scope.use_imports_values.get(&import) {
while let Some(&m) = scope.use_imports_values.get(&ImportOrGlob::Import(import)) {
match m {
ImportOrDef::Import(i) => {
let module_id = i.import.lookup(db).container;
@ -514,29 +571,11 @@ impl ItemScope {
}
_ => _ = glob_imports.types.remove(&lookup),
}
let import = match import {
Some(ImportType::ExternCrate(extern_crate)) => {
Some(ImportOrExternCrate::ExternCrate(extern_crate))
}
Some(ImportType::Import(import)) => {
Some(ImportOrExternCrate::Import(import))
}
None | Some(ImportType::Glob(_)) => None,
};
let import = import.map(Into::into);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_types.insert(
import,
match prev {
Some(ImportOrExternCrate::Import(import)) => {
ImportOrDef::Import(import)
}
Some(ImportOrExternCrate::ExternCrate(import)) => {
ImportOrDef::ExternCrate(import)
}
None => ImportOrDef::Def(fld.def),
},
);
self.use_imports_types
.insert(import, prev.map_or(ImportOrDef::Def(fld.def), Into::into));
}
entry.insert(fld);
changed = true;
@ -552,28 +591,12 @@ impl ItemScope {
}
_ => {
if glob_imports.types.remove(&lookup) {
let import = match import {
Some(ImportType::ExternCrate(extern_crate)) => {
Some(ImportOrExternCrate::ExternCrate(extern_crate))
}
Some(ImportType::Import(import)) => {
Some(ImportOrExternCrate::Import(import))
}
None | Some(ImportType::Glob(_)) => None,
};
let import = import.map(Into::into);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_types.insert(
import,
match prev {
Some(ImportOrExternCrate::Import(import)) => {
ImportOrDef::Import(import)
}
Some(ImportOrExternCrate::ExternCrate(import)) => {
ImportOrDef::ExternCrate(import)
}
None => ImportOrDef::Def(fld.def),
},
prev.map_or(ImportOrDef::Def(fld.def), Into::into),
);
}
cov_mark::hit!(import_shadowed);
@ -597,18 +620,14 @@ impl ItemScope {
_ => _ = glob_imports.values.remove(&lookup),
}
let import = match import {
Some(ImportType::Import(import)) => Some(import),
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_values.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
None => ImportOrDef::Def(fld.def),
},
);
self.use_imports_values
.insert(import, prev.map_or(ImportOrDef::Def(fld.def), Into::into));
}
entry.insert(fld);
changed = true;
@ -616,19 +635,16 @@ impl ItemScope {
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
if glob_imports.values.remove(&lookup) {
cov_mark::hit!(import_shadowed);
let import = match import {
Some(ImportType::Import(import)) => Some(import),
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_values.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
None => ImportOrDef::Def(fld.def),
},
);
self.use_imports_values
.insert(import, prev.map_or(ImportOrDef::Def(fld.def), Into::into));
}
entry.insert(fld);
changed = true;
@ -649,17 +665,15 @@ impl ItemScope {
_ => _ = glob_imports.macros.remove(&lookup),
}
let import = match import {
Some(ImportType::Import(import)) => Some(import),
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_macros.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
None => ImportOrDef::Def(fld.def.into()),
},
prev.map_or_else(|| ImportOrDef::Def(fld.def.into()), Into::into),
);
}
entry.insert(fld);
@ -669,17 +683,15 @@ impl ItemScope {
if glob_imports.macros.remove(&lookup) {
cov_mark::hit!(import_shadowed);
let import = match import {
Some(ImportType::Import(import)) => Some(import),
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_macros.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
None => ImportOrDef::Def(fld.def.into()),
},
prev.map_or_else(|| ImportOrDef::Def(fld.def.into()), Into::into),
);
}
entry.insert(fld);
@ -704,16 +716,27 @@ impl ItemScope {
.map(|def| &mut def.vis)
.chain(self.values.values_mut().map(|def| &mut def.vis))
.chain(self.unnamed_trait_imports.values_mut().map(|def| &mut def.vis))
.for_each(|vis| {
*vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit)
.for_each(|vis| match vis {
&mut Visibility::Module(_, visibility_explicitness) => {
*vis = Visibility::Module(this_module, visibility_explicitness)
}
Visibility::Public => {
*vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit)
}
});
for mac in self.macros.values_mut() {
if matches!(mac.def, MacroId::ProcMacroId(_) if mac.import.is_none()) {
continue;
}
mac.vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit);
match mac.vis {
Visibility::Module(_, visibility_explicitness) => {
mac.vis = Visibility::Module(this_module, visibility_explicitness)
}
Visibility::Public => {
mac.vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit)
}
}
}
}
@ -732,20 +755,25 @@ impl ItemScope {
buf.push_str(" t");
match import {
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
Some(ImportOrExternCrate::Glob(_)) => buf.push('g'),
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
None => (),
}
}
if let Some(Item { import, .. }) = def.values {
buf.push_str(" v");
if import.is_some() {
buf.push('i');
match import {
Some(ImportOrGlob::Import(_)) => buf.push('i'),
Some(ImportOrGlob::Glob(_)) => buf.push('g'),
None => (),
}
}
if let Some(Item { import, .. }) = def.macros {
buf.push_str(" m");
if import.is_some() {
buf.push('i');
match import {
Some(ImportOrGlob::Import(_)) => buf.push('i'),
Some(ImportOrGlob::Glob(_)) => buf.push('g'),
None => (),
}
}
if def.is_none() {

View file

@ -28,7 +28,7 @@ use triomphe::Arc;
use crate::{
attr::Attrs,
db::DefDatabase,
item_scope::{ImportId, ImportOrExternCrate, ImportType, PerNsGlobImports},
item_scope::{ImportId, ImportOrExternCrate, ImportOrGlob, ImportType, PerNsGlobImports},
item_tree::{
self, AttrOwner, FieldsShape, FileItemTreeId, ImportKind, ItemTree, ItemTreeId,
ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, UseTreeKind,
@ -527,7 +527,10 @@ impl DefCollector<'_> {
// FIXME: This should specifically look for a glob import somehow and record that here
self.def_map.prelude = Some((
m,
import.and_then(ImportOrExternCrate::into_import).map(|it| it.import),
import
.and_then(ImportOrExternCrate::into_import)
.and_then(ImportOrGlob::into_import)
.map(|it| it.import),
));
}
types => {

View file

@ -103,8 +103,8 @@ mod a {
c: t
crate::a::b::c
A: v
b: t
A: vg
b: tg
"#]],
);
}
@ -256,8 +256,8 @@ pub enum Foo { Bar, Baz }
"#,
expect![[r#"
crate
Bar: t v
Baz: t v
Bar: tg vg
Baz: tg vg
"#]],
);
}
@ -421,10 +421,10 @@ pub struct NotExported;
"#,
expect![[r#"
crate
Exported: t v
PublicItem: t v
allowed_reexport: t
exported: t
Exported: tg vg
PublicItem: tg vg
allowed_reexport: tg
exported: tg
not_allowed_reexport1: _
not_allowed_reexport2: _
"#]],
@ -692,7 +692,7 @@ mod b {
b: t
crate::a
T: t v
T: t vg
crate::b
T: v

View file

@ -18,9 +18,9 @@ pub struct Baz;
"#,
expect![[r#"
crate
Baz: t v
Foo: t v
bar: t
Baz: tg vg
Foo: tg vg
bar: tg
foo: t
crate::foo
@ -53,20 +53,20 @@ pub use super::*;
"#,
expect![[r#"
crate
Baz: t v
Foo: t v
bar: t
Baz: tg vg
Foo: tg vg
bar: tg
foo: t
crate::foo
Baz: t v
Baz: tg vg
Foo: t v
bar: t
crate::foo::bar
Baz: t v
Foo: t v
bar: t
Foo: tg vg
bar: tg
"#]],
);
}
@ -91,20 +91,20 @@ pub use super::*;
",
expect![[r#"
crate
Baz: t v
bar: t
Baz: tg vg
bar: tg
foo: t
crate::foo
Baz: t v
Baz: tg vg
PrivateStructFoo: t v
bar: t
crate::foo::bar
Baz: t v
PrivateStructBar: t v
PrivateStructFoo: t v
bar: t
PrivateStructFoo: tg vg
bar: tg
"#]],
);
}
@ -130,9 +130,9 @@ pub(crate) struct PubCrateStruct;
",
expect![[r#"
crate
Foo: t
PubCrateStruct: t v
bar: t
Foo: tg
PubCrateStruct: tg vg
bar: tg
foo: t
crate::foo
@ -160,7 +160,7 @@ pub struct Baz;
"#,
expect![[r#"
crate
Baz: t v
Baz: tg vg
"#]],
);
}
@ -178,7 +178,7 @@ struct Foo;
"#,
expect![[r#"
crate
Baz: t v
Baz: tg vg
"#]],
);
}
@ -193,8 +193,8 @@ use self::Foo::*;
"#,
expect![[r#"
crate
Bar: t v
Baz: t v
Bar: tg vg
Baz: tg vg
Foo: t
"#]],
);
@ -210,8 +210,8 @@ use self::Foo::{*};
"#,
expect![[r#"
crate
Bar: t v
Baz: t v
Bar: tg vg
Baz: tg vg
Foo: t
"#]],
);
@ -359,7 +359,7 @@ use event::Event;
event: t
crate::event
Event: t v
Event: t vg
serenity: t
crate::event::serenity
@ -388,10 +388,10 @@ use reexport::*;
"#,
expect![[r#"
crate
Trait: t
Trait: tg
defs: t
function: v
makro: m
function: vg
makro: mg
reexport: t
crate::defs
@ -400,10 +400,10 @@ use reexport::*;
makro: m
crate::reexport
Trait: t
function: v
Trait: tg
function: vg
inner: t
makro: m
makro: mg
crate::reexport::inner
Trait: ti
@ -442,12 +442,12 @@ mod glob_target {
ShouldBePrivate: t v
crate::outer
ShouldBePrivate: t v
ShouldBePrivate: tg vg
inner_superglob: t
crate::outer::inner_superglob
ShouldBePrivate: t v
inner_superglob: t
ShouldBePrivate: tg vg
inner_superglob: tg
"#]],
);
}
@ -473,20 +473,20 @@ use reexport_2::*;
"#,
expect![[r#"
crate
Placeholder: t v
Placeholder: tg vg
libs: t
reexport_1: t
reexport_1: tg
reexport_2: t
crate::libs
Placeholder: t v
crate::reexport_2
Placeholder: t v
Placeholder: tg vg
reexport_1: t
crate::reexport_2::reexport_1
Placeholder: t v
Placeholder: tg vg
"#]],
);
}

View file

@ -97,9 +97,9 @@ macro_rules! structs {
bar: t
crate::bar
Bar: t
Foo: t
bar: t
Bar: tg
Foo: tg
bar: tg
"#]],
);
}
@ -130,9 +130,9 @@ macro_rules! structs {
bar: t
crate::bar
Bar: t
Foo: t
bar: t
Bar: tg
Foo: tg
bar: tg
"#]],
);
}
@ -169,9 +169,9 @@ macro_rules! inner {
bar: t
crate::bar
Bar: t
Foo: t
bar: t
Bar: tg
Foo: tg
bar: tg
"#]],
);
}
@ -794,7 +794,7 @@ pub trait Clone {}
"#,
expect![[r#"
crate
Clone: t m
Clone: tg mg
"#]],
);
}
@ -1075,9 +1075,9 @@ macro_rules! mbe {
"#,
expect![[r#"
crate
DummyTrait: m
attribute_macro: m
function_like_macro: m
DummyTrait: mg
attribute_macro: mg
function_like_macro: mg
"#]],
);
}

View file

@ -6,7 +6,7 @@
use bitflags::bitflags;
use crate::{
item_scope::{ImportId, ImportOrExternCrate, ItemInNs},
item_scope::{ImportId, ImportOrExternCrate, ImportOrGlob, ItemInNs},
visibility::Visibility,
MacroId, ModuleDefId,
};
@ -36,8 +36,8 @@ pub struct Item<Def, Import = ImportId> {
}
pub type TypesItem = Item<ModuleDefId, ImportOrExternCrate>;
pub type ValuesItem = Item<ModuleDefId>;
pub type MacrosItem = Item<MacroId>;
pub type ValuesItem = Item<ModuleDefId, ImportOrGlob>;
pub type MacrosItem = Item<MacroId, ImportOrGlob>;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct PerNs {
@ -59,7 +59,7 @@ impl PerNs {
PerNs { types: None, values: None, macros: None }
}
pub fn values(def: ModuleDefId, vis: Visibility, import: Option<ImportId>) -> PerNs {
pub fn values(def: ModuleDefId, vis: Visibility, import: Option<ImportOrGlob>) -> PerNs {
PerNs { types: None, values: Some(Item { def, vis, import }), macros: None }
}
@ -84,7 +84,7 @@ impl PerNs {
}
}
pub fn macros(def: MacroId, vis: Visibility, import: Option<ImportId>) -> PerNs {
pub fn macros(def: MacroId, vis: Visibility, import: Option<ImportOrGlob>) -> PerNs {
PerNs { types: None, values: None, macros: Some(Item { def, vis, import }) }
}
@ -108,7 +108,7 @@ impl PerNs {
self.values.map(|it| it.def)
}
pub fn take_values_import(self) -> Option<(ModuleDefId, Option<ImportId>)> {
pub fn take_values_import(self) -> Option<(ModuleDefId, Option<ImportOrGlob>)> {
self.values.map(|it| (it.def, it.import))
}
@ -116,7 +116,7 @@ impl PerNs {
self.macros.map(|it| it.def)
}
pub fn take_macros_import(self) -> Option<(MacroId, Option<ImportId>)> {
pub fn take_macros_import(self) -> Option<(MacroId, Option<ImportOrGlob>)> {
self.macros.map(|it| (it.def, it.import))
}
@ -159,14 +159,12 @@ impl PerNs {
.map(|it| (ItemInNs::Types(it.def), it.import))
.into_iter()
.chain(
self.values.map(|it| {
(ItemInNs::Values(it.def), it.import.map(ImportOrExternCrate::Import))
}),
self.values
.map(|it| (ItemInNs::Values(it.def), it.import.map(ImportOrExternCrate::from))),
)
.chain(
self.macros.map(|it| {
(ItemInNs::Macros(it.def), it.import.map(ImportOrExternCrate::Import))
}),
self.macros
.map(|it| (ItemInNs::Macros(it.def), it.import.map(ImportOrExternCrate::from))),
)
}
}

View file

@ -19,7 +19,7 @@ use crate::{
db::DefDatabase,
generics::{GenericParams, TypeOrConstParamData},
hir::{BindingId, ExprId, LabelId},
item_scope::{BuiltinShadowMode, ImportId, ImportOrExternCrate, BUILTIN_SCOPE},
item_scope::{BuiltinShadowMode, ImportOrExternCrate, ImportOrGlob, BUILTIN_SCOPE},
lang_item::LangItemTarget,
nameres::{DefMap, MacroSubNs, ResolvePathResultPrefixInfo},
path::{ModPath, Path, PathKind},
@ -107,7 +107,7 @@ pub enum TypeNs {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ResolveValueResult {
ValueNs(ValueNs, Option<ImportId>),
ValueNs(ValueNs, Option<ImportOrGlob>),
Partial(TypeNs, usize, Option<ImportOrExternCrate>),
}
@ -485,7 +485,7 @@ impl Resolver {
db: &dyn DefDatabase,
path: &ModPath,
expected_macro_kind: Option<MacroSubNs>,
) -> Option<(MacroId, Option<ImportId>)> {
) -> Option<(MacroId, Option<ImportOrGlob>)> {
let (item_map, module) = self.item_scope();
item_map
.resolve_path(db, module, path, BuiltinShadowMode::Other, expected_macro_kind)
@ -1014,7 +1014,7 @@ impl ModuleItemMap {
}
}
fn to_value_ns(per_ns: PerNs) -> Option<(ValueNs, Option<ImportId>)> {
fn to_value_ns(per_ns: PerNs) -> Option<(ValueNs, Option<ImportOrGlob>)> {
let (def, import) = per_ns.take_values_import()?;
let res = match def {
ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it),

View file

@ -3,7 +3,7 @@
use either::Either;
use hir_def::{
db::DefDatabase,
item_scope::{ImportId, ImportOrExternCrate},
item_scope::{ImportId, ImportOrExternCrate, ImportOrGlob},
per_ns::Item,
src::{HasChildSource, HasSource},
visibility::{Visibility, VisibilityExplicitness},
@ -55,9 +55,10 @@ impl DeclarationLocation {
}
/// Represents an outstanding module that the symbol collector must collect symbols from.
#[derive(Debug)]
struct SymbolCollectorWork {
module_id: ModuleId,
parent: Option<DefWithBodyId>,
parent: Option<Name>,
}
pub struct SymbolCollector<'a> {
@ -81,7 +82,15 @@ impl<'a> SymbolCollector<'a> {
}
}
pub fn new_module(db: &dyn HirDatabase, module: Module) -> Box<[FileSymbol]> {
let mut symbol_collector = SymbolCollector::new(db);
symbol_collector.collect(module);
symbol_collector.finish()
}
pub fn collect(&mut self, module: Module) {
let _p = tracing::info_span!("SymbolCollector::collect", ?module).entered();
tracing::info!(?module, "SymbolCollector::collect",);
self.edition = module.krate().edition(self.db);
// The initial work is the root module we're collecting, additional work will
@ -97,16 +106,12 @@ impl<'a> SymbolCollector<'a> {
self.symbols.into_iter().collect()
}
pub fn collect_module(db: &dyn HirDatabase, module: Module) -> Box<[FileSymbol]> {
let mut symbol_collector = SymbolCollector::new(db);
symbol_collector.collect(module);
symbol_collector.finish()
}
fn do_work(&mut self, work: SymbolCollectorWork) {
let _p = tracing::info_span!("SymbolCollector::do_work", ?work).entered();
tracing::info!(?work, "SymbolCollector::do_work");
self.db.unwind_if_cancelled();
let parent_name = work.parent.and_then(|id| self.def_with_body_id_name(id));
let parent_name = work.parent.map(|name| name.as_str().to_smolstr());
self.with_container_name(parent_name, |s| s.collect_from_module(work.module_id));
}
@ -116,18 +121,18 @@ impl<'a> SymbolCollector<'a> {
ModuleDefId::ModuleId(id) => this.push_module(id, name),
ModuleDefId::FunctionId(id) => {
this.push_decl(id, name, false);
this.collect_from_body(id);
this.collect_from_body(id, Some(name.clone()));
}
ModuleDefId::AdtId(AdtId::StructId(id)) => this.push_decl(id, name, false),
ModuleDefId::AdtId(AdtId::EnumId(id)) => this.push_decl(id, name, false),
ModuleDefId::AdtId(AdtId::UnionId(id)) => this.push_decl(id, name, false),
ModuleDefId::ConstId(id) => {
this.push_decl(id, name, false);
this.collect_from_body(id);
this.collect_from_body(id, Some(name.clone()));
}
ModuleDefId::StaticId(id) => {
this.push_decl(id, name, false);
this.collect_from_body(id);
this.collect_from_body(id, Some(name.clone()));
}
ModuleDefId::TraitId(id) => {
this.push_decl(id, name, false);
@ -235,6 +240,7 @@ impl<'a> SymbolCollector<'a> {
if is_explicit_import(vis) {
match i {
ImportOrExternCrate::Import(i) => push_import(self, i, name, def),
ImportOrExternCrate::Glob(_) => (),
ImportOrExternCrate::ExternCrate(i) => {
push_extern_crate(self, i, name, def)
}
@ -249,7 +255,10 @@ impl<'a> SymbolCollector<'a> {
for (name, Item { def, vis, import }) in scope.macros() {
if let Some(i) = import {
if is_explicit_import(vis) {
push_import(self, i, name, def.into());
match i {
ImportOrGlob::Import(i) => push_import(self, i, name, def.into()),
ImportOrGlob::Glob(_) => (),
}
}
continue;
}
@ -260,7 +269,10 @@ impl<'a> SymbolCollector<'a> {
for (name, Item { def, vis, import }) in scope.values() {
if let Some(i) = import {
if is_explicit_import(vis) {
push_import(self, i, name, def);
match i {
ImportOrGlob::Import(i) => push_import(self, i, name, def),
ImportOrGlob::Glob(_) => (),
}
}
continue;
}
@ -269,7 +281,7 @@ impl<'a> SymbolCollector<'a> {
}
for const_id in scope.unnamed_consts() {
self.collect_from_body(const_id);
self.collect_from_body(const_id, None);
}
for (name, id) in scope.legacy_macros() {
@ -285,7 +297,7 @@ impl<'a> SymbolCollector<'a> {
}
}
fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>) {
fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>, name: Option<Name>) {
let body_id = body_id.into();
let body = self.db.body(body_id);
@ -294,7 +306,7 @@ impl<'a> SymbolCollector<'a> {
for (id, _) in def_map.modules() {
self.work.push(SymbolCollectorWork {
module_id: def_map.module_id(id),
parent: Some(body_id),
parent: name.clone(),
});
}
}
@ -333,24 +345,6 @@ impl<'a> SymbolCollector<'a> {
}
}
fn def_with_body_id_name(&self, body_id: DefWithBodyId) -> Option<SmolStr> {
match body_id {
DefWithBodyId::FunctionId(id) => {
Some(self.db.function_data(id).name.display_no_db(self.edition).to_smolstr())
}
DefWithBodyId::StaticId(id) => {
Some(self.db.static_data(id).name.display_no_db(self.edition).to_smolstr())
}
DefWithBodyId::ConstId(id) => {
Some(self.db.const_data(id).name.as_ref()?.display_no_db(self.edition).to_smolstr())
}
DefWithBodyId::VariantId(id) => {
Some(self.db.enum_variant_data(id).name.display_no_db(self.edition).to_smolstr())
}
DefWithBodyId::InTypeConstId(_) => Some("in type const".into()),
}
}
fn push_assoc_item(&mut self, assoc_item_id: AssocItemId, name: &Name) {
match assoc_item_id {
AssocItemId::FunctionId(id) => self.push_decl(id, name, true),

View file

@ -143,7 +143,7 @@ fn library_symbols(db: &dyn SymbolsDatabase, source_root_id: SourceRootId) -> Ar
fn module_symbols(db: &dyn SymbolsDatabase, module: Module) -> Arc<SymbolIndex> {
let _p = tracing::info_span!("module_symbols").entered();
Arc::new(SymbolIndex::new(SymbolCollector::collect_module(db.upcast(), module)))
Arc::new(SymbolIndex::new(SymbolCollector::new_module(db.upcast(), module)))
}
pub fn crate_symbols(db: &dyn SymbolsDatabase, krate: Crate) -> Box<[Arc<SymbolIndex>]> {
@ -284,13 +284,15 @@ impl SymbolIndex {
builder.insert(key, value).unwrap();
}
// FIXME: fst::Map should ideally have a way to shrink the backing buffer without the unwrap dance
let map = fst::Map::new({
let mut buf = builder.into_inner().unwrap();
buf.shrink_to_fit();
buf
})
.unwrap();
let map = builder
.into_inner()
.and_then(|mut buf| {
fst::Map::new({
buf.shrink_to_fit();
buf
})
})
.unwrap();
SymbolIndex { symbols, map }
}
@ -491,7 +493,7 @@ pub(self) use crate::Trait as IsThisJustATrait;
.modules(&db)
.into_iter()
.map(|module_id| {
let mut symbols = SymbolCollector::collect_module(&db, module_id);
let mut symbols = SymbolCollector::new_module(&db, module_id);
symbols.sort_by_key(|it| it.name.as_str().to_owned());
(module_id, symbols)
})
@ -518,7 +520,7 @@ struct Duplicate;
.modules(&db)
.into_iter()
.map(|module_id| {
let mut symbols = SymbolCollector::collect_module(&db, module_id);
let mut symbols = SymbolCollector::new_module(&db, module_id);
symbols.sort_by_key(|it| it.name.as_str().to_owned());
(module_id, symbols)
})