diff options
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 797f90d50..31390bb7f 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -13,7 +13,9 @@ use hir_def::{ | |||
13 | resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, | 13 | resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, |
14 | DefWithBodyId, | 14 | DefWithBodyId, |
15 | }; | 15 | }; |
16 | use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; | 16 | use hir_expand::{ |
17 | name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind, Source, | ||
18 | }; | ||
17 | use ra_syntax::{ | 19 | use ra_syntax::{ |
18 | ast::{self, AstNode}, | 20 | ast::{self, AstNode}, |
19 | match_ast, AstPtr, | 21 | match_ast, AstPtr, |
@@ -24,11 +26,9 @@ use ra_syntax::{ | |||
24 | use crate::{ | 26 | use crate::{ |
25 | db::HirDatabase, | 27 | db::HirDatabase, |
26 | expr::{BodySourceMap, ExprScopes, ScopeId}, | 28 | expr::{BodySourceMap, ExprScopes, ScopeId}, |
27 | ids::LocationCtx, | ||
28 | ty::method_resolution::{self, implements_trait}, | 29 | ty::method_resolution::{self, implements_trait}, |
29 | Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, | 30 | Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, |
30 | GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, ScopeDef, Static, | 31 | GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, TypeAlias, |
31 | Struct, Trait, Ty, TypeAlias, | ||
32 | }; | 32 | }; |
33 | 33 | ||
34 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { | 34 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { |
@@ -67,16 +67,12 @@ fn def_with_body_from_child_node( | |||
67 | db: &impl HirDatabase, | 67 | db: &impl HirDatabase, |
68 | child: Source<&SyntaxNode>, | 68 | child: Source<&SyntaxNode>, |
69 | ) -> Option<DefWithBody> { | 69 | ) -> Option<DefWithBody> { |
70 | let module_source = crate::ModuleSource::from_child_node(db, child); | ||
71 | let module = Module::from_definition(db, Source::new(child.file_id, module_source))?; | ||
72 | let ctx = LocationCtx::new(db, module.id, child.file_id); | ||
73 | |||
74 | child.value.ancestors().find_map(|node| { | 70 | child.value.ancestors().find_map(|node| { |
75 | match_ast! { | 71 | match_ast! { |
76 | match node { | 72 | match node { |
77 | ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); }, | 73 | ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); }, |
78 | ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); }, | 74 | ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); }, |
79 | ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) }, | 75 | ast::StaticDef(def) => { return Static::from_source(db, child.with_value(def)).map(DefWithBody::from); }, |
80 | _ => { None }, | 76 | _ => { None }, |
81 | } | 77 | } |
82 | } | 78 | } |
@@ -158,8 +154,8 @@ impl SourceAnalyzer { | |||
158 | ) -> SourceAnalyzer { | 154 | ) -> SourceAnalyzer { |
159 | let def_with_body = def_with_body_from_child_node(db, node); | 155 | let def_with_body = def_with_body_from_child_node(db, node); |
160 | if let Some(def) = def_with_body { | 156 | if let Some(def) = def_with_body { |
161 | let source_map = def.body_source_map(db); | 157 | let (_body, source_map) = db.body_with_source_map(def.into()); |
162 | let scopes = def.expr_scopes(db); | 158 | let scopes = db.expr_scopes(def.into()); |
163 | let scope = match offset { | 159 | let scope = match offset { |
164 | None => scope_for(&scopes, &source_map, node), | 160 | None => scope_for(&scopes, &source_map, node), |
165 | Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), | 161 | Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), |
@@ -169,7 +165,7 @@ impl SourceAnalyzer { | |||
169 | resolver, | 165 | resolver, |
170 | body_owner: Some(def), | 166 | body_owner: Some(def), |
171 | body_source_map: Some(source_map), | 167 | body_source_map: Some(source_map), |
172 | infer: Some(def.infer(db)), | 168 | infer: Some(db.infer(def)), |
173 | scopes: Some(scopes), | 169 | scopes: Some(scopes), |
174 | file_id: node.file_id, | 170 | file_id: node.file_id, |
175 | } | 171 | } |
@@ -219,6 +215,11 @@ impl SourceAnalyzer { | |||
219 | self.infer.as_ref()?.field_resolution(expr_id) | 215 | self.infer.as_ref()?.field_resolution(expr_id) |
220 | } | 216 | } |
221 | 217 | ||
218 | pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { | ||
219 | let expr_id = self.expr_id(&field.expr()?)?; | ||
220 | self.infer.as_ref()?.record_field_resolution(expr_id) | ||
221 | } | ||
222 | |||
222 | pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<crate::VariantDef> { | 223 | pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<crate::VariantDef> { |
223 | let expr_id = self.expr_id(&record_lit.clone().into())?; | 224 | let expr_id = self.expr_id(&record_lit.clone().into())?; |
224 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id) | 225 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id) |
@@ -544,7 +545,7 @@ fn adjust( | |||
544 | } | 545 | } |
545 | 546 | ||
546 | /// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to. | 547 | /// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to. |
547 | /// FIXME: Not completed | 548 | /// FIXME: Not completed |
548 | fn to_macro_file_kind(macro_call: &ast::MacroCall) -> MacroFileKind { | 549 | fn to_macro_file_kind(macro_call: &ast::MacroCall) -> MacroFileKind { |
549 | let syn = macro_call.syntax(); | 550 | let syn = macro_call.syntax(); |
550 | let parent = match syn.parent() { | 551 | let parent = match syn.parent() { |