diff options
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 071c1bb18..e7bc4df97 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -37,7 +37,7 @@ pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option<Mod | |||
37 | pub fn module_from_declaration( | 37 | pub fn module_from_declaration( |
38 | db: &impl HirDatabase, | 38 | db: &impl HirDatabase, |
39 | file_id: FileId, | 39 | file_id: FileId, |
40 | decl: &ast::Module, | 40 | decl: ast::Module, |
41 | ) -> Option<Module> { | 41 | ) -> Option<Module> { |
42 | let parent_module = module_from_file_id(db, file_id); | 42 | let parent_module = module_from_file_id(db, file_id); |
43 | let child_name = decl.name(); | 43 | let child_name = decl.name(); |
@@ -50,8 +50,8 @@ pub fn module_from_declaration( | |||
50 | /// Locates the module by position in the source code. | 50 | /// Locates the module by position in the source code. |
51 | pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> { | 51 | pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> { |
52 | let parse = db.parse(position.file_id); | 52 | let parse = db.parse(position.file_id); |
53 | match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) { | 53 | match &find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) { |
54 | Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m), | 54 | Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m.clone()), |
55 | _ => module_from_file_id(db, position.file_id), | 55 | _ => module_from_file_id(db, position.file_id), |
56 | } | 56 | } |
57 | } | 57 | } |
@@ -59,12 +59,12 @@ pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Op | |||
59 | fn module_from_inline( | 59 | fn module_from_inline( |
60 | db: &impl HirDatabase, | 60 | db: &impl HirDatabase, |
61 | file_id: FileId, | 61 | file_id: FileId, |
62 | module: &ast::Module, | 62 | module: ast::Module, |
63 | ) -> Option<Module> { | 63 | ) -> Option<Module> { |
64 | assert!(!module.has_semi()); | 64 | assert!(!module.has_semi()); |
65 | let file_id = file_id.into(); | 65 | let file_id = file_id.into(); |
66 | let ast_id_map = db.ast_id_map(file_id); | 66 | let ast_id_map = db.ast_id_map(file_id); |
67 | let item_id = ast_id_map.ast_id(module).with_file_id(file_id); | 67 | let item_id = ast_id_map.ast_id(&module).with_file_id(file_id); |
68 | module_from_source(db, file_id, Some(item_id)) | 68 | module_from_source(db, file_id, Some(item_id)) |
69 | } | 69 | } |
70 | 70 | ||
@@ -127,16 +127,16 @@ fn try_get_resolver_for_node( | |||
127 | file_id: FileId, | 127 | file_id: FileId, |
128 | node: &SyntaxNode, | 128 | node: &SyntaxNode, |
129 | ) -> Option<Resolver> { | 129 | ) -> Option<Resolver> { |
130 | if let Some(module) = ast::Module::cast(node) { | 130 | if let Some(module) = ast::Module::cast(node.clone()) { |
131 | Some(module_from_declaration(db, file_id, module)?.resolver(db)) | 131 | Some(module_from_declaration(db, file_id, module)?.resolver(db)) |
132 | } else if let Some(_) = ast::SourceFile::cast(node) { | 132 | } else if let Some(_) = ast::SourceFile::cast(node.clone()) { |
133 | Some(module_from_source(db, file_id.into(), None)?.resolver(db)) | 133 | Some(module_from_source(db, file_id.into(), None)?.resolver(db)) |
134 | } else if let Some(s) = ast::StructDef::cast(node) { | 134 | } else if let Some(s) = ast::StructDef::cast(node.clone()) { |
135 | let module = module_from_child_node(db, file_id, s.syntax())?; | 135 | let module = module_from_child_node(db, file_id, s.syntax())?; |
136 | Some(struct_from_module(db, module, s).resolver(db)) | 136 | Some(struct_from_module(db, module, &s).resolver(db)) |
137 | } else if let Some(e) = ast::EnumDef::cast(node) { | 137 | } else if let Some(e) = ast::EnumDef::cast(node.clone()) { |
138 | let module = module_from_child_node(db, file_id, e.syntax())?; | 138 | let module = module_from_child_node(db, file_id, e.syntax())?; |
139 | Some(enum_from_module(db, module, e).resolver(db)) | 139 | Some(enum_from_module(db, module, &e).resolver(db)) |
140 | } else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { | 140 | } else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { |
141 | Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db)) | 141 | Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db)) |
142 | } else { | 142 | } else { |
@@ -153,14 +153,14 @@ fn def_with_body_from_child_node( | |||
153 | let module = module_from_child_node(db, file_id, node)?; | 153 | let module = module_from_child_node(db, file_id, node)?; |
154 | let ctx = LocationCtx::new(db, module, file_id.into()); | 154 | let ctx = LocationCtx::new(db, module, file_id.into()); |
155 | node.ancestors().find_map(|node| { | 155 | node.ancestors().find_map(|node| { |
156 | if let Some(def) = ast::FnDef::cast(node) { | 156 | if let Some(def) = ast::FnDef::cast(node.clone()) { |
157 | return Some(Function { id: ctx.to_def(def) }.into()); | 157 | return Some(Function { id: ctx.to_def(&def) }.into()); |
158 | } | 158 | } |
159 | if let Some(def) = ast::ConstDef::cast(node) { | 159 | if let Some(def) = ast::ConstDef::cast(node.clone()) { |
160 | return Some(Const { id: ctx.to_def(def) }.into()); | 160 | return Some(Const { id: ctx.to_def(&def) }.into()); |
161 | } | 161 | } |
162 | if let Some(def) = ast::StaticDef::cast(node) { | 162 | if let Some(def) = ast::StaticDef::cast(node.clone()) { |
163 | return Some(Static { id: ctx.to_def(def) }.into()); | 163 | return Some(Static { id: ctx.to_def(&def) }.into()); |
164 | } | 164 | } |
165 | None | 165 | None |
166 | }) | 166 | }) |
@@ -237,7 +237,7 @@ impl SourceAnalyzer { | |||
237 | SourceAnalyzer { | 237 | SourceAnalyzer { |
238 | resolver: node | 238 | resolver: node |
239 | .ancestors() | 239 | .ancestors() |
240 | .find_map(|node| try_get_resolver_for_node(db, file_id, node)) | 240 | .find_map(|node| try_get_resolver_for_node(db, file_id, &node)) |
241 | .unwrap_or_default(), | 241 | .unwrap_or_default(), |
242 | body_source_map: None, | 242 | body_source_map: None, |
243 | infer: None, | 243 | infer: None, |
@@ -257,17 +257,17 @@ impl SourceAnalyzer { | |||
257 | } | 257 | } |
258 | 258 | ||
259 | pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { | 259 | pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { |
260 | let expr_id = self.body_source_map.as_ref()?.node_expr(call.into())?; | 260 | let expr_id = self.body_source_map.as_ref()?.node_expr(&call.clone().into())?; |
261 | self.infer.as_ref()?.method_resolution(expr_id) | 261 | self.infer.as_ref()?.method_resolution(expr_id) |
262 | } | 262 | } |
263 | 263 | ||
264 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { | 264 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { |
265 | let expr_id = self.body_source_map.as_ref()?.node_expr(field.into())?; | 265 | let expr_id = self.body_source_map.as_ref()?.node_expr(&field.clone().into())?; |
266 | self.infer.as_ref()?.field_resolution(expr_id) | 266 | self.infer.as_ref()?.field_resolution(expr_id) |
267 | } | 267 | } |
268 | 268 | ||
269 | pub fn resolve_variant(&self, struct_lit: &ast::StructLit) -> Option<crate::VariantDef> { | 269 | pub fn resolve_variant(&self, struct_lit: &ast::StructLit) -> Option<crate::VariantDef> { |
270 | let expr_id = self.body_source_map.as_ref()?.node_expr(struct_lit.into())?; | 270 | let expr_id = self.body_source_map.as_ref()?.node_expr(&struct_lit.clone().into())?; |
271 | self.infer.as_ref()?.variant_resolution(expr_id) | 271 | self.infer.as_ref()?.variant_resolution(expr_id) |
272 | } | 272 | } |
273 | 273 | ||
@@ -290,18 +290,18 @@ impl SourceAnalyzer { | |||
290 | 290 | ||
291 | pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> { | 291 | pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> { |
292 | if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { | 292 | if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { |
293 | let expr_id = self.body_source_map.as_ref()?.node_expr(path_expr.into())?; | 293 | let expr_id = self.body_source_map.as_ref()?.node_expr(&path_expr.into())?; |
294 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { | 294 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { |
295 | return Some(PathResolution::AssocItem(assoc)); | 295 | return Some(PathResolution::AssocItem(assoc)); |
296 | } | 296 | } |
297 | } | 297 | } |
298 | if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { | 298 | if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { |
299 | let pat_id = self.body_source_map.as_ref()?.node_pat(path_pat.into())?; | 299 | let pat_id = self.body_source_map.as_ref()?.node_pat(&path_pat.into())?; |
300 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { | 300 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { |
301 | return Some(PathResolution::AssocItem(assoc)); | 301 | return Some(PathResolution::AssocItem(assoc)); |
302 | } | 302 | } |
303 | } | 303 | } |
304 | let hir_path = crate::Path::from_ast(path)?; | 304 | let hir_path = crate::Path::from_ast(path.clone())?; |
305 | let res = self.resolver.resolve_path_without_assoc_items(db, &hir_path); | 305 | let res = self.resolver.resolve_path_without_assoc_items(db, &hir_path); |
306 | let res = res.clone().take_types().or_else(|| res.take_values())?; | 306 | let res = res.clone().take_types().or_else(|| res.take_values())?; |
307 | let res = match res { | 307 | let res = match res { |
@@ -343,12 +343,12 @@ impl SourceAnalyzer { | |||
343 | // FIXME: at least, this should work with any DefWithBody, but ideally | 343 | // FIXME: at least, this should work with any DefWithBody, but ideally |
344 | // this should be hir-based altogether | 344 | // this should be hir-based altogether |
345 | let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); | 345 | let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); |
346 | let ptr = Either::A(AstPtr::new(pat.into())); | 346 | let ptr = Either::A(AstPtr::new(&ast::Pat::from(pat.clone()))); |
347 | fn_def | 347 | fn_def |
348 | .syntax() | 348 | .syntax() |
349 | .descendants() | 349 | .descendants() |
350 | .filter_map(ast::NameRef::cast) | 350 | .filter_map(ast::NameRef::cast) |
351 | .filter(|name_ref| match self.resolve_local_name(*name_ref) { | 351 | .filter(|name_ref| match self.resolve_local_name(&name_ref) { |
352 | None => false, | 352 | None => false, |
353 | Some(entry) => entry.ptr() == ptr, | 353 | Some(entry) => entry.ptr() == ptr, |
354 | }) | 354 | }) |
@@ -411,7 +411,7 @@ fn scope_for( | |||
411 | node: &SyntaxNode, | 411 | node: &SyntaxNode, |
412 | ) -> Option<ScopeId> { | 412 | ) -> Option<ScopeId> { |
413 | node.ancestors() | 413 | node.ancestors() |
414 | .map(SyntaxNodePtr::new) | 414 | .map(|it| SyntaxNodePtr::new(&it)) |
415 | .filter_map(|ptr| source_map.syntax_expr(ptr)) | 415 | .filter_map(|ptr| source_map.syntax_expr(ptr)) |
416 | .find_map(|it| scopes.scope_for(it)) | 416 | .find_map(|it| scopes.scope_for(it)) |
417 | } | 417 | } |