From 006a583381ab66f229c4d49531cb50687343eea3 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Thu, 5 Dec 2019 08:33:29 -0500 Subject: Use placeholder instead of `Option` --- crates/ra_hir_def/src/nameres.rs | 22 +++++++++------------- crates/ra_hir_def/src/nameres/collector.rs | 2 +- 2 files changed, 10 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 0a2b32bbd..98bab1bfe 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -104,8 +104,9 @@ impl std::ops::Index for CrateDefMap { #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] pub enum ModuleOrigin { - /// It should not be `None` after collecting definitions. - Root(Option), + CrateRoot { + definition: FileId, + }, /// Note that non-inline modules, by definition, live inside non-macro file. File { declaration: AstId, @@ -118,15 +119,11 @@ pub enum ModuleOrigin { impl Default for ModuleOrigin { fn default() -> Self { - ModuleOrigin::Root(None) + ModuleOrigin::CrateRoot { definition: FileId(0) } } } impl ModuleOrigin { - fn root(file_id: FileId) -> Self { - ModuleOrigin::Root(Some(file_id)) - } - pub(crate) fn not_sure_file(file: Option, declaration: AstId) -> Self { match file { None => ModuleOrigin::Inline { definition: declaration }, @@ -138,14 +135,14 @@ impl ModuleOrigin { match self { ModuleOrigin::File { declaration: module, .. } | ModuleOrigin::Inline { definition: module, .. } => Some(*module), - ModuleOrigin::Root(_) => None, + ModuleOrigin::CrateRoot { .. } => None, } } pub(crate) fn file_id(&self) -> Option { match self { - ModuleOrigin::File { definition: file_id, .. } | ModuleOrigin::Root(Some(file_id)) => { - Some(*file_id) + ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => { + Some(*definition) } _ => None, } @@ -155,12 +152,11 @@ impl ModuleOrigin { /// That is, a file or a `mod foo {}` with items. fn definition_source(&self, db: &impl DefDatabase) -> InFile { match self { - ModuleOrigin::File { definition: file_id, .. } | ModuleOrigin::Root(Some(file_id)) => { - let file_id = *file_id; + ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => { + let file_id = *definition; let sf = db.parse(file_id).tree(); return InFile::new(file_id.into(), ModuleSource::SourceFile(sf)); } - ModuleOrigin::Root(None) => unreachable!(), ModuleOrigin::Inline { definition } => { InFile::new(definition.file_id, ModuleSource::Module(definition.to_node(db))) } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 6f4a3e42e..9d948d4f4 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -131,7 +131,7 @@ where let file_id = crate_graph.crate_root(self.def_map.krate); let raw_items = self.db.raw_items(file_id.into()); let module_id = self.def_map.root; - self.def_map.modules[module_id].origin = ModuleOrigin::root(file_id); + self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; ModCollector { def_collector: &mut *self, module_id, -- cgit v1.2.3