diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/module.rs | 30 | ||||
-rw-r--r-- | crates/ra_hir/src/ids.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/method_resolution.rs | 2 |
4 files changed, 22 insertions, 26 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index aed0ea958..81638e384 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -97,14 +97,14 @@ impl Module { | |||
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/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 127dcc570..8668d6c8a 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs | |||
@@ -66,21 +66,21 @@ impl Module { | |||
66 | Some((file_id, src)) | 66 | Some((file_id, src)) |
67 | } | 67 | } |
68 | 68 | ||
69 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 69 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option<Crate> { |
70 | let root = self.crate_root(db)?; | 70 | let root = self.crate_root(db); |
71 | let loc = root.def_id.loc(db); | 71 | let loc = root.def_id.loc(db); |
72 | 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(); |
73 | 73 | ||
74 | let crate_graph = db.crate_graph(); | 74 | let crate_graph = db.crate_graph(); |
75 | 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)?; |
76 | Ok(Some(Crate::new(crate_id))) | 76 | Some(Crate::new(crate_id)) |
77 | } | 77 | } |
78 | 78 | ||
79 | pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> { | 79 | pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module { |
80 | let loc = self.def_id.loc(db); | 80 | let loc = self.def_id.loc(db); |
81 | let module_tree = db.module_tree(loc.source_root_id); | 81 | let module_tree = db.module_tree(loc.source_root_id); |
82 | let module_id = loc.module_id.crate_root(&module_tree); | 82 | let module_id = loc.module_id.crate_root(&module_tree); |
83 | Ok(Module::from_module_id(db, loc.source_root_id, module_id)) | 83 | Module::from_module_id(db, loc.source_root_id, module_id) |
84 | } | 84 | } |
85 | 85 | ||
86 | /// Finds a child module with the specified name. | 86 | /// Finds a child module with the specified name. |
@@ -92,7 +92,7 @@ impl Module { | |||
92 | } | 92 | } |
93 | 93 | ||
94 | /// Iterates over all child modules. | 94 | /// Iterates over all child modules. |
95 | 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> { |
96 | // FIXME this should be implementable without collecting into a vec, but | 96 | // FIXME this should be implementable without collecting into a vec, but |
97 | // 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 |
98 | // module tree. | 98 | // module tree. |
@@ -103,18 +103,14 @@ impl Module { | |||
103 | .children(&module_tree) | 103 | .children(&module_tree) |
104 | .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)) |
105 | .collect::<Vec<_>>(); | 105 | .collect::<Vec<_>>(); |
106 | Ok(children.into_iter()) | 106 | children.into_iter() |
107 | } | 107 | } |
108 | 108 | ||
109 | pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | 109 | pub fn parent_impl(&self, db: &impl HirDatabase) -> Option<Module> { |
110 | let loc = self.def_id.loc(db); | 110 | let loc = self.def_id.loc(db); |
111 | let module_tree = db.module_tree(loc.source_root_id); | 111 | let module_tree = db.module_tree(loc.source_root_id); |
112 | let parent_id = ctry!(loc.module_id.parent(&module_tree)); | 112 | let parent_id = loc.module_id.parent(&module_tree)?; |
113 | Ok(Some(Module::from_module_id( | 113 | Some(Module::from_module_id(db, loc.source_root_id, parent_id)) |
114 | db, | ||
115 | loc.source_root_id, | ||
116 | parent_id, | ||
117 | ))) | ||
118 | } | 114 | } |
119 | 115 | ||
120 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 116 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
@@ -132,10 +128,10 @@ impl Module { | |||
132 | ) -> Cancelable<PerNs<DefId>> { | 128 | ) -> Cancelable<PerNs<DefId>> { |
133 | let mut curr_per_ns = PerNs::types( | 129 | let mut curr_per_ns = PerNs::types( |
134 | match path.kind { | 130 | match path.kind { |
135 | PathKind::Crate => self.crate_root(db)?, | 131 | PathKind::Crate => self.crate_root(db), |
136 | PathKind::Self_ | PathKind::Plain => self.clone(), | 132 | PathKind::Self_ | PathKind::Plain => self.clone(), |
137 | PathKind::Super => { | 133 | PathKind::Super => { |
138 | if let Some(p) = self.parent(db)? { | 134 | if let Some(p) = self.parent(db) { |
139 | p | 135 | p |
140 | } else { | 136 | } else { |
141 | 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/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 7c3839388..f0da2ee2b 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 | ||