From 6210e82041849bad6129331b9e45ac0bae6fe569 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 6 Jan 2019 16:47:59 +0100 Subject: Use HIR Expr for type inference Now we can reuse the type inference inside a function when typing whitespace etc. :) --- crates/ra_analysis/src/completion/complete_dot.rs | 14 +- crates/ra_analysis/src/hover.rs | 9 +- crates/ra_hir/src/expr.rs | 31 +- crates/ra_hir/src/ty.rs | 544 +++++++++------------ crates/ra_hir/src/ty/tests.rs | 24 +- crates/ra_hir/src/ty/tests/data/0001_basics.txt | 2 +- crates/ra_hir/src/ty/tests/data/0004_struct.txt | 2 +- crates/ra_hir/src/ty/tests/data/0005_refs.txt | 4 +- crates/ra_hir/src/ty/tests/data/0006_backwards.txt | 6 +- .../ra_hir/src/ty/tests/data/0008_boolean_op.txt | 14 +- 10 files changed, 308 insertions(+), 342 deletions(-) diff --git a/crates/ra_analysis/src/completion/complete_dot.rs b/crates/ra_analysis/src/completion/complete_dot.rs index 031d8b98f..ef3f4271b 100644 --- a/crates/ra_analysis/src/completion/complete_dot.rs +++ b/crates/ra_analysis/src/completion/complete_dot.rs @@ -1,3 +1,4 @@ +use ra_db::LocalSyntaxPtr; use ra_syntax::ast::AstNode; use hir::{Ty, Def}; @@ -11,11 +12,14 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca _ => return Ok(()), }; let infer_result = function.infer(ctx.db)?; - let receiver_ty = if let Some(ty) = infer_result.type_of_node(receiver.syntax()) { - ty - } else { - return Ok(()); - }; + let syntax_mapping = function.body_syntax_mapping(ctx.db)?; + let expr = + if let Some(expr) = syntax_mapping.syntax_expr(LocalSyntaxPtr::new(receiver.syntax())) { + expr + } else { + return Ok(()); + }; + let receiver_ty = infer_result[expr].clone(); if !ctx.is_method_call { complete_fields(acc, ctx, receiver_ty)?; } diff --git a/crates/ra_analysis/src/hover.rs b/crates/ra_analysis/src/hover.rs index ba1fb9beb..067e5c12d 100644 --- a/crates/ra_analysis/src/hover.rs +++ b/crates/ra_analysis/src/hover.rs @@ -64,7 +64,14 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable