Skip to content

Commit

Permalink
Merge pull request #19018 from Veykril/push-wxqqunxwrply
Browse files Browse the repository at this point in the history
internal: Record the use tree index in glob imports
  • Loading branch information
Veykril authored Jan 24, 2025
2 parents a62e2f5 + 433888c commit 6e4c29f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 95 deletions.
4 changes: 2 additions & 2 deletions crates/hir-def/src/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ impl ImportMap {
let attr_id = if let Some(import) = import {
match import {
ImportOrExternCrate::ExternCrate(id) => Some(id.into()),
ImportOrExternCrate::Import(id) => Some(id.import.into()),
ImportOrExternCrate::Glob(id) => Some(id.into()),
ImportOrExternCrate::Import(id) => Some(id.use_.into()),
ImportOrExternCrate::Glob(id) => Some(id.use_.into()),
}
} else {
match item {
Expand Down
122 changes: 62 additions & 60 deletions crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct PerNsGlobImports {

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrExternCrate {
Glob(UseId),
Glob(GlobId),
Import(ImportId),
ExternCrate(ExternCrateId),
}
Expand All @@ -45,29 +45,41 @@ impl From<ImportOrGlob> for ImportOrExternCrate {
}
}

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> {
pub fn import_or_glob(self) -> Option<ImportOrGlob> {
match self {
ImportOrExternCrate::Import(it) => Some(ImportOrGlob::Import(it)),
ImportOrExternCrate::Glob(it) => Some(ImportOrGlob::Glob(it)),
_ => None,
}
}

pub fn import(self) -> Option<ImportId> {
match self {
ImportOrExternCrate::Import(it) => Some(it),
_ => None,
}
}

pub fn glob(self) -> Option<GlobId> {
match self {
ImportOrExternCrate::Glob(id) => Some(id),
_ => None,
}
}

pub fn use_(self) -> Option<UseId> {
match self {
ImportOrExternCrate::Glob(id) => Some(id.use_),
ImportOrExternCrate::Import(id) => Some(id.use_),
_ => None,
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrGlob {
Glob(UseId),
Glob(GlobId),
Import(ImportId),
}

Expand All @@ -79,17 +91,11 @@ impl ImportOrGlob {
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ImportType {
Import(ImportId),
Glob(UseId),
ExternCrate(ExternCrateId),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ImportOrDef {
Import(ImportId),
Glob(UseId),
Glob(GlobId),
ExternCrate(ExternCrateId),
Def(ModuleDefId),
}
Expand All @@ -115,7 +121,13 @@ impl From<ImportOrGlob> for ImportOrDef {

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ImportId {
pub import: UseId,
pub use_: UseId,
pub idx: Idx<ast::UseTree>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct GlobId {
pub use_: UseId,
pub idx: Idx<ast::UseTree>,
}

Expand Down Expand Up @@ -236,7 +248,7 @@ impl ItemScope {
self.use_imports_types
.keys()
.copied()
.filter_map(ImportOrExternCrate::into_import)
.filter_map(ImportOrExternCrate::import_or_glob)
.chain(self.use_imports_values.keys().copied())
.chain(self.use_imports_macros.keys().copied())
.filter_map(ImportOrGlob::into_import)
Expand All @@ -252,7 +264,7 @@ impl ItemScope {
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;
let module_id = i.use_.lookup(db).container;
def_map = module_id.def_map(db);
scope = &def_map[module_id.local_id].scope;
import = i;
Expand All @@ -268,7 +280,7 @@ impl ItemScope {
while let Some(&m) = scope.use_imports_types.get(&ImportOrExternCrate::Import(import)) {
match m {
ImportOrDef::Import(i) => {
let module_id = i.import.lookup(db).container;
let module_id = i.use_.lookup(db).container;
def_map = module_id.def_map(db);
scope = &def_map[module_id.local_id].scope;
import = i;
Expand All @@ -284,7 +296,7 @@ impl ItemScope {
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;
let module_id = i.use_.lookup(db).container;
def_map = module_id.def_map(db);
scope = &def_map[module_id.local_id].scope;
import = i;
Expand Down Expand Up @@ -545,17 +557,21 @@ impl ItemScope {
self.unnamed_trait_imports.get(&tr).map(|trait_| trait_.vis)
}

pub(crate) fn push_unnamed_trait(&mut self, tr: TraitId, vis: Visibility) {
// FIXME: import
self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import: None });
pub(crate) fn push_unnamed_trait(
&mut self,
tr: TraitId,
vis: Visibility,
import: Option<ImportId>,
) {
self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import });
}

pub(crate) fn push_res_with_import(
&mut self,
glob_imports: &mut PerNsGlobImports,
lookup: (LocalModuleId, Name),
def: PerNs,
import: Option<ImportType>,
import: Option<ImportOrExternCrate>,
) -> bool {
let mut changed = false;

Expand All @@ -566,12 +582,11 @@ impl ItemScope {
match existing {
Entry::Vacant(entry) => {
match import {
Some(ImportType::Glob(_)) => {
Some(ImportOrExternCrate::Glob(_)) => {
glob_imports.types.insert(lookup.clone());
}
_ => _ = glob_imports.types.remove(&lookup),
}
let import = import.map(Into::into);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_types
Expand All @@ -582,7 +597,7 @@ impl ItemScope {
}
Entry::Occupied(mut entry) => {
match import {
Some(ImportType::Glob(..)) => {
Some(ImportOrExternCrate::Glob(..)) => {
// Multiple globs may import the same item and they may
// override visibility from previously resolved globs. This is
// currently handled by `DefCollector`, because we need to
Expand All @@ -591,7 +606,6 @@ impl ItemScope {
}
_ => {
if glob_imports.types.remove(&lookup) {
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(
Expand All @@ -614,16 +628,12 @@ impl ItemScope {
match existing {
Entry::Vacant(entry) => {
match import {
Some(ImportType::Glob(_)) => {
Some(ImportOrExternCrate::Glob(_)) => {
glob_imports.values.insert(lookup.clone());
}
_ => _ = glob_imports.values.remove(&lookup),
}
let import = match import {
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let import = import.and_then(ImportOrExternCrate::import_or_glob);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_values
Expand All @@ -632,15 +642,13 @@ impl ItemScope {
entry.insert(fld);
changed = true;
}
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
Entry::Occupied(mut entry)
if !matches!(import, Some(ImportOrExternCrate::Glob(..))) =>
{
if glob_imports.values.remove(&lookup) {
cov_mark::hit!(import_shadowed);

let import = match import {
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let import = import.and_then(ImportOrExternCrate::import_or_glob);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_values
Expand All @@ -659,16 +667,12 @@ impl ItemScope {
match existing {
Entry::Vacant(entry) => {
match import {
Some(ImportType::Glob(_)) => {
Some(ImportOrExternCrate::Glob(_)) => {
glob_imports.macros.insert(lookup.clone());
}
_ => _ = glob_imports.macros.remove(&lookup),
}
let import = match import {
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let import = import.and_then(ImportOrExternCrate::import_or_glob);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_macros.insert(
Expand All @@ -679,14 +683,12 @@ impl ItemScope {
entry.insert(fld);
changed = true;
}
Entry::Occupied(mut entry) if !matches!(import, Some(ImportType::Glob(..))) => {
Entry::Occupied(mut entry)
if !matches!(import, Some(ImportOrExternCrate::Glob(..))) =>
{
if glob_imports.macros.remove(&lookup) {
cov_mark::hit!(import_shadowed);
let import = match import {
Some(ImportType::Import(import)) => Some(ImportOrGlob::Import(import)),
Some(ImportType::Glob(u)) => Some(ImportOrGlob::Glob(u)),
_ => None,
};
let import = import.and_then(ImportOrExternCrate::import_or_glob);
let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_macros.insert(
Expand Down Expand Up @@ -856,7 +858,7 @@ impl PerNs {
match def {
ModuleDefId::ModuleId(_) => PerNs::types(def, v, import),
ModuleDefId::FunctionId(_) => {
PerNs::values(def, v, import.and_then(ImportOrExternCrate::into_import))
PerNs::values(def, v, import.and_then(ImportOrExternCrate::import_or_glob))
}
ModuleDefId::AdtId(adt) => match adt {
AdtId::UnionId(_) => PerNs::types(def, v, import),
Expand All @@ -871,14 +873,14 @@ impl PerNs {
},
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v, import),
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => {
PerNs::values(def, v, import.and_then(ImportOrExternCrate::into_import))
PerNs::values(def, v, import.and_then(ImportOrExternCrate::import_or_glob))
}
ModuleDefId::TraitId(_) => PerNs::types(def, v, import),
ModuleDefId::TraitAliasId(_) => PerNs::types(def, v, import),
ModuleDefId::TypeAliasId(_) => PerNs::types(def, v, import),
ModuleDefId::BuiltinType(_) => PerNs::types(def, v, import),
ModuleDefId::MacroId(mac) => {
PerNs::macros(mac, v, import.and_then(ImportOrExternCrate::into_import))
PerNs::macros(mac, v, import.and_then(ImportOrExternCrate::import_or_glob))
}
}
}
Expand Down
Loading

0 comments on commit 6e4c29f

Please sign in to comment.