From a7858bb7bf0a784d56b2b9ef97785a4fa78f7853 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 25 Aug 2019 19:28:32 +0200 Subject: Report type mismatches in analysis-stats Only the number usually; each one individually when running with -v. --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/expr.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 66a58efed..0f9ff97f1 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -617,7 +617,7 @@ impl Function { self.data(db).name.clone() } - pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc { + pub fn body_source_map(self, db: &impl HirDatabase) -> Arc { db.body_with_source_map(self.into()).1 } diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 7cdc7555c..57225ae91 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -128,27 +128,27 @@ impl Index for Body { } impl BodySourceMap { - pub(crate) fn expr_syntax(&self, expr: ExprId) -> Option { + pub fn expr_syntax(&self, expr: ExprId) -> Option { self.expr_map_back.get(expr).cloned() } - pub(crate) fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option { + pub fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option { self.expr_map.get(&ptr).cloned() } - pub(crate) fn node_expr(&self, node: &ast::Expr) -> Option { + pub fn node_expr(&self, node: &ast::Expr) -> Option { self.expr_map.get(&SyntaxNodePtr::new(node.syntax())).cloned() } - pub(crate) fn pat_syntax(&self, pat: PatId) -> Option { + pub fn pat_syntax(&self, pat: PatId) -> Option { self.pat_map_back.get(pat).cloned() } - pub(crate) fn node_pat(&self, node: &ast::Pat) -> Option { + pub fn node_pat(&self, node: &ast::Pat) -> Option { self.pat_map.get(&Either::A(AstPtr::new(node))).cloned() } - pub(crate) fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr { + pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr { self.field_map[&(expr, field)] } } -- cgit v1.2.3 From f92177cfb5088809892455262841e24cf1ecf5b6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 30 Aug 2019 20:16:28 +0200 Subject: Add an expr_source method analogous to the source methods in the code model ... and use that instead of exposing the source map. --- crates/ra_hir/src/code_model.rs | 51 +++++++++++++++++++++++++++---------- crates/ra_hir/src/code_model/src.rs | 30 +++++++++++++++++++--- crates/ra_hir/src/expr.rs | 12 ++++----- crates/ra_hir/src/lib.rs | 6 ++--- crates/ra_hir/src/source_binder.rs | 6 ++--- crates/ra_hir/src/ty/infer.rs | 4 +-- 6 files changed, 79 insertions(+), 30 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 0f9ff97f1..f7efc1b66 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -510,18 +510,6 @@ pub enum DefWithBody { impl_froms!(DefWithBody: Function, Const, Static); impl DefWithBody { - pub fn infer(self, db: &impl HirDatabase) -> Arc { - db.infer(self) - } - - pub fn body(self, db: &impl HirDatabase) -> Arc { - db.body_hir(self) - } - - pub fn body_source_map(self, db: &impl HirDatabase) -> Arc { - db.body_with_source_map(self).1 - } - /// Builds a resolver for code inside this item. pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { match self { @@ -532,6 +520,43 @@ impl DefWithBody { } } +pub trait HasBody: Copy { + fn infer(self, db: &impl HirDatabase) -> Arc; + fn body(self, db: &impl HirDatabase) -> Arc; + fn body_source_map(self, db: &impl HirDatabase) -> Arc; +} + +impl HasBody for T +where + T: Into + Copy + HasSource, +{ + fn infer(self, db: &impl HirDatabase) -> Arc { + db.infer(self.into()) + } + + fn body(self, db: &impl HirDatabase) -> Arc { + db.body_hir(self.into()) + } + + fn body_source_map(self, db: &impl HirDatabase) -> Arc { + db.body_with_source_map(self.into()).1 + } +} + +impl HasBody for DefWithBody { + fn infer(self, db: &impl HirDatabase) -> Arc { + db.infer(self) + } + + fn body(self, db: &impl HirDatabase) -> Arc { + db.body_hir(self) + } + + fn body_source_map(self, db: &impl HirDatabase) -> Arc { + db.body_with_source_map(self).1 + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Function { pub(crate) id: FunctionId, @@ -617,7 +642,7 @@ impl Function { self.data(db).name.clone() } - pub fn body_source_map(self, db: &impl HirDatabase) -> Arc { + pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc { db.body_with_source_map(self.into()).1 } diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 32bd9c661..e5bae16ab 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -1,9 +1,9 @@ -use ra_syntax::ast; +use ra_syntax::ast::{self, AstNode}; use crate::{ ids::AstItemDef, AstDatabase, Const, DefDatabase, Enum, EnumVariant, FieldSource, Function, - HirFileId, MacroDef, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, - Union, + HasBody, HirDatabase, HirFileId, MacroDef, Module, ModuleSource, Static, Struct, StructField, + Trait, TypeAlias, Union, }; pub struct Source { @@ -108,3 +108,27 @@ impl HasSource for MacroDef { Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) } } } + +pub trait HasBodySource: HasBody + HasSource +where + Self::Ast: AstNode, +{ + fn expr_source( + self, + db: &impl HirDatabase, + expr_id: crate::expr::ExprId, + ) -> Option> { + let source_map = self.body_source_map(db); + let expr_syntax = source_map.expr_syntax(expr_id)?; + let source = self.source(db); + let node = expr_syntax.to_node(&source.ast.syntax()); + ast::Expr::cast(node).map(|ast| Source { file_id: source.file_id, ast }) + } +} + +impl HasBodySource for T +where + T: HasBody + HasSource, + T::Ast: AstNode, +{ +} diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 57225ae91..7cdc7555c 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -128,27 +128,27 @@ impl Index for Body { } impl BodySourceMap { - pub fn expr_syntax(&self, expr: ExprId) -> Option { + pub(crate) fn expr_syntax(&self, expr: ExprId) -> Option { self.expr_map_back.get(expr).cloned() } - pub fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option { + pub(crate) fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option { self.expr_map.get(&ptr).cloned() } - pub fn node_expr(&self, node: &ast::Expr) -> Option { + pub(crate) fn node_expr(&self, node: &ast::Expr) -> Option { self.expr_map.get(&SyntaxNodePtr::new(node.syntax())).cloned() } - pub fn pat_syntax(&self, pat: PatId) -> Option { + pub(crate) fn pat_syntax(&self, pat: PatId) -> Option { self.pat_map_back.get(pat).cloned() } - pub fn node_pat(&self, node: &ast::Pat) -> Option { + pub(crate) fn node_pat(&self, node: &ast::Pat) -> Option { self.pat_map.get(&Either::A(AstPtr::new(node))).cloned() } - pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr { + pub(crate) fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr { self.field_map[&(expr, field)] } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 018fcd096..752653ad7 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -75,8 +75,8 @@ pub use self::{ pub use self::code_model::{ docs::{DocDef, Docs, Documentation}, - src::{HasSource, Source}, + src::{HasBodySource, HasSource, Source}, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, - EnumVariant, FieldSource, FnData, Function, MacroDef, Module, ModuleDef, ModuleSource, Static, - Struct, StructField, Trait, TypeAlias, Union, + EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, ModuleSource, + Static, Struct, StructField, Trait, TypeAlias, Union, }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 56ff7da3a..43aec201a 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -27,9 +27,9 @@ use crate::{ name, path::{PathKind, PathSegment}, ty::method_resolution::implements_trait, - AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HirDatabase, HirFileId, - MacroDef, Module, ModuleDef, Name, Path, PerNs, Resolution, Resolver, Static, Struct, Trait, - Ty, + AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HasBody, HirDatabase, + HirFileId, MacroDef, Module, ModuleDef, Name, Path, PerNs, Resolution, Resolver, Static, + Struct, Trait, Ty, }; /// Locates the module by `FileId`. Picks topmost module in the file. diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 812990426..9ba146299 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -50,8 +50,8 @@ use crate::{ }, ty::infer::diagnostics::InferenceDiagnostic, type_ref::{Mutability, TypeRef}, - AdtDef, ConstData, DefWithBody, FnData, Function, HirDatabase, ImplItem, ModuleDef, Name, Path, - StructField, + AdtDef, ConstData, DefWithBody, FnData, Function, HasBody, HirDatabase, ImplItem, ModuleDef, + Name, Path, StructField, }; mod unify; -- cgit v1.2.3