mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 17:18:32 +03:00
module-scoped defloc
This commit is contained in:
+15
-12
@@ -61,15 +61,18 @@ pub fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> Source
|
||||
pub struct DefId(u32);
|
||||
ra_db::impl_numeric_id!(DefId);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub(crate) enum DefKind {
|
||||
Module,
|
||||
Item,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum DefLoc {
|
||||
Module {
|
||||
id: ModuleId,
|
||||
source_root: SourceRootId,
|
||||
},
|
||||
Item {
|
||||
source_item_id: SourceItemId,
|
||||
},
|
||||
pub struct DefLoc {
|
||||
pub(crate) kind: DefKind,
|
||||
source_root_id: SourceRootId,
|
||||
module_id: ModuleId,
|
||||
source_item_id: SourceItemId,
|
||||
}
|
||||
|
||||
impl DefId {
|
||||
@@ -92,12 +95,12 @@ pub enum Def {
|
||||
impl DefId {
|
||||
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
|
||||
let loc = self.loc(db);
|
||||
let res = match loc {
|
||||
DefLoc::Module { id, source_root } => {
|
||||
let descr = Module::new(db, source_root, id)?;
|
||||
let res = match loc.kind {
|
||||
DefKind::Module => {
|
||||
let descr = Module::new(db, loc.source_root_id, loc.module_id)?;
|
||||
Def::Module(descr)
|
||||
}
|
||||
DefLoc::Item { .. } => Def::Item,
|
||||
DefKind::Item => Def::Item,
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
use relative_path::RelativePathBuf;
|
||||
|
||||
use crate::{
|
||||
DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
|
||||
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
|
||||
arena::{Arena, Id},
|
||||
};
|
||||
|
||||
@@ -127,9 +127,11 @@ pub fn name(&self) -> Option<SmolStr> {
|
||||
}
|
||||
|
||||
pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
|
||||
let def_loc = DefLoc::Module {
|
||||
id: self.module_id,
|
||||
source_root: self.source_root_id,
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Module,
|
||||
source_root_id: self.source_root_id,
|
||||
module_id: self.module_id,
|
||||
source_item_id: self.module_id.source(&self.tree).0,
|
||||
};
|
||||
def_loc.id(db)
|
||||
}
|
||||
@@ -161,7 +163,12 @@ pub fn resolve_path(&self, db: &impl HirDatabase, path: Path) -> Cancelable<Opti
|
||||
let segments = path.segments;
|
||||
for name in segments.iter() {
|
||||
let module = match curr.loc(db) {
|
||||
DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?,
|
||||
DefLoc {
|
||||
kind: DefKind::Module,
|
||||
source_root_id,
|
||||
module_id,
|
||||
..
|
||||
} => Module::new(db, source_root_id, module_id)?,
|
||||
_ => return Ok(None),
|
||||
};
|
||||
let scope = module.scope(db)?;
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
use crate::{
|
||||
Cancelable, FileId,
|
||||
DefId, DefLoc,
|
||||
DefId, DefLoc, DefKind,
|
||||
SourceItemId, SourceFileItemId, SourceFileItems,
|
||||
Path, PathKind,
|
||||
HirDatabase,
|
||||
@@ -247,7 +247,10 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) {
|
||||
// handle submodules separatelly
|
||||
continue;
|
||||
}
|
||||
let def_loc = DefLoc::Item {
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Item,
|
||||
source_root_id: self.source_root,
|
||||
module_id,
|
||||
source_item_id: SourceItemId {
|
||||
file_id,
|
||||
item_id: item.id,
|
||||
@@ -261,10 +264,12 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) {
|
||||
module_items.items.insert(item.name.clone(), resolution);
|
||||
}
|
||||
|
||||
for (name, mod_id) in module_id.children(&self.module_tree) {
|
||||
let def_loc = DefLoc::Module {
|
||||
id: mod_id,
|
||||
source_root: self.source_root,
|
||||
for (name, module_id) in module_id.children(&self.module_tree) {
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Module,
|
||||
source_root_id: self.source_root,
|
||||
module_id,
|
||||
source_item_id: module_id.source(&self.module_tree).0,
|
||||
};
|
||||
let def_id = def_loc.id(self.db);
|
||||
let resolution = Resolution {
|
||||
@@ -316,7 +321,11 @@ fn resolve_import(&mut self, module_id: ModuleId, import: &Import) {
|
||||
|
||||
if !is_last {
|
||||
curr = match def_id.loc(self.db) {
|
||||
DefLoc::Module { id, .. } => id,
|
||||
DefLoc {
|
||||
kind: DefKind::Module,
|
||||
module_id,
|
||||
..
|
||||
} => module_id,
|
||||
_ => return,
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user