From 7f3bf7cc738d02fde80d4fde9f32cbbe72896b87 Mon Sep 17 00:00:00 2001 From: Lenard Pratt Date: Sat, 30 Mar 2019 10:50:00 +0000 Subject: Added defWithBody --- crates/ra_hir/src/code_model_api.rs | 84 ++++++++++++++++++++++++++++++++++--- crates/ra_hir/src/db.rs | 9 ++-- crates/ra_hir/src/expr.rs | 40 +++++++++++++----- crates/ra_hir/src/expr/scope.rs | 6 +-- crates/ra_hir/src/lib.rs | 2 + 5 files changed, 117 insertions(+), 24 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 624c25c4d..db6e67d7f 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -433,6 +433,78 @@ impl Docs for EnumVariant { } } +/// The defs which have a body. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum DefWithBody { + Func(Function), + Const(Const), + Static(Static), +} + +impl DefWithBody { + pub fn get_funct(&self) -> &Function { + match *self { + DefWithBody::Func(ref f) => f, + _ => unreachable!() + } + } + + pub fn const_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + match *self { + DefWithBody::Const(ref c) => c.source(db), + _ => unreachable!() + } + } + + pub fn func_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + match *self { + DefWithBody::Func(ref f) => f.source(db), + _ => unreachable!() + } + } + + pub fn static_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { + match *self { + DefWithBody::Static(ref s) => s.source(db), + _ => unreachable!() + } + } + + pub fn infer(&self, db: &impl HirDatabase) -> Arc { + db.infer(*self) + } + + pub fn body(&self, db: &impl HirDatabase) -> Arc { + db.body_hir(*self) + } + + /// Builds a resolver for code inside this item. + pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { + // // take the outer scope... + // let r = self + // .impl_block(db) + // .map(|ib| ib.resolver(db)) + // .unwrap_or_else(|| self.module(db).resolver(db)); + // // ...and add generic params, if present + // let p = self.generic_params(db); + // let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; + // r + unimplemented!() + } + + pub fn signature(&self, db: &impl HirDatabase) -> Arc { + // db.fn_signature(*self) + unimplemented!() + } + + pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { + let scopes = db.expr_scopes(*self); + let source_map = db.body_with_source_map(*self).1; + ScopesWithSourceMap { scopes, source_map } + } + +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Function { pub(crate) id: FunctionId, @@ -483,11 +555,11 @@ impl Function { } pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc { - db.body_with_source_map(*self).1 + db.body_with_source_map(DefWithBody::Func(*self)).1 } pub fn body(&self, db: &impl HirDatabase) -> Arc { - db.body_hir(*self) + db.body_hir(DefWithBody::Func(*self)) } pub fn ty(&self, db: &impl HirDatabase) -> Ty { @@ -495,8 +567,8 @@ impl Function { } pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { - let scopes = db.expr_scopes(*self); - let source_map = db.body_with_source_map(*self).1; + let scopes = db.expr_scopes( DefWithBody::Func(*self)); + let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1; ScopesWithSourceMap { scopes, source_map } } @@ -505,7 +577,7 @@ impl Function { } pub fn infer(&self, db: &impl HirDatabase) -> Arc { - db.infer(*self) + db.infer(DefWithBody::Func(*self)) } pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { @@ -716,4 +788,4 @@ impl Docs for TypeAlias { fn docs(&self, db: &impl HirDatabase) -> Option { docs_from_ast(&*self.source(db).1) } -} +} \ No newline at end of file diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 147005848..a2aff241c 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -8,6 +8,7 @@ use crate::{ Function, FnSignature, ExprScopes, TypeAlias, Struct, Enum, StructField, Const, ConstSignature, Static, + DefWithBody, nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap}, ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig}, adt::{StructData, EnumData}, @@ -83,10 +84,10 @@ pub trait DefDatabase: SourceDatabase + AsRef { #[salsa::query_group(HirDatabaseStorage)] pub trait HirDatabase: DefDatabase { #[salsa::invoke(ExprScopes::expr_scopes_query)] - fn expr_scopes(&self, func: Function) -> Arc; + fn expr_scopes(&self, def: DefWithBody) -> Arc; #[salsa::invoke(crate::ty::infer)] - fn infer(&self, func: Function) -> Arc; + fn infer(&self, def:DefWithBody) -> Arc; #[salsa::invoke(crate::ty::type_for_def)] fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty; @@ -100,11 +101,11 @@ pub trait HirDatabase: DefDatabase { #[salsa::invoke(crate::expr::body_with_source_map_query)] fn body_with_source_map( &self, - func: Function, + def: DefWithBody, ) -> (Arc, Arc); #[salsa::invoke(crate::expr::body_hir_query)] - fn body_hir(&self, func: Function) -> Arc; + fn body_hir(&self, def: DefWithBody) -> Arc; #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] fn impls_in_crate(&self, krate: Crate) -> Arc; diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index a85422955..280746761 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -10,7 +10,7 @@ use ra_syntax::{ }; use crate::{ - Path, Name, HirDatabase, Function, Resolver, + Path, Name, HirDatabase, Function, Resolver,DefWithBody, name::AsName, type_ref::{Mutability, TypeRef}, }; @@ -29,7 +29,7 @@ impl_arena_id!(ExprId); pub struct Body { // FIXME: this should be more general, consts & statics also have bodies /// The Function of the item this body belongs to - owner: Function, + owner: DefWithBody, exprs: Arena, pats: Arena, /// The patterns for the function's parameters. While the parameter types are @@ -66,7 +66,7 @@ impl Body { self.body_expr } - pub fn owner(&self) -> Function { + pub fn owner(&self) -> DefWithBody { self.owner } @@ -464,7 +464,7 @@ impl Pat { // Queries struct ExprCollector { - owner: Function, + owner: DefWithBody, exprs: Arena, pats: Arena, source_map: BodySourceMap, @@ -473,7 +473,7 @@ struct ExprCollector { } impl ExprCollector { - fn new(owner: Function) -> Self { + fn new(owner: DefWithBody) -> Self { ExprCollector { owner, exprs: Arena::default(), @@ -503,6 +503,9 @@ impl ExprCollector { self.exprs.alloc(block) } + + + fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); match expr.kind() { @@ -871,6 +874,15 @@ impl ExprCollector { } } + + fn collect_const_body(&mut self,node:&ast::ConstDef) { + + } + + fn collect_static_body(&mut self,node:&ast::StaticDef) { + + } + fn collect_fn_body(&mut self, node: &ast::FnDef) { if let Some(param_list) = node.param_list() { if let Some(self_param) = param_list.self_param() { @@ -917,19 +929,25 @@ impl ExprCollector { pub(crate) fn body_with_source_map_query( db: &impl HirDatabase, - func: Function, + def: DefWithBody, ) -> (Arc, Arc) { - let mut collector = ExprCollector::new(func); - // FIXME: consts, etc. - collector.collect_fn_body(&func.source(db).1); + let mut collector = ExprCollector::new(def); + // FIXME: do can this be turned into a method + + match def { + DefWithBody::Const(ref c) => collector.collect_const_body(&def.const_source(db).1), + DefWithBody::Func(ref f) => collector.collect_fn_body(&def.func_source(db).1), + DefWithBody::Static(ref s) => collector.collect_static_body(&def.static_source(db).1) + } + let (body, source_map) = collector.finish(); (Arc::new(body), Arc::new(source_map)) } -pub(crate) fn body_hir_query(db: &impl HirDatabase, func: Function) -> Arc { - db.body_with_source_map(func).0 +pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { + db.body_with_source_map(def).0 } #[cfg(test)] diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index ed005c9f7..539da06c3 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -10,7 +10,7 @@ use ra_syntax::{ use ra_arena::{Arena, RawId, impl_arena_id}; use crate::{ - Name, AsName, Function, + Name, AsName, Function,DefWithBody, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, HirDatabase, }; @@ -40,8 +40,8 @@ pub struct ScopeData { impl ExprScopes { // FIXME: This should take something more general than Function - pub(crate) fn expr_scopes_query(db: &impl HirDatabase, function: Function) -> Arc { - let body = db.body_hir(function); + pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { + let body = db.body_hir(def); let res = ExprScopes::new(body); Arc::new(res) } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 7c603bbd3..62cec72d9 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -67,10 +67,12 @@ pub use self::{ pub use self::code_model_api::{ Crate, CrateDependency, + DefWithBody, Module, ModuleDef, ModuleSource, Struct, Enum, EnumVariant, Function, FnSignature, StructField, FieldSource, Static, Const, ConstSignature, Trait, TypeAlias, + }; -- cgit v1.2.3 From 88e22e9d70ac3a35989ad1d45386f86697877c4c Mon Sep 17 00:00:00 2001 From: Lenard Pratt Date: Sat, 30 Mar 2019 11:17:31 +0000 Subject: Added const bodies and static body to the ast and added inference the inference test reduce code duplication --- crates/ra_hir/src/code_model_api.rs | 87 +++++++++++++++---------------------- crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/expr.rs | 36 +++++++-------- crates/ra_hir/src/expr/scope.rs | 3 +- crates/ra_hir/src/lib.rs | 1 - crates/ra_hir/src/source_binder.rs | 44 ++++++++++++++++++- crates/ra_hir/src/ty/infer.rs | 20 ++++++--- crates/ra_hir/src/ty/tests.rs | 61 ++++++++++++++++++++++++-- 8 files changed, 168 insertions(+), 86 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index db6e67d7f..c3e5e26c3 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -436,65 +436,33 @@ impl Docs for EnumVariant { /// The defs which have a body. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum DefWithBody { - Func(Function), + Function(Function), Const(Const), Static(Static), } -impl DefWithBody { - pub fn get_funct(&self) -> &Function { - match *self { - DefWithBody::Func(ref f) => f, - _ => unreachable!() - } - } - - pub fn const_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { - match *self { - DefWithBody::Const(ref c) => c.source(db), - _ => unreachable!() - } - } - - pub fn func_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { - match *self { - DefWithBody::Func(ref f) => f.source(db), - _ => unreachable!() - } - } - - pub fn static_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { - match *self { - DefWithBody::Static(ref s) => s.source(db), - _ => unreachable!() - } - } +impl_froms!(DefWithBody: Function, Const, Static); +impl DefWithBody { pub fn infer(&self, db: &impl HirDatabase) -> Arc { db.infer(*self) } + pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc { + db.body_with_source_map(*self).1 + } + pub fn body(&self, db: &impl HirDatabase) -> Arc { db.body_hir(*self) } - + /// Builds a resolver for code inside this item. pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { - // // take the outer scope... - // let r = self - // .impl_block(db) - // .map(|ib| ib.resolver(db)) - // .unwrap_or_else(|| self.module(db).resolver(db)); - // // ...and add generic params, if present - // let p = self.generic_params(db); - // let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; - // r - unimplemented!() - } - - pub fn signature(&self, db: &impl HirDatabase) -> Arc { - // db.fn_signature(*self) - unimplemented!() + match *self { + DefWithBody::Const(ref c) => c.resolver(db), + DefWithBody::Function(ref f) => f.resolver(db), + DefWithBody::Static(ref s) => s.resolver(db), + } } pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { @@ -502,7 +470,6 @@ impl DefWithBody { let source_map = db.body_with_source_map(*self).1; ScopesWithSourceMap { scopes, source_map } } - } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -555,11 +522,11 @@ impl Function { } pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc { - db.body_with_source_map(DefWithBody::Func(*self)).1 + db.body_with_source_map((*self).into()).1 } pub fn body(&self, db: &impl HirDatabase) -> Arc { - db.body_hir(DefWithBody::Func(*self)) + db.body_hir((*self).into()) } pub fn ty(&self, db: &impl HirDatabase) -> Ty { @@ -567,8 +534,8 @@ impl Function { } pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { - let scopes = db.expr_scopes( DefWithBody::Func(*self)); - let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1; + let scopes = db.expr_scopes((*self).into()); + let source_map = db.body_with_source_map((*self).into()).1; ScopesWithSourceMap { scopes, source_map } } @@ -577,7 +544,7 @@ impl Function { } pub fn infer(&self, db: &impl HirDatabase) -> Arc { - db.infer(DefWithBody::Func(*self)) + db.infer((*self).into()) } pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { @@ -633,6 +600,14 @@ impl Const { db.const_signature(*self) } + pub fn infer(&self, db: &impl HirDatabase) -> Arc { + db.infer((*self).into()) + } + + pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc { + db.body_with_source_map((*self).into()).1 + } + /// The containing impl block, if this is a method. pub fn impl_block(&self, db: &impl DefDatabase) -> Option { let module_impls = db.impls_in_module(self.module(db)); @@ -697,6 +672,14 @@ impl Static { // take the outer scope... self.module(db).resolver(db) } + + pub fn infer(&self, db: &impl HirDatabase) -> Arc { + db.infer((*self).into()) + } + + pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc { + db.body_with_source_map((*self).into()).1 + } } impl Docs for Static { @@ -788,4 +771,4 @@ impl Docs for TypeAlias { fn docs(&self, db: &impl HirDatabase) -> Option { docs_from_ast(&*self.source(db).1) } -} \ No newline at end of file +} diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index a2aff241c..be8a8c98b 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -87,7 +87,7 @@ pub trait HirDatabase: DefDatabase { fn expr_scopes(&self, def: DefWithBody) -> Arc; #[salsa::invoke(crate::ty::infer)] - fn infer(&self, def:DefWithBody) -> Arc; + fn infer(&self, def: DefWithBody) -> Arc; #[salsa::invoke(crate::ty::type_for_def)] fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty; diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 280746761..bc4c63d3c 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -10,7 +10,7 @@ use ra_syntax::{ }; use crate::{ - Path, Name, HirDatabase, Function, Resolver,DefWithBody, + Path, Name, HirDatabase, Resolver,DefWithBody, name::AsName, type_ref::{Mutability, TypeRef}, }; @@ -27,8 +27,7 @@ impl_arena_id!(ExprId); /// The body of an item (function, const etc.). #[derive(Debug, Eq, PartialEq)] pub struct Body { - // FIXME: this should be more general, consts & statics also have bodies - /// The Function of the item this body belongs to + /// The def of the item this body belongs to owner: DefWithBody, exprs: Arena, pats: Arena, @@ -503,9 +502,6 @@ impl ExprCollector { self.exprs.alloc(block) } - - - fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); match expr.kind() { @@ -874,13 +870,14 @@ impl ExprCollector { } } - - fn collect_const_body(&mut self,node:&ast::ConstDef) { - + fn collect_const_body(&mut self, node: &ast::ConstDef) { + let body = self.collect_expr_opt(node.body()); + self.body_expr = Some(body); } - fn collect_static_body(&mut self,node:&ast::StaticDef) { - + fn collect_static_body(&mut self, node: &ast::StaticDef) { + let body = self.collect_expr_opt(node.body()); + self.body_expr = Some(body); } fn collect_fn_body(&mut self, node: &ast::FnDef) { @@ -931,28 +928,27 @@ pub(crate) fn body_with_source_map_query( db: &impl HirDatabase, def: DefWithBody, ) -> (Arc, Arc) { - let mut collector = ExprCollector::new(def); - // FIXME: do can this be turned into a method - match def { - DefWithBody::Const(ref c) => collector.collect_const_body(&def.const_source(db).1), - DefWithBody::Func(ref f) => collector.collect_fn_body(&def.func_source(db).1), - DefWithBody::Static(ref s) => collector.collect_static_body(&def.static_source(db).1) + DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1), + DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1), + DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1), } - + let (body, source_map) = collector.finish(); (Arc::new(body), Arc::new(source_map)) } pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { - db.body_with_source_map(def).0 + db.body_with_source_map(def).0 } +#[cfg(test)] +use crate::{Function}; #[cfg(test)] fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { - let mut collector = ExprCollector::new(function); + let mut collector = ExprCollector::new(DefWithBody::Function(function)); collector.collect_fn_body(node); collector.finish() } diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 539da06c3..9d6b1eb2b 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -10,7 +10,7 @@ use ra_syntax::{ use ra_arena::{Arena, RawId, impl_arena_id}; use crate::{ - Name, AsName, Function,DefWithBody, + Name, AsName,DefWithBody, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, HirDatabase, }; @@ -297,6 +297,7 @@ mod tests { use ra_syntax::{SourceFile, algo::find_node_at_offset}; use test_utils::{extract_offset, assert_eq_text}; use ra_arena::ArenaId; + use crate::{Function}; use crate::expr; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 62cec72d9..5b6abcf6d 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -74,5 +74,4 @@ pub use self::code_model_api::{ StructField, FieldSource, Static, Const, ConstSignature, Trait, TypeAlias, - }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 9dae4c3d1..430dbc522 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -13,7 +13,7 @@ use ra_syntax::{ }; use crate::{ - HirDatabase, Function, Struct, Enum, + HirDatabase, Function, Struct, Enum,Const,Static, AsName, Module, HirFileId, Crate, Trait, Resolver, ids::LocationCtx, expr, AstId @@ -87,6 +87,27 @@ fn module_from_source( ) } +pub fn const_from_source( + db: &impl HirDatabase, + file_id: FileId, + const_def: &ast::ConstDef, +) -> Option { + let module = module_from_child_node(db, file_id, const_def.syntax())?; + let res = const_from_module(db, module, const_def); + Some(res) +} + +pub fn const_from_module( + db: &impl HirDatabase, + module: Module, + const_def: &ast::ConstDef, +) -> Const { + let (file_id, _) = module.definition_source(db); + let file_id = file_id.into(); + let ctx = LocationCtx::new(db, module, file_id); + Const { id: ctx.to_def(const_def) } +} + pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option { let file = db.parse(position.file_id); let fn_def = find_node_at_offset::(file.syntax(), position.offset)?; @@ -134,6 +155,27 @@ pub fn struct_from_module( Struct { id: ctx.to_def(struct_def) } } +pub fn static_from_source( + db: &impl HirDatabase, + file_id: FileId, + static_def: &ast::StaticDef, +) -> Option { + let module = module_from_child_node(db, file_id, static_def.syntax())?; + let res = static_from_module(db, module, static_def); + Some(res) +} + +pub fn static_from_module( + db: &impl HirDatabase, + module: Module, + static_def: &ast::StaticDef, +) -> Static { + let (file_id, _) = module.definition_source(db); + let file_id = file_id.into(); + let ctx = LocationCtx::new(db, module, file_id); + Static { id: ctx.to_def(static_def) } +} + pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { let (file_id, _) = module.definition_source(db); let file_id = file_id.into(); diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 573115321..887153484 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -27,8 +27,9 @@ use test_utils::tested_by; use crate::{ Function, StructField, Path, Name, - FnSignature, AdtDef, + FnSignature, AdtDef,ConstSignature, HirDatabase, + DefWithBody, ImplItem, type_ref::{TypeRef, Mutability}, expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, @@ -43,14 +44,17 @@ use crate::{ use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeCtor}; /// The entry point of type inference. -pub fn infer(db: &impl HirDatabase, func: Function) -> Arc { +pub fn infer(db: &impl HirDatabase, def: DefWithBody) -> Arc { db.check_canceled(); - let body = func.body(db); - let resolver = func.resolver(db); + let body = def.body(db); + let resolver = def.resolver(db); let mut ctx = InferenceContext::new(db, body, resolver); - let signature = func.signature(db); - ctx.collect_fn_signature(&signature); + match def { + DefWithBody::Const(ref c) => ctx.collect_const_signature(&c.signature(db)), + DefWithBody::Function(ref f) => ctx.collect_fn_signature(&f.signature(db)), + DefWithBody::Static(ref s) => ctx.collect_const_signature(&s.signature(db)), + } ctx.infer_body(); @@ -1142,6 +1146,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { ty } + fn collect_const_signature(&mut self, signature: &ConstSignature) { + self.return_ty = self.make_ty(signature.type_ref()); + } + fn collect_fn_signature(&mut self, signature: &FnSignature) { let body = Arc::clone(&self.body); // avoid borrow checker problem for (type_ref, pat) in signature.params().iter().zip(body.params()) { diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 655f3c522..1e84e2d06 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -11,6 +11,8 @@ use crate::{ source_binder, mock::MockDatabase, ty::display::HirDisplay, + ty::InferenceResult, + expr::BodySourceMap }; // These tests compare the inference results for all expressions in a file @@ -1267,6 +1269,9 @@ fn test() { } "#), @r###" +[52; 53) '1': u32 +[103; 104) '2': u32 +[211; 212) '5': u32 [227; 305) '{ ...:ID; }': () [237; 238) 'x': u32 [241; 252) 'Struct::FOO': u32 @@ -1855,6 +1860,9 @@ fn test() { } "#), @r###" +[49; 50) '0': u32 +[80; 83) '101': u32 +[126; 128) '99': u32 [95; 213) '{ ...NST; }': () [138; 139) 'x': {unknown} [142; 153) 'LOCAL_CONST': {unknown} @@ -1881,6 +1889,10 @@ fn test() { } "#), @r###" +[29; 32) '101': u32 +[70; 73) '101': u32 +[118; 120) '99': u32 +[161; 163) '99': u32 [85; 280) '{ ...MUT; }': () [173; 174) 'x': {unknown} [177; 189) 'LOCAL_STATIC': {unknown} @@ -2212,6 +2224,24 @@ fn test>() { ); } +#[test] +fn infer_const_body() { + assert_snapshot_matches!( + infer(r#" +const A: u32 = 1 + 1; +static B: u64 = { let x = 1; x }; +"#), + @r###" +[16; 17) '1': u32 +[16; 21) '1 + 1': u32 +[20; 21) '1': u32 +[39; 55) '{ let ...1; x }': u64 +[45; 46) 'x': u64 +[49; 50) '1': u64 +[52; 53) 'x': u64"### + ); +} + fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { let func = source_binder::function_from_position(db, pos).unwrap(); let body_source_map = func.body_source_map(db); @@ -2228,11 +2258,11 @@ fn infer(content: &str) -> String { let source_file = db.parse(file_id); let mut acc = String::new(); acc.push_str("\n"); - for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { - let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap(); - let inference_result = func.infer(&db); - let body_source_map = func.body_source_map(&db); + + let mut infer_def = |inference_result: Arc, + body_source_map: Arc| { let mut types = Vec::new(); + for (pat, ty) in inference_result.type_of_pat.iter() { let syntax_ptr = match body_source_map.pat_syntax(pat) { Some(sp) => sp, @@ -2240,6 +2270,7 @@ fn infer(content: &str) -> String { }; types.push((syntax_ptr, ty)); } + for (expr, ty) in inference_result.type_of_expr.iter() { let syntax_ptr = match body_source_map.expr_syntax(expr) { Some(sp) => sp, @@ -2260,7 +2291,29 @@ fn infer(content: &str) -> String { ) .unwrap(); } + }; + + for const_def in source_file.syntax().descendants().filter_map(ast::ConstDef::cast) { + let konst = source_binder::const_from_source(&db, file_id, const_def).unwrap(); + let inference_result = konst.infer(&db); + let body_source_map = konst.body_source_map(&db); + infer_def(inference_result, body_source_map) } + + for static_def in source_file.syntax().descendants().filter_map(ast::StaticDef::cast) { + let static_ = source_binder::static_from_source(&db, file_id, static_def).unwrap(); + let inference_result = static_.infer(&db); + let body_source_map = static_.body_source_map(&db); + infer_def(inference_result, body_source_map) + } + + for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { + let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap(); + let inference_result = func.infer(&db); + let body_source_map = func.body_source_map(&db); + infer_def(inference_result, body_source_map) + } + acc.truncate(acc.trim_end().len()); acc } -- cgit v1.2.3 From b9d2c2c21fe8880fe1ca29b70b03db1d3faac554 Mon Sep 17 00:00:00 2001 From: Lenard Pratt Date: Tue, 2 Apr 2019 19:21:08 +0100 Subject: made ExprCollector pub(crate) and moved collect_fn_body_syntax --- crates/ra_hir/src/expr.rs | 11 +---------- crates/ra_hir/src/expr/scope.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index bc4c63d3c..51366de0a 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -462,7 +462,7 @@ impl Pat { // Queries -struct ExprCollector { +pub(crate) struct ExprCollector { owner: DefWithBody, exprs: Arena, pats: Arena, @@ -943,12 +943,3 @@ pub(crate) fn body_with_source_map_query( pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { db.body_with_source_map(def).0 } - -#[cfg(test)] -use crate::{Function}; -#[cfg(test)] -fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { - let mut collector = ExprCollector::new(DefWithBody::Function(function)); - collector.collect_fn_body(node); - collector.finish() -} diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 9d6b1eb2b..48283907b 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -297,9 +297,9 @@ mod tests { use ra_syntax::{SourceFile, algo::find_node_at_offset}; use test_utils::{extract_offset, assert_eq_text}; use ra_arena::ArenaId; - use crate::{Function}; + use crate::Function; - use crate::expr; + use crate::expr::{ExprCollector}; use super::*; @@ -317,7 +317,7 @@ mod tests { let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; - let (body, source_map) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); + let (body, source_map) = collect_fn_body_syntax(irrelevant_function, fn_def); let scopes = ExprScopes::new(Arc::new(body)); let scopes = ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; @@ -406,6 +406,12 @@ mod tests { ); } + fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { + let mut collector = ExprCollector::new(DefWithBody::Function(function)); + collector.collect_fn_body(node); + collector.finish() + } + fn do_check_local_name(code: &str, expected_offset: u32) { let (off, code) = extract_offset(code); let file = SourceFile::parse(&code); @@ -416,7 +422,7 @@ mod tests { let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; - let (body, source_map) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); + let (body, source_map) = collect_fn_body_syntax(irrelevant_function, fn_def); let scopes = ExprScopes::new(Arc::new(body)); let scopes = ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; -- cgit v1.2.3