aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-04-12 22:44:47 +0100
committerAleksey Kladov <[email protected]>2019-04-12 22:44:47 +0100
commit0fd93bc14a2d0ce2edd682d26c18979c13f181c5 (patch)
treee800dc46abe63bad2eaffaf7f3c0f4d8358d0c9a /crates
parent20013de2abb95bc024f55163b1a5044cfb52a873 (diff)
use really correct resolver for expressions
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/add_explicit_type.rs2
-rw-r--r--crates/ra_assists/src/add_missing_impl_members.rs2
-rw-r--r--crates/ra_assists/src/fill_match_arms.rs2
-rw-r--r--crates/ra_assists/src/fill_struct_fields.rs1
-rw-r--r--crates/ra_hir/src/source_binder.rs24
-rw-r--r--crates/ra_ide_api/src/call_info.rs2
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs3
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs2
-rw-r--r--crates/ra_ide_api/src/hover.rs2
9 files changed, 27 insertions, 13 deletions
diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs
index c3674ffdc..cb0ac9885 100644
--- a/crates/ra_assists/src/add_explicit_type.rs
+++ b/crates/ra_assists/src/add_explicit_type.rs
@@ -29,7 +29,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
29 } 29 }
30 // Infer type 30 // Infer type
31 let db = ctx.db; 31 let db = ctx.db;
32 let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax()); 32 let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax(), None);
33 let ty = analyzer.type_of(db, expr)?; 33 let ty = analyzer.type_of(db, expr)?;
34 // Assist not applicable if the type is unknown 34 // Assist not applicable if the type is unknown
35 if is_unknown(&ty) { 35 if is_unknown(&ty) {
diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs
index 104a4d78a..100ebb7b6 100644
--- a/crates/ra_assists/src/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/add_missing_impl_members.rs
@@ -45,7 +45,7 @@ fn add_missing_impl_members_inner(
45 let trait_def = { 45 let trait_def = {
46 let file_id = ctx.frange.file_id; 46 let file_id = ctx.frange.file_id;
47 let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; 47 let position = FilePosition { file_id, offset: impl_node.syntax().range().start() };
48 let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax()); 48 let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None);
49 49
50 resolve_target_trait_def(ctx.db, &analyzer, impl_node)? 50 resolve_target_trait_def(ctx.db, &analyzer, impl_node)?
51 }; 51 };
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs
index 8110b2676..45e327cd4 100644
--- a/crates/ra_assists/src/fill_match_arms.rs
+++ b/crates/ra_assists/src/fill_match_arms.rs
@@ -20,7 +20,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
20 } 20 }
21 21
22 let expr = match_expr.expr()?; 22 let expr = match_expr.expr()?;
23 let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, expr.syntax()); 23 let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, expr.syntax(), None);
24 let match_expr_ty = analyzer.type_of(ctx.db, expr)?; 24 let match_expr_ty = analyzer.type_of(ctx.db, expr)?;
25 let enum_def = match_expr_ty.autoderef(ctx.db).find_map(|ty| match ty.as_adt() { 25 let enum_def = match_expr_ty.autoderef(ctx.db).find_map(|ty| match ty.as_adt() {
26 Some((AdtDef::Enum(e), _)) => Some(e), 26 Some((AdtDef::Enum(e), _)) => Some(e),
diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs
index 81f762e8d..663b4f669 100644
--- a/crates/ra_assists/src/fill_struct_fields.rs
+++ b/crates/ra_assists/src/fill_struct_fields.rs
@@ -55,6 +55,7 @@ where
55 self.ctx.db, 55 self.ctx.db,
56 self.ctx.frange.file_id, 56 self.ctx.frange.file_id,
57 self.struct_lit.syntax(), 57 self.struct_lit.syntax(),
58 None,
58 ); 59 );
59 let struct_lit_ty = analyzer.type_of(self.ctx.db, self.struct_lit.into())?; 60 let struct_lit_ty = analyzer.type_of(self.ctx.db, self.struct_lit.into())?;
60 let struct_def = match struct_lit_ty.as_adt() { 61 let struct_def = match struct_lit_ty.as_adt() {
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 182bc36ff..846394212 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -9,7 +9,7 @@ use std::sync::Arc;
9 9
10use ra_db::{FileId, FilePosition}; 10use ra_db::{FileId, FilePosition};
11use ra_syntax::{ 11use ra_syntax::{
12 SyntaxNode, AstPtr, 12 SyntaxNode, AstPtr, TextUnit,
13 ast::{self, AstNode, NameOwner}, 13 ast::{self, AstNode, NameOwner},
14 algo::find_node_at_offset, 14 algo::find_node_at_offset,
15}; 15};
@@ -196,13 +196,21 @@ pub fn trait_from_module(
196 Trait { id: ctx.to_def(trait_def) } 196 Trait { id: ctx.to_def(trait_def) }
197} 197}
198 198
199fn resolver_for_node(db: &impl HirDatabase, file_id: FileId, node: &SyntaxNode) -> Resolver { 199fn resolver_for_node(
200 db: &impl HirDatabase,
201 file_id: FileId,
202 node: &SyntaxNode,
203 offset: Option<TextUnit>,
204) -> Resolver {
200 node.ancestors() 205 node.ancestors()
201 .find_map(|node| { 206 .find_map(|node| {
202 if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() { 207 if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() {
203 if let Some(func) = function_from_child_node(db, file_id, node) { 208 if let Some(func) = function_from_child_node(db, file_id, node) {
204 let scopes = func.scopes(db); 209 let scopes = func.scopes(db);
205 let scope = scopes.scope_for(&node); 210 let scope = match offset {
211 None => scopes.scope_for(&node),
212 Some(offset) => scopes.scope_for_offset(offset),
213 };
206 Some(expr::resolver_for_scope(func.body(db), db, scope)) 214 Some(expr::resolver_for_scope(func.body(db), db, scope))
207 } else { 215 } else {
208 // FIXME const/static/array length 216 // FIXME const/static/array length
@@ -260,7 +268,12 @@ pub enum PathResolution {
260} 268}
261 269
262impl SourceAnalyzer { 270impl SourceAnalyzer {
263 pub fn new(db: &impl HirDatabase, file_id: FileId, node: &SyntaxNode) -> SourceAnalyzer { 271 pub fn new(
272 db: &impl HirDatabase,
273 file_id: FileId,
274 node: &SyntaxNode,
275 offset: Option<TextUnit>,
276 ) -> SourceAnalyzer {
264 let def_with_body = node.ancestors().find_map(|node| { 277 let def_with_body = node.ancestors().find_map(|node| {
265 if let Some(src) = ast::FnDef::cast(node) { 278 if let Some(src) = ast::FnDef::cast(node) {
266 return function_from_source(db, file_id, src).map(DefWithBody::from); 279 return function_from_source(db, file_id, src).map(DefWithBody::from);
@@ -274,8 +287,7 @@ impl SourceAnalyzer {
274 None 287 None
275 }); 288 });
276 SourceAnalyzer { 289 SourceAnalyzer {
277 //TODO: use scope_for_offset here to get correct scope for completion 290 resolver: resolver_for_node(db, file_id, node, offset),
278 resolver: resolver_for_node(db, file_id, node),
279 body_source_map: def_with_body.map(|it| it.body_source_map(db)), 291 body_source_map: def_with_body.map(|it| it.body_source_map(db)),
280 infer: def_with_body.map(|it| it.infer(db)), 292 infer: def_with_body.map(|it| it.infer(db)),
281 } 293 }
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs
index a8495ab01..4413aec73 100644
--- a/crates/ra_ide_api/src/call_info.rs
+++ b/crates/ra_ide_api/src/call_info.rs
@@ -17,7 +17,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
17 let calling_node = FnCallNode::with_node(syntax, position.offset)?; 17 let calling_node = FnCallNode::with_node(syntax, position.offset)?;
18 let name_ref = calling_node.name_ref()?; 18 let name_ref = calling_node.name_ref()?;
19 19
20 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax()); 20 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
21 let function = match calling_node { 21 let function = match calling_node {
22 FnCallNode::CallExpr(expr) => { 22 FnCallNode::CallExpr(expr) => {
23 //FIXME: apply subst 23 //FIXME: apply subst
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index 86b30e787..359f2cffa 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -48,7 +48,8 @@ impl<'a> CompletionContext<'a> {
48 ) -> Option<CompletionContext<'a>> { 48 ) -> Option<CompletionContext<'a>> {
49 let module = source_binder::module_from_position(db, position); 49 let module = source_binder::module_from_position(db, position);
50 let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?; 50 let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?;
51 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, token.parent()); 51 let analyzer =
52 hir::SourceAnalyzer::new(db, position.file_id, token.parent(), Some(position.offset));
52 let mut ctx = CompletionContext { 53 let mut ctx = CompletionContext {
53 db, 54 db,
54 analyzer, 55 analyzer,
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index abcc682e7..517dffbca 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -47,7 +47,7 @@ pub(crate) fn reference_definition(
47) -> ReferenceResult { 47) -> ReferenceResult {
48 use self::ReferenceResult::*; 48 use self::ReferenceResult::*;
49 49
50 let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax()); 50 let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
51 51
52 // Special cases: 52 // Special cases:
53 53
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index 0cba5a665..397b56786 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -132,7 +132,7 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
132 .ancestors() 132 .ancestors()
133 .take_while(|it| it.range() == leaf_node.range()) 133 .take_while(|it| it.range() == leaf_node.range())
134 .find(|&it| ast::Expr::cast(it).is_some() || ast::Pat::cast(it).is_some())?; 134 .find(|&it| ast::Expr::cast(it).is_some() || ast::Pat::cast(it).is_some())?;
135 let analyzer = hir::SourceAnalyzer::new(db, frange.file_id, node); 135 let analyzer = hir::SourceAnalyzer::new(db, frange.file_id, node, None);
136 let ty = if let Some(ty) = ast::Expr::cast(node).and_then(|e| analyzer.type_of(db, e)) { 136 let ty = if let Some(ty) = ast::Expr::cast(node).and_then(|e| analyzer.type_of(db, e)) {
137 ty 137 ty
138 } else if let Some(ty) = ast::Pat::cast(node).and_then(|p| analyzer.type_of_pat(db, p)) { 138 } else if let Some(ty) = ast::Pat::cast(node).and_then(|p| analyzer.type_of_pat(db, p)) {