Check Fileid in SourceFileMap

This commit is contained in:
Aleksey Kladov 2018-12-09 13:18:46 +03:00
parent 13100da7a2
commit 159525b120
5 changed files with 23 additions and 7 deletions

View File

@ -110,16 +110,32 @@ pub struct SourceItemId {
}
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
#[derive(Debug, PartialEq, Eq, Default)]
#[derive(Debug, PartialEq, Eq)]
pub struct SourceFileItems {
file_id: FileId,
arena: Arena<SyntaxNode>,
}
impl SourceFileItems {
fn new(file_id: FileId) -> SourceFileItems {
SourceFileItems {
file_id,
arena: Arena::default(),
}
}
fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
self.arena.alloc(item)
}
pub fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId {
pub fn id_of(&self, file_id: FileId, item: SyntaxNodeRef) -> SourceFileItemId {
assert_eq!(
self.file_id, file_id,
"SourceFileItems: wrong file, expected {:?}, got {:?}",
self.file_id, file_id
);
self.id_of_unchecked(item)
}
fn id_of_unchecked(&self, item: SyntaxNodeRef) -> SourceFileItemId {
let (id, _item) = self
.arena
.iter()

View File

@ -280,7 +280,7 @@ pub(crate) fn new_inline(
) -> ModuleSource {
assert!(!m.has_semi());
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(m.syntax());
let item_id = file_items.id_of(file_id, m.syntax());
ModuleSource::new(file_id, item_id)
}

View File

@ -168,7 +168,7 @@ fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> O
}
fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
let file_item_id = file_items.id_of(item.syntax());
let file_item_id = file_items.id_of_unchecked(item.syntax());
let start_offset = item.syntax().range().start();
Path::expand_use_item(item, |path, range| {
let kind = match range {
@ -188,7 +188,7 @@ fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Optio
let name = item.name()?.text();
let kind = item.syntax().kind();
let vis = Vis::Other;
let id = file_items.id_of(item.syntax());
let id = file_items.id_of_unchecked(item.syntax());
let res = ModuleItem {
id,
name,

View File

@ -36,7 +36,7 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> {
}
pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> {
let mut res = SourceFileItems::default();
let mut res = SourceFileItems::new(file_id);
let source_file = db.source_file(file_id);
res.alloc(source_file.syntax().owned());
let source_file = source_file.borrowed();

View File

@ -75,7 +75,7 @@ pub fn function_from_source(
) -> Cancelable<Option<Function>> {
let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?);
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(fn_def.syntax());
let item_id = file_items.id_of(file_id, fn_def.syntax());
let source_item_id = SourceItemId { file_id, item_id };
let def_loc = DefLoc {
kind: DefKind::Function,