diff options
Diffstat (limited to 'crates/ra_hir')
| -rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 26 | ||||
| -rw-r--r-- | crates/ra_hir/src/code_model_impl/krate.rs | 8 | ||||
| -rw-r--r-- | crates/ra_hir/src/code_model_impl/module.rs | 49 | ||||
| -rw-r--r-- | crates/ra_hir/src/ids.rs | 2 | ||||
| -rw-r--r-- | crates/ra_hir/src/impl_block.rs | 2 | ||||
| -rw-r--r-- | crates/ra_hir/src/nameres.rs | 4 | ||||
| -rw-r--r-- | crates/ra_hir/src/ty/method_resolution.rs | 4 |
7 files changed, 44 insertions, 51 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 5db53a34f..db270b871 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
| @@ -33,10 +33,10 @@ impl Crate { | |||
| 33 | pub fn crate_id(&self) -> CrateId { | 33 | pub fn crate_id(&self) -> CrateId { |
| 34 | self.crate_id | 34 | self.crate_id |
| 35 | } | 35 | } |
| 36 | pub fn dependencies(&self, db: &impl HirDatabase) -> Cancelable<Vec<CrateDependency>> { | 36 | pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<CrateDependency> { |
| 37 | Ok(self.dependencies_impl(db)) | 37 | self.dependencies_impl(db) |
| 38 | } | 38 | } |
| 39 | pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | 39 | pub fn root_module(&self, db: &impl HirDatabase) -> Option<Module> { |
| 40 | self.root_module_impl(db) | 40 | self.root_module_impl(db) |
| 41 | } | 41 | } |
| 42 | } | 42 | } |
| @@ -78,12 +78,12 @@ pub enum Problem { | |||
| 78 | 78 | ||
| 79 | impl Module { | 79 | impl Module { |
| 80 | /// Name of this module. | 80 | /// Name of this module. |
| 81 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { | 81 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { |
| 82 | self.name_impl(db) | 82 | self.name_impl(db) |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 85 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
| 86 | pub fn definition_source(&self, db: &impl HirDatabase) -> Cancelable<(FileId, ModuleSource)> { | 86 | pub fn definition_source(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) { |
| 87 | self.definition_source_impl(db) | 87 | self.definition_source_impl(db) |
| 88 | } | 88 | } |
| 89 | 89 | ||
| @@ -92,19 +92,19 @@ impl Module { | |||
| 92 | pub fn declaration_source( | 92 | pub fn declaration_source( |
| 93 | &self, | 93 | &self, |
| 94 | db: &impl HirDatabase, | 94 | db: &impl HirDatabase, |
| 95 | ) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> { | 95 | ) -> Option<(FileId, TreeArc<ast::Module>)> { |
| 96 | self.declaration_source_impl(db) | 96 | self.declaration_source_impl(db) |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | /// Returns the crate this module is part of. | 99 | /// Returns the crate this module is part of. |
| 100 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 100 | pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> { |
| 101 | self.krate_impl(db) | 101 | self.krate_impl(db) |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | /// Topmost parent of this module. Every module has a `crate_root`, but some | 104 | /// Topmost parent of this module. Every module has a `crate_root`, but some |
| 105 | /// might be missing `krate`. This can happen if a module's file is not included | 105 | /// might be missing `krate`. This can happen if a module's file is not included |
| 106 | /// in the module tree of any target in Cargo.toml. | 106 | /// in the module tree of any target in Cargo.toml. |
| 107 | pub fn crate_root(&self, db: &impl HirDatabase) -> Cancelable<Module> { | 107 | pub fn crate_root(&self, db: &impl HirDatabase) -> Module { |
| 108 | self.crate_root_impl(db) | 108 | self.crate_root_impl(db) |
| 109 | } | 109 | } |
| 110 | 110 | ||
| @@ -114,23 +114,23 @@ impl Module { | |||
| 114 | } | 114 | } |
| 115 | 115 | ||
| 116 | /// Iterates over all child modules. | 116 | /// Iterates over all child modules. |
| 117 | pub fn children(&self, db: &impl HirDatabase) -> Cancelable<impl Iterator<Item = Module>> { | 117 | pub fn children(&self, db: &impl HirDatabase) -> impl Iterator<Item = Module> { |
| 118 | self.children_impl(db) | 118 | self.children_impl(db) |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | /// Finds a parent module. | 121 | /// Finds a parent module. |
| 122 | pub fn parent(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | 122 | pub fn parent(&self, db: &impl HirDatabase) -> Option<Module> { |
| 123 | self.parent_impl(db) | 123 | self.parent_impl(db) |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | pub fn path_to_root(&self, db: &impl HirDatabase) -> Cancelable<Vec<Module>> { | 126 | pub fn path_to_root(&self, db: &impl HirDatabase) -> Vec<Module> { |
| 127 | let mut res = vec![self.clone()]; | 127 | let mut res = vec![self.clone()]; |
| 128 | let mut curr = self.clone(); | 128 | let mut curr = self.clone(); |
| 129 | while let Some(next) = curr.parent(db)? { | 129 | while let Some(next) = curr.parent(db) { |
| 130 | res.push(next.clone()); | 130 | res.push(next.clone()); |
| 131 | curr = next | 131 | curr = next |
| 132 | } | 132 | } |
| 133 | Ok(res) | 133 | res |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 136 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs index 712c6c86a..8c6e34873 100644 --- a/crates/ra_hir/src/code_model_impl/krate.rs +++ b/crates/ra_hir/src/code_model_impl/krate.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use ra_db::{CrateId, Cancelable}; | 1 | use ra_db::CrateId; |
| 2 | 2 | ||
| 3 | use crate::{ | 3 | use crate::{ |
| 4 | HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, SourceItemId, | 4 | HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, SourceItemId, |
| @@ -20,7 +20,7 @@ impl Crate { | |||
| 20 | }) | 20 | }) |
| 21 | .collect() | 21 | .collect() |
| 22 | } | 22 | } |
| 23 | pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | 23 | pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Option<Module> { |
| 24 | let crate_graph = db.crate_graph(); | 24 | let crate_graph = db.crate_graph(); |
| 25 | let file_id = crate_graph.crate_root(self.crate_id); | 25 | let file_id = crate_graph.crate_root(self.crate_id); |
| 26 | let source_root_id = db.file_source_root(file_id); | 26 | let source_root_id = db.file_source_root(file_id); |
| @@ -31,7 +31,7 @@ impl Crate { | |||
| 31 | file_id, | 31 | file_id, |
| 32 | item_id: None, | 32 | item_id: None, |
| 33 | }; | 33 | }; |
| 34 | let module_id = ctry!(module_tree.find_module_by_source(source)); | 34 | let module_id = module_tree.find_module_by_source(source)?; |
| 35 | 35 | ||
| 36 | let def_loc = DefLoc { | 36 | let def_loc = DefLoc { |
| 37 | kind: DefKind::Module, | 37 | kind: DefKind::Module, |
| @@ -42,6 +42,6 @@ impl Crate { | |||
| 42 | let def_id = def_loc.id(db); | 42 | let def_id = def_loc.id(db); |
| 43 | 43 | ||
| 44 | let module = Module::new(def_id); | 44 | let module = Module::new(def_id); |
| 45 | Ok(Some(module)) | 45 | Some(module) |
| 46 | } | 46 | } |
| 47 | } | 47 | } |
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 67808d282..8668d6c8a 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs | |||
| @@ -30,17 +30,14 @@ impl Module { | |||
| 30 | Module::new(def_id) | 30 | Module::new(def_id) |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { | 33 | pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> { |
| 34 | let loc = self.def_id.loc(db); | 34 | let loc = self.def_id.loc(db); |
| 35 | let module_tree = db.module_tree(loc.source_root_id); | 35 | let module_tree = db.module_tree(loc.source_root_id); |
| 36 | let link = ctry!(loc.module_id.parent_link(&module_tree)); | 36 | let link = loc.module_id.parent_link(&module_tree)?; |
| 37 | Ok(Some(link.name(&module_tree).clone())) | 37 | Some(link.name(&module_tree).clone()) |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | pub fn definition_source_impl( | 40 | pub fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) { |
| 41 | &self, | ||
| 42 | db: &impl HirDatabase, | ||
| 43 | ) -> Cancelable<(FileId, ModuleSource)> { | ||
| 44 | let loc = self.def_id.loc(db); | 41 | let loc = self.def_id.loc(db); |
| 45 | let file_id = loc.source_item_id.file_id.as_original_file(); | 42 | let file_id = loc.source_item_id.file_id.as_original_file(); |
| 46 | let syntax_node = db.file_item(loc.source_item_id); | 43 | let syntax_node = db.file_item(loc.source_item_id); |
| @@ -50,40 +47,40 @@ impl Module { | |||
| 50 | let module = ast::Module::cast(&syntax_node).unwrap(); | 47 | let module = ast::Module::cast(&syntax_node).unwrap(); |
| 51 | ModuleSource::Module(module.to_owned()) | 48 | ModuleSource::Module(module.to_owned()) |
| 52 | }; | 49 | }; |
| 53 | Ok((file_id, module_source)) | 50 | (file_id, module_source) |
| 54 | } | 51 | } |
| 55 | 52 | ||
| 56 | pub fn declaration_source_impl( | 53 | pub fn declaration_source_impl( |
| 57 | &self, | 54 | &self, |
| 58 | db: &impl HirDatabase, | 55 | db: &impl HirDatabase, |
| 59 | ) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> { | 56 | ) -> Option<(FileId, TreeArc<ast::Module>)> { |
| 60 | let loc = self.def_id.loc(db); | 57 | let loc = self.def_id.loc(db); |
| 61 | let module_tree = db.module_tree(loc.source_root_id); | 58 | let module_tree = db.module_tree(loc.source_root_id); |
| 62 | let link = ctry!(loc.module_id.parent_link(&module_tree)); | 59 | let link = loc.module_id.parent_link(&module_tree)?; |
| 63 | let file_id = link | 60 | let file_id = link |
| 64 | .owner(&module_tree) | 61 | .owner(&module_tree) |
| 65 | .source(&module_tree) | 62 | .source(&module_tree) |
| 66 | .file_id | 63 | .file_id |
| 67 | .as_original_file(); | 64 | .as_original_file(); |
| 68 | let src = link.source(&module_tree, db); | 65 | let src = link.source(&module_tree, db); |
| 69 | Ok(Some((file_id, src))) | 66 | Some((file_id, src)) |
| 70 | } | 67 | } |
| 71 | 68 | ||
| 72 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 69 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option<Crate> { |
| 73 | let root = self.crate_root(db)?; | 70 | let root = self.crate_root(db); |
| 74 | let loc = root.def_id.loc(db); | 71 | let loc = root.def_id.loc(db); |
| 75 | let file_id = loc.source_item_id.file_id.as_original_file(); | 72 | let file_id = loc.source_item_id.file_id.as_original_file(); |
| 76 | 73 | ||
| 77 | let crate_graph = db.crate_graph(); | 74 | let crate_graph = db.crate_graph(); |
| 78 | let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id)); | 75 | let crate_id = crate_graph.crate_id_for_crate_root(file_id)?; |
| 79 | Ok(Some(Crate::new(crate_id))) | 76 | Some(Crate::new(crate_id)) |
| 80 | } | 77 | } |
| 81 | 78 | ||
| 82 | pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> { | 79 | pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module { |
| 83 | let loc = self.def_id.loc(db); | 80 | let loc = self.def_id.loc(db); |
| 84 | let module_tree = db.module_tree(loc.source_root_id); | 81 | let module_tree = db.module_tree(loc.source_root_id); |
| 85 | let module_id = loc.module_id.crate_root(&module_tree); | 82 | let module_id = loc.module_id.crate_root(&module_tree); |
| 86 | Ok(Module::from_module_id(db, loc.source_root_id, module_id)) | 83 | Module::from_module_id(db, loc.source_root_id, module_id) |
| 87 | } | 84 | } |
| 88 | 85 | ||
| 89 | /// Finds a child module with the specified name. | 86 | /// Finds a child module with the specified name. |
| @@ -95,7 +92,7 @@ impl Module { | |||
| 95 | } | 92 | } |
| 96 | 93 | ||
| 97 | /// Iterates over all child modules. | 94 | /// Iterates over all child modules. |
| 98 | pub fn children_impl(&self, db: &impl HirDatabase) -> Cancelable<impl Iterator<Item = Module>> { | 95 | pub fn children_impl(&self, db: &impl HirDatabase) -> impl Iterator<Item = Module> { |
| 99 | // FIXME this should be implementable without collecting into a vec, but | 96 | // FIXME this should be implementable without collecting into a vec, but |
| 100 | // it's kind of hard since the iterator needs to keep a reference to the | 97 | // it's kind of hard since the iterator needs to keep a reference to the |
| 101 | // module tree. | 98 | // module tree. |
| @@ -106,18 +103,14 @@ impl Module { | |||
| 106 | .children(&module_tree) | 103 | .children(&module_tree) |
| 107 | .map(|(_, module_id)| Module::from_module_id(db, loc.source_root_id, module_id)) | 104 | .map(|(_, module_id)| Module::from_module_id(db, loc.source_root_id, module_id)) |
| 108 | .collect::<Vec<_>>(); | 105 | .collect::<Vec<_>>(); |
| 109 | Ok(children.into_iter()) | 106 | children.into_iter() |
| 110 | } | 107 | } |
| 111 | 108 | ||
| 112 | pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | 109 | pub fn parent_impl(&self, db: &impl HirDatabase) -> Option<Module> { |
| 113 | let loc = self.def_id.loc(db); | 110 | let loc = self.def_id.loc(db); |
| 114 | let module_tree = db.module_tree(loc.source_root_id); | 111 | let module_tree = db.module_tree(loc.source_root_id); |
| 115 | let parent_id = ctry!(loc.module_id.parent(&module_tree)); | 112 | let parent_id = loc.module_id.parent(&module_tree)?; |
| 116 | Ok(Some(Module::from_module_id( | 113 | Some(Module::from_module_id(db, loc.source_root_id, parent_id)) |
| 117 | db, | ||
| 118 | loc.source_root_id, | ||
| 119 | parent_id, | ||
| 120 | ))) | ||
| 121 | } | 114 | } |
| 122 | 115 | ||
| 123 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 116 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
| @@ -135,10 +128,10 @@ impl Module { | |||
| 135 | ) -> Cancelable<PerNs<DefId>> { | 128 | ) -> Cancelable<PerNs<DefId>> { |
| 136 | let mut curr_per_ns = PerNs::types( | 129 | let mut curr_per_ns = PerNs::types( |
| 137 | match path.kind { | 130 | match path.kind { |
| 138 | PathKind::Crate => self.crate_root(db)?, | 131 | PathKind::Crate => self.crate_root(db), |
| 139 | PathKind::Self_ | PathKind::Plain => self.clone(), | 132 | PathKind::Self_ | PathKind::Plain => self.clone(), |
| 140 | PathKind::Super => { | 133 | PathKind::Super => { |
| 141 | if let Some(p) = self.parent(db)? { | 134 | if let Some(p) = self.parent(db) { |
| 142 | p | 135 | p |
| 143 | } else { | 136 | } else { |
| 144 | return Ok(PerNs::none()); | 137 | return Ok(PerNs::none()); |
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index d7cc9b4ca..7b572061a 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs | |||
| @@ -217,7 +217,7 @@ impl DefId { | |||
| 217 | 217 | ||
| 218 | /// Returns the containing crate. | 218 | /// Returns the containing crate. |
| 219 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 219 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { |
| 220 | Ok(self.module(db)?.krate(db)?) | 220 | Ok(self.module(db)?.krate(db)) |
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | /// Returns the containing impl block, if this is an impl item. | 223 | /// Returns the containing impl block, if this is an impl item. |
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index c9a9fb99f..ce9087b49 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
| @@ -167,7 +167,7 @@ impl ModuleImplBlocks { | |||
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | fn collect(&mut self, db: &impl HirDatabase, module: Module) -> Cancelable<()> { | 169 | fn collect(&mut self, db: &impl HirDatabase, module: Module) -> Cancelable<()> { |
| 170 | let (file_id, module_source) = module.definition_source(db)?; | 170 | let (file_id, module_source) = module.definition_source(db); |
| 171 | let node = match &module_source { | 171 | let node = match &module_source { |
| 172 | ModuleSource::SourceFile(node) => node.syntax(), | 172 | ModuleSource::SourceFile(node) => node.syntax(), |
| 173 | ModuleSource::Module(node) => node | 173 | ModuleSource::Module(node) => node |
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index edb3b1e64..e51cbe786 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs | |||
| @@ -353,8 +353,8 @@ where | |||
| 353 | if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file()) | 353 | if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file()) |
| 354 | { | 354 | { |
| 355 | let krate = Crate::new(crate_id); | 355 | let krate = Crate::new(crate_id); |
| 356 | for dep in krate.dependencies(self.db)? { | 356 | for dep in krate.dependencies(self.db) { |
| 357 | if let Some(module) = dep.krate.root_module(self.db)? { | 357 | if let Some(module) = dep.krate.root_module(self.db) { |
| 358 | let def_id = module.def_id; | 358 | let def_id = module.def_id; |
| 359 | self.add_module_item( | 359 | self.add_module_item( |
| 360 | &mut module_items, | 360 | &mut module_items, |
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 7c3839388..c7fbcfd06 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs | |||
| @@ -75,7 +75,7 @@ impl CrateImplBlocks { | |||
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | for child in module.children(db)? { | 78 | for child in module.children(db) { |
| 79 | self.collect_recursive(db, child)?; | 79 | self.collect_recursive(db, child)?; |
| 80 | } | 80 | } |
| 81 | 81 | ||
| @@ -93,7 +93,7 @@ impl CrateImplBlocks { | |||
| 93 | source_root_id, | 93 | source_root_id, |
| 94 | impls: FxHashMap::default(), | 94 | impls: FxHashMap::default(), |
| 95 | }; | 95 | }; |
| 96 | if let Some(module) = krate.root_module(db)? { | 96 | if let Some(module) = krate.root_module(db) { |
| 97 | crate_impl_blocks.collect_recursive(db, module)?; | 97 | crate_impl_blocks.collect_recursive(db, module)?; |
| 98 | } | 98 | } |
| 99 | Ok(Arc::new(crate_impl_blocks)) | 99 | Ok(Arc::new(crate_impl_blocks)) |
