From 159525b120121b217f2831869b6ea9a3cf9fe554 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 Dec 2018 13:18:46 +0300 Subject: Check Fileid in SourceFileMap --- crates/ra_hir/src/lib.rs | 20 ++++++++++++++++++-- crates/ra_hir/src/module.rs | 2 +- crates/ra_hir/src/module/nameres.rs | 4 ++-- crates/ra_hir/src/query_definitions.rs | 2 +- crates/ra_hir/src/source_binder.rs | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 983ce99cb..f50b922af 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -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, } 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() diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index 580c737c3..e7a49f83a 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs @@ -280,7 +280,7 @@ impl ModuleSource { ) -> 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) } diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 61a1acfe6..6511359d0 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -168,7 +168,7 @@ impl InputModuleItems { } 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 @@ impl ModuleItem { 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, diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index e4d721601..bb4457d07 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -36,7 +36,7 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { } pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { - 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(); diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 479155805..0c16ccc24 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -75,7 +75,7 @@ pub fn function_from_source( ) -> Cancelable> { 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, -- cgit v1.2.3