aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorice1000 <[email protected]>2019-12-05 13:33:29 +0000
committerice1000 <[email protected]>2019-12-05 13:33:29 +0000
commit006a583381ab66f229c4d49531cb50687343eea3 (patch)
tree0471c26b78e6c847f1ac7460202c848a7389e91e
parent7702f690a9592605be71104ec9d0b732af940fcc (diff)
Use placeholder instead of `Option`
-rw-r--r--crates/ra_hir_def/src/nameres.rs22
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs2
2 files changed, 10 insertions, 14 deletions
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<LocalModuleId> for CrateDefMap {
104 104
105#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] 105#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
106pub enum ModuleOrigin { 106pub enum ModuleOrigin {
107 /// It should not be `None` after collecting definitions. 107 CrateRoot {
108 Root(Option<FileId>), 108 definition: FileId,
109 },
109 /// Note that non-inline modules, by definition, live inside non-macro file. 110 /// Note that non-inline modules, by definition, live inside non-macro file.
110 File { 111 File {
111 declaration: AstId<ast::Module>, 112 declaration: AstId<ast::Module>,
@@ -118,15 +119,11 @@ pub enum ModuleOrigin {
118 119
119impl Default for ModuleOrigin { 120impl Default for ModuleOrigin {
120 fn default() -> Self { 121 fn default() -> Self {
121 ModuleOrigin::Root(None) 122 ModuleOrigin::CrateRoot { definition: FileId(0) }
122 } 123 }
123} 124}
124 125
125impl ModuleOrigin { 126impl ModuleOrigin {
126 fn root(file_id: FileId) -> Self {
127 ModuleOrigin::Root(Some(file_id))
128 }
129
130 pub(crate) fn not_sure_file(file: Option<FileId>, declaration: AstId<ast::Module>) -> Self { 127 pub(crate) fn not_sure_file(file: Option<FileId>, declaration: AstId<ast::Module>) -> Self {
131 match file { 128 match file {
132 None => ModuleOrigin::Inline { definition: declaration }, 129 None => ModuleOrigin::Inline { definition: declaration },
@@ -138,14 +135,14 @@ impl ModuleOrigin {
138 match self { 135 match self {
139 ModuleOrigin::File { declaration: module, .. } 136 ModuleOrigin::File { declaration: module, .. }
140 | ModuleOrigin::Inline { definition: module, .. } => Some(*module), 137 | ModuleOrigin::Inline { definition: module, .. } => Some(*module),
141 ModuleOrigin::Root(_) => None, 138 ModuleOrigin::CrateRoot { .. } => None,
142 } 139 }
143 } 140 }
144 141
145 pub(crate) fn file_id(&self) -> Option<FileId> { 142 pub(crate) fn file_id(&self) -> Option<FileId> {
146 match self { 143 match self {
147 ModuleOrigin::File { definition: file_id, .. } | ModuleOrigin::Root(Some(file_id)) => { 144 ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
148 Some(*file_id) 145 Some(*definition)
149 } 146 }
150 _ => None, 147 _ => None,
151 } 148 }
@@ -155,12 +152,11 @@ impl ModuleOrigin {
155 /// That is, a file or a `mod foo {}` with items. 152 /// That is, a file or a `mod foo {}` with items.
156 fn definition_source(&self, db: &impl DefDatabase) -> InFile<ModuleSource> { 153 fn definition_source(&self, db: &impl DefDatabase) -> InFile<ModuleSource> {
157 match self { 154 match self {
158 ModuleOrigin::File { definition: file_id, .. } | ModuleOrigin::Root(Some(file_id)) => { 155 ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
159 let file_id = *file_id; 156 let file_id = *definition;
160 let sf = db.parse(file_id).tree(); 157 let sf = db.parse(file_id).tree();
161 return InFile::new(file_id.into(), ModuleSource::SourceFile(sf)); 158 return InFile::new(file_id.into(), ModuleSource::SourceFile(sf));
162 } 159 }
163 ModuleOrigin::Root(None) => unreachable!(),
164 ModuleOrigin::Inline { definition } => { 160 ModuleOrigin::Inline { definition } => {
165 InFile::new(definition.file_id, ModuleSource::Module(definition.to_node(db))) 161 InFile::new(definition.file_id, ModuleSource::Module(definition.to_node(db)))
166 } 162 }
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
131 let file_id = crate_graph.crate_root(self.def_map.krate); 131 let file_id = crate_graph.crate_root(self.def_map.krate);
132 let raw_items = self.db.raw_items(file_id.into()); 132 let raw_items = self.db.raw_items(file_id.into());
133 let module_id = self.def_map.root; 133 let module_id = self.def_map.root;
134 self.def_map.modules[module_id].origin = ModuleOrigin::root(file_id); 134 self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
135 ModCollector { 135 ModCollector {
136 def_collector: &mut *self, 136 def_collector: &mut *self,
137 module_id, 137 module_id,