diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 983ce99cb26..f50b922af21 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 580c737c300..e7a49f83abe 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs @@ -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) } diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 61a1acfe6f9..6511359d08b 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -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, diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index e4d721601df..bb4457d072e 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 47915580502..0c16ccc243d 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,