diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-02 20:01:54 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-02 20:01:54 +0100 |
commit | fdbebccd71d38c4dffffe918b036bbfa39355c5f (patch) | |
tree | 541c13ce234023bbfc640a38e55e860c029fd52c /crates | |
parent | 01a54f094ba7c17a6788ec706f12b07d8d60af4b (diff) | |
parent | b9d2c2c21fe8880fe1ca29b70b03db1d3faac554 (diff) |
Merge #1076
1076: Const body inference r=flodiebold a=Lapz
This is the second part of #887. I've added type inference on const bodies and introduced the DefWithBody containing Function, Const and Static. I want to add tests but im unsure on how I would go about testing that completions work.
Co-authored-by: Lenard Pratt <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 65 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir/src/expr.rs | 47 | ||||
-rw-r--r-- | crates/ra_hir/src/expr/scope.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 44 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 20 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 61 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 12 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 2 |
10 files changed, 231 insertions, 49 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 87d81f4a4..9e6170440 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -429,6 +429,45 @@ impl Docs for EnumVariant { | |||
429 | } | 429 | } |
430 | } | 430 | } |
431 | 431 | ||
432 | /// The defs which have a body. | ||
433 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
434 | pub enum DefWithBody { | ||
435 | Function(Function), | ||
436 | Const(Const), | ||
437 | Static(Static), | ||
438 | } | ||
439 | |||
440 | impl_froms!(DefWithBody: Function, Const, Static); | ||
441 | |||
442 | impl DefWithBody { | ||
443 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | ||
444 | db.infer(*self) | ||
445 | } | ||
446 | |||
447 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | ||
448 | db.body_with_source_map(*self).1 | ||
449 | } | ||
450 | |||
451 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | ||
452 | db.body_hir(*self) | ||
453 | } | ||
454 | |||
455 | /// Builds a resolver for code inside this item. | ||
456 | pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { | ||
457 | match *self { | ||
458 | DefWithBody::Const(ref c) => c.resolver(db), | ||
459 | DefWithBody::Function(ref f) => f.resolver(db), | ||
460 | DefWithBody::Static(ref s) => s.resolver(db), | ||
461 | } | ||
462 | } | ||
463 | |||
464 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { | ||
465 | let scopes = db.expr_scopes(*self); | ||
466 | let source_map = db.body_with_source_map(*self).1; | ||
467 | ScopesWithSourceMap { scopes, source_map } | ||
468 | } | ||
469 | } | ||
470 | |||
432 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 471 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
433 | pub struct Function { | 472 | pub struct Function { |
434 | pub(crate) id: FunctionId, | 473 | pub(crate) id: FunctionId, |
@@ -479,11 +518,11 @@ impl Function { | |||
479 | } | 518 | } |
480 | 519 | ||
481 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | 520 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { |
482 | db.body_with_source_map(*self).1 | 521 | db.body_with_source_map((*self).into()).1 |
483 | } | 522 | } |
484 | 523 | ||
485 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | 524 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { |
486 | db.body_hir(*self) | 525 | db.body_hir((*self).into()) |
487 | } | 526 | } |
488 | 527 | ||
489 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 528 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { |
@@ -491,8 +530,8 @@ impl Function { | |||
491 | } | 530 | } |
492 | 531 | ||
493 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { | 532 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { |
494 | let scopes = db.expr_scopes(*self); | 533 | let scopes = db.expr_scopes((*self).into()); |
495 | let source_map = db.body_with_source_map(*self).1; | 534 | let source_map = db.body_with_source_map((*self).into()).1; |
496 | ScopesWithSourceMap { scopes, source_map } | 535 | ScopesWithSourceMap { scopes, source_map } |
497 | } | 536 | } |
498 | 537 | ||
@@ -501,7 +540,7 @@ impl Function { | |||
501 | } | 540 | } |
502 | 541 | ||
503 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 542 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
504 | db.infer(*self) | 543 | db.infer((*self).into()) |
505 | } | 544 | } |
506 | 545 | ||
507 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { | 546 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
@@ -557,6 +596,14 @@ impl Const { | |||
557 | db.const_signature(*self) | 596 | db.const_signature(*self) |
558 | } | 597 | } |
559 | 598 | ||
599 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | ||
600 | db.infer((*self).into()) | ||
601 | } | ||
602 | |||
603 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | ||
604 | db.body_with_source_map((*self).into()).1 | ||
605 | } | ||
606 | |||
560 | /// The containing impl block, if this is a method. | 607 | /// The containing impl block, if this is a method. |
561 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { | 608 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { |
562 | let module_impls = db.impls_in_module(self.module(db)); | 609 | let module_impls = db.impls_in_module(self.module(db)); |
@@ -621,6 +668,14 @@ impl Static { | |||
621 | // take the outer scope... | 668 | // take the outer scope... |
622 | self.module(db).resolver(db) | 669 | self.module(db).resolver(db) |
623 | } | 670 | } |
671 | |||
672 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | ||
673 | db.infer((*self).into()) | ||
674 | } | ||
675 | |||
676 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | ||
677 | db.body_with_source_map((*self).into()).1 | ||
678 | } | ||
624 | } | 679 | } |
625 | 680 | ||
626 | impl Docs for Static { | 681 | impl Docs for Static { |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 147005848..be8a8c98b 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -8,6 +8,7 @@ use crate::{ | |||
8 | Function, FnSignature, ExprScopes, TypeAlias, | 8 | Function, FnSignature, ExprScopes, TypeAlias, |
9 | Struct, Enum, StructField, | 9 | Struct, Enum, StructField, |
10 | Const, ConstSignature, Static, | 10 | Const, ConstSignature, Static, |
11 | DefWithBody, | ||
11 | nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap}, | 12 | nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap}, |
12 | ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig}, | 13 | ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig}, |
13 | adt::{StructData, EnumData}, | 14 | adt::{StructData, EnumData}, |
@@ -83,10 +84,10 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> { | |||
83 | #[salsa::query_group(HirDatabaseStorage)] | 84 | #[salsa::query_group(HirDatabaseStorage)] |
84 | pub trait HirDatabase: DefDatabase { | 85 | pub trait HirDatabase: DefDatabase { |
85 | #[salsa::invoke(ExprScopes::expr_scopes_query)] | 86 | #[salsa::invoke(ExprScopes::expr_scopes_query)] |
86 | fn expr_scopes(&self, func: Function) -> Arc<ExprScopes>; | 87 | fn expr_scopes(&self, def: DefWithBody) -> Arc<ExprScopes>; |
87 | 88 | ||
88 | #[salsa::invoke(crate::ty::infer)] | 89 | #[salsa::invoke(crate::ty::infer)] |
89 | fn infer(&self, func: Function) -> Arc<InferenceResult>; | 90 | fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>; |
90 | 91 | ||
91 | #[salsa::invoke(crate::ty::type_for_def)] | 92 | #[salsa::invoke(crate::ty::type_for_def)] |
92 | fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty; | 93 | fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty; |
@@ -100,11 +101,11 @@ pub trait HirDatabase: DefDatabase { | |||
100 | #[salsa::invoke(crate::expr::body_with_source_map_query)] | 101 | #[salsa::invoke(crate::expr::body_with_source_map_query)] |
101 | fn body_with_source_map( | 102 | fn body_with_source_map( |
102 | &self, | 103 | &self, |
103 | func: Function, | 104 | def: DefWithBody, |
104 | ) -> (Arc<crate::expr::Body>, Arc<crate::expr::BodySourceMap>); | 105 | ) -> (Arc<crate::expr::Body>, Arc<crate::expr::BodySourceMap>); |
105 | 106 | ||
106 | #[salsa::invoke(crate::expr::body_hir_query)] | 107 | #[salsa::invoke(crate::expr::body_hir_query)] |
107 | fn body_hir(&self, func: Function) -> Arc<crate::expr::Body>; | 108 | fn body_hir(&self, def: DefWithBody) -> Arc<crate::expr::Body>; |
108 | 109 | ||
109 | #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] | 110 | #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] |
110 | fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>; | 111 | fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>; |
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 45012827f..b2a237ece 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -10,7 +10,7 @@ use ra_syntax::{ | |||
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Path, Name, HirDatabase, Function, Resolver, | 13 | Path, Name, HirDatabase, Resolver,DefWithBody, |
14 | name::AsName, | 14 | name::AsName, |
15 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeRef}, |
16 | }; | 16 | }; |
@@ -27,9 +27,8 @@ impl_arena_id!(ExprId); | |||
27 | /// The body of an item (function, const etc.). | 27 | /// The body of an item (function, const etc.). |
28 | #[derive(Debug, Eq, PartialEq)] | 28 | #[derive(Debug, Eq, PartialEq)] |
29 | pub struct Body { | 29 | pub struct Body { |
30 | // FIXME: this should be more general, consts & statics also have bodies | 30 | /// The def of the item this body belongs to |
31 | /// The Function of the item this body belongs to | 31 | owner: DefWithBody, |
32 | owner: Function, | ||
33 | exprs: Arena<ExprId, Expr>, | 32 | exprs: Arena<ExprId, Expr>, |
34 | pats: Arena<PatId, Pat>, | 33 | pats: Arena<PatId, Pat>, |
35 | /// The patterns for the function's parameters. While the parameter types are | 34 | /// The patterns for the function's parameters. While the parameter types are |
@@ -66,7 +65,7 @@ impl Body { | |||
66 | self.body_expr | 65 | self.body_expr |
67 | } | 66 | } |
68 | 67 | ||
69 | pub fn owner(&self) -> Function { | 68 | pub fn owner(&self) -> DefWithBody { |
70 | self.owner | 69 | self.owner |
71 | } | 70 | } |
72 | 71 | ||
@@ -463,8 +462,8 @@ impl Pat { | |||
463 | 462 | ||
464 | // Queries | 463 | // Queries |
465 | 464 | ||
466 | struct ExprCollector { | 465 | pub(crate) struct ExprCollector { |
467 | owner: Function, | 466 | owner: DefWithBody, |
468 | exprs: Arena<ExprId, Expr>, | 467 | exprs: Arena<ExprId, Expr>, |
469 | pats: Arena<PatId, Pat>, | 468 | pats: Arena<PatId, Pat>, |
470 | source_map: BodySourceMap, | 469 | source_map: BodySourceMap, |
@@ -473,7 +472,7 @@ struct ExprCollector { | |||
473 | } | 472 | } |
474 | 473 | ||
475 | impl ExprCollector { | 474 | impl ExprCollector { |
476 | fn new(owner: Function) -> Self { | 475 | fn new(owner: DefWithBody) -> Self { |
477 | ExprCollector { | 476 | ExprCollector { |
478 | owner, | 477 | owner, |
479 | exprs: Arena::default(), | 478 | exprs: Arena::default(), |
@@ -866,6 +865,16 @@ impl ExprCollector { | |||
866 | } | 865 | } |
867 | } | 866 | } |
868 | 867 | ||
868 | fn collect_const_body(&mut self, node: &ast::ConstDef) { | ||
869 | let body = self.collect_expr_opt(node.body()); | ||
870 | self.body_expr = Some(body); | ||
871 | } | ||
872 | |||
873 | fn collect_static_body(&mut self, node: &ast::StaticDef) { | ||
874 | let body = self.collect_expr_opt(node.body()); | ||
875 | self.body_expr = Some(body); | ||
876 | } | ||
877 | |||
869 | fn collect_fn_body(&mut self, node: &ast::FnDef) { | 878 | fn collect_fn_body(&mut self, node: &ast::FnDef) { |
870 | if let Some(param_list) = node.param_list() { | 879 | if let Some(param_list) = node.param_list() { |
871 | if let Some(self_param) = param_list.self_param() { | 880 | if let Some(self_param) = param_list.self_param() { |
@@ -910,24 +919,20 @@ impl ExprCollector { | |||
910 | 919 | ||
911 | pub(crate) fn body_with_source_map_query( | 920 | pub(crate) fn body_with_source_map_query( |
912 | db: &impl HirDatabase, | 921 | db: &impl HirDatabase, |
913 | func: Function, | 922 | def: DefWithBody, |
914 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | 923 | ) -> (Arc<Body>, Arc<BodySourceMap>) { |
915 | let mut collector = ExprCollector::new(func); | 924 | let mut collector = ExprCollector::new(def); |
916 | 925 | ||
917 | // FIXME: consts, etc. | 926 | match def { |
918 | collector.collect_fn_body(&func.source(db).1); | 927 | DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1), |
928 | DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1), | ||
929 | DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1), | ||
930 | } | ||
919 | 931 | ||
920 | let (body, source_map) = collector.finish(); | 932 | let (body, source_map) = collector.finish(); |
921 | (Arc::new(body), Arc::new(source_map)) | 933 | (Arc::new(body), Arc::new(source_map)) |
922 | } | 934 | } |
923 | 935 | ||
924 | pub(crate) fn body_hir_query(db: &impl HirDatabase, func: Function) -> Arc<Body> { | 936 | pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { |
925 | db.body_with_source_map(func).0 | 937 | db.body_with_source_map(def).0 |
926 | } | ||
927 | |||
928 | #[cfg(test)] | ||
929 | fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { | ||
930 | let mut collector = ExprCollector::new(function); | ||
931 | collector.collect_fn_body(node); | ||
932 | collector.finish() | ||
933 | } | 938 | } |
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index ed005c9f7..48283907b 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs | |||
@@ -10,7 +10,7 @@ use ra_syntax::{ | |||
10 | use ra_arena::{Arena, RawId, impl_arena_id}; | 10 | use ra_arena::{Arena, RawId, impl_arena_id}; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Name, AsName, Function, | 13 | Name, AsName,DefWithBody, |
14 | expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, | 14 | expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, |
15 | HirDatabase, | 15 | HirDatabase, |
16 | }; | 16 | }; |
@@ -40,8 +40,8 @@ pub struct ScopeData { | |||
40 | 40 | ||
41 | impl ExprScopes { | 41 | impl ExprScopes { |
42 | // FIXME: This should take something more general than Function | 42 | // FIXME: This should take something more general than Function |
43 | pub(crate) fn expr_scopes_query(db: &impl HirDatabase, function: Function) -> Arc<ExprScopes> { | 43 | pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<ExprScopes> { |
44 | let body = db.body_hir(function); | 44 | let body = db.body_hir(def); |
45 | let res = ExprScopes::new(body); | 45 | let res = ExprScopes::new(body); |
46 | Arc::new(res) | 46 | Arc::new(res) |
47 | } | 47 | } |
@@ -297,8 +297,9 @@ mod tests { | |||
297 | use ra_syntax::{SourceFile, algo::find_node_at_offset}; | 297 | use ra_syntax::{SourceFile, algo::find_node_at_offset}; |
298 | use test_utils::{extract_offset, assert_eq_text}; | 298 | use test_utils::{extract_offset, assert_eq_text}; |
299 | use ra_arena::ArenaId; | 299 | use ra_arena::ArenaId; |
300 | use crate::Function; | ||
300 | 301 | ||
301 | use crate::expr; | 302 | use crate::expr::{ExprCollector}; |
302 | 303 | ||
303 | use super::*; | 304 | use super::*; |
304 | 305 | ||
@@ -316,7 +317,7 @@ mod tests { | |||
316 | let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); | 317 | let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); |
317 | let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); | 318 | let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); |
318 | let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; | 319 | let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; |
319 | let (body, source_map) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); | 320 | let (body, source_map) = collect_fn_body_syntax(irrelevant_function, fn_def); |
320 | let scopes = ExprScopes::new(Arc::new(body)); | 321 | let scopes = ExprScopes::new(Arc::new(body)); |
321 | let scopes = | 322 | let scopes = |
322 | ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; | 323 | ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; |
@@ -405,6 +406,12 @@ mod tests { | |||
405 | ); | 406 | ); |
406 | } | 407 | } |
407 | 408 | ||
409 | fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { | ||
410 | let mut collector = ExprCollector::new(DefWithBody::Function(function)); | ||
411 | collector.collect_fn_body(node); | ||
412 | collector.finish() | ||
413 | } | ||
414 | |||
408 | fn do_check_local_name(code: &str, expected_offset: u32) { | 415 | fn do_check_local_name(code: &str, expected_offset: u32) { |
409 | let (off, code) = extract_offset(code); | 416 | let (off, code) = extract_offset(code); |
410 | let file = SourceFile::parse(&code); | 417 | let file = SourceFile::parse(&code); |
@@ -415,7 +422,7 @@ mod tests { | |||
415 | let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); | 422 | let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); |
416 | 423 | ||
417 | let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; | 424 | let irrelevant_function = Function { id: crate::ids::FunctionId::from_raw(0.into()) }; |
418 | let (body, source_map) = expr::collect_fn_body_syntax(irrelevant_function, fn_def); | 425 | let (body, source_map) = collect_fn_body_syntax(irrelevant_function, fn_def); |
419 | let scopes = ExprScopes::new(Arc::new(body)); | 426 | let scopes = ExprScopes::new(Arc::new(body)); |
420 | let scopes = | 427 | let scopes = |
421 | ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; | 428 | ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 643bee6cd..c19450f39 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -67,6 +67,7 @@ pub use self::{ | |||
67 | 67 | ||
68 | pub use self::code_model_api::{ | 68 | pub use self::code_model_api::{ |
69 | Crate, CrateDependency, | 69 | Crate, CrateDependency, |
70 | DefWithBody, | ||
70 | Module, ModuleDef, ModuleSource, | 71 | Module, ModuleDef, ModuleSource, |
71 | Struct, Enum, EnumVariant, | 72 | Struct, Enum, EnumVariant, |
72 | Function, FnSignature, | 73 | Function, FnSignature, |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 54dc27399..182ed4c91 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -13,7 +13,7 @@ use ra_syntax::{ | |||
13 | }; | 13 | }; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | HirDatabase, Function, Struct, Enum, | 16 | HirDatabase, Function, Struct, Enum,Const,Static, |
17 | AsName, Module, HirFileId, Crate, Trait, Resolver, | 17 | AsName, Module, HirFileId, Crate, Trait, Resolver, |
18 | ids::LocationCtx, | 18 | ids::LocationCtx, |
19 | expr, AstId | 19 | expr, AstId |
@@ -87,6 +87,27 @@ fn module_from_source( | |||
87 | ) | 87 | ) |
88 | } | 88 | } |
89 | 89 | ||
90 | pub fn const_from_source( | ||
91 | db: &impl HirDatabase, | ||
92 | file_id: FileId, | ||
93 | const_def: &ast::ConstDef, | ||
94 | ) -> Option<Const> { | ||
95 | let module = module_from_child_node(db, file_id, const_def.syntax())?; | ||
96 | let res = const_from_module(db, module, const_def); | ||
97 | Some(res) | ||
98 | } | ||
99 | |||
100 | pub fn const_from_module( | ||
101 | db: &impl HirDatabase, | ||
102 | module: Module, | ||
103 | const_def: &ast::ConstDef, | ||
104 | ) -> Const { | ||
105 | let (file_id, _) = module.definition_source(db); | ||
106 | let file_id = file_id.into(); | ||
107 | let ctx = LocationCtx::new(db, module, file_id); | ||
108 | Const { id: ctx.to_def(const_def) } | ||
109 | } | ||
110 | |||
90 | pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { | 111 | pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { |
91 | let file = db.parse(position.file_id); | 112 | let file = db.parse(position.file_id); |
92 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?; | 113 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?; |
@@ -134,6 +155,27 @@ pub fn struct_from_module( | |||
134 | Struct { id: ctx.to_def(struct_def) } | 155 | Struct { id: ctx.to_def(struct_def) } |
135 | } | 156 | } |
136 | 157 | ||
158 | pub fn static_from_source( | ||
159 | db: &impl HirDatabase, | ||
160 | file_id: FileId, | ||
161 | static_def: &ast::StaticDef, | ||
162 | ) -> Option<Static> { | ||
163 | let module = module_from_child_node(db, file_id, static_def.syntax())?; | ||
164 | let res = static_from_module(db, module, static_def); | ||
165 | Some(res) | ||
166 | } | ||
167 | |||
168 | pub fn static_from_module( | ||
169 | db: &impl HirDatabase, | ||
170 | module: Module, | ||
171 | static_def: &ast::StaticDef, | ||
172 | ) -> Static { | ||
173 | let (file_id, _) = module.definition_source(db); | ||
174 | let file_id = file_id.into(); | ||
175 | let ctx = LocationCtx::new(db, module, file_id); | ||
176 | Static { id: ctx.to_def(static_def) } | ||
177 | } | ||
178 | |||
137 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { | 179 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { |
138 | let (file_id, _) = module.definition_source(db); | 180 | let (file_id, _) = module.definition_source(db); |
139 | let file_id = file_id.into(); | 181 | 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; | |||
27 | 27 | ||
28 | use crate::{ | 28 | use crate::{ |
29 | Function, StructField, Path, Name, | 29 | Function, StructField, Path, Name, |
30 | FnSignature, AdtDef, | 30 | FnSignature, AdtDef,ConstSignature, |
31 | HirDatabase, | 31 | HirDatabase, |
32 | DefWithBody, | ||
32 | ImplItem, | 33 | ImplItem, |
33 | type_ref::{TypeRef, Mutability}, | 34 | type_ref::{TypeRef, Mutability}, |
34 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, | 35 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, |
@@ -43,14 +44,17 @@ use crate::{ | |||
43 | use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeCtor}; | 44 | use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeCtor}; |
44 | 45 | ||
45 | /// The entry point of type inference. | 46 | /// The entry point of type inference. |
46 | pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> { | 47 | pub fn infer(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { |
47 | db.check_canceled(); | 48 | db.check_canceled(); |
48 | let body = func.body(db); | 49 | let body = def.body(db); |
49 | let resolver = func.resolver(db); | 50 | let resolver = def.resolver(db); |
50 | let mut ctx = InferenceContext::new(db, body, resolver); | 51 | let mut ctx = InferenceContext::new(db, body, resolver); |
51 | 52 | ||
52 | let signature = func.signature(db); | 53 | match def { |
53 | ctx.collect_fn_signature(&signature); | 54 | DefWithBody::Const(ref c) => ctx.collect_const_signature(&c.signature(db)), |
55 | DefWithBody::Function(ref f) => ctx.collect_fn_signature(&f.signature(db)), | ||
56 | DefWithBody::Static(ref s) => ctx.collect_const_signature(&s.signature(db)), | ||
57 | } | ||
54 | 58 | ||
55 | ctx.infer_body(); | 59 | ctx.infer_body(); |
56 | 60 | ||
@@ -1142,6 +1146,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1142 | ty | 1146 | ty |
1143 | } | 1147 | } |
1144 | 1148 | ||
1149 | fn collect_const_signature(&mut self, signature: &ConstSignature) { | ||
1150 | self.return_ty = self.make_ty(signature.type_ref()); | ||
1151 | } | ||
1152 | |||
1145 | fn collect_fn_signature(&mut self, signature: &FnSignature) { | 1153 | fn collect_fn_signature(&mut self, signature: &FnSignature) { |
1146 | let body = Arc::clone(&self.body); // avoid borrow checker problem | 1154 | let body = Arc::clone(&self.body); // avoid borrow checker problem |
1147 | for (type_ref, pat) in signature.params().iter().zip(body.params()) { | 1155 | 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 943c5499b..0b7c841df 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -11,6 +11,8 @@ use crate::{ | |||
11 | source_binder, | 11 | source_binder, |
12 | mock::MockDatabase, | 12 | mock::MockDatabase, |
13 | ty::display::HirDisplay, | 13 | ty::display::HirDisplay, |
14 | ty::InferenceResult, | ||
15 | expr::BodySourceMap | ||
14 | }; | 16 | }; |
15 | 17 | ||
16 | // These tests compare the inference results for all expressions in a file | 18 | // These tests compare the inference results for all expressions in a file |
@@ -1267,6 +1269,9 @@ fn test() { | |||
1267 | } | 1269 | } |
1268 | "#), | 1270 | "#), |
1269 | @r###" | 1271 | @r###" |
1272 | [52; 53) '1': u32 | ||
1273 | [103; 104) '2': u32 | ||
1274 | [211; 212) '5': u32 | ||
1270 | [227; 305) '{ ...:ID; }': () | 1275 | [227; 305) '{ ...:ID; }': () |
1271 | [237; 238) 'x': u32 | 1276 | [237; 238) 'x': u32 |
1272 | [241; 252) 'Struct::FOO': u32 | 1277 | [241; 252) 'Struct::FOO': u32 |
@@ -1855,6 +1860,9 @@ fn test() { | |||
1855 | } | 1860 | } |
1856 | "#), | 1861 | "#), |
1857 | @r###" | 1862 | @r###" |
1863 | [49; 50) '0': u32 | ||
1864 | [80; 83) '101': u32 | ||
1865 | [126; 128) '99': u32 | ||
1858 | [95; 213) '{ ...NST; }': () | 1866 | [95; 213) '{ ...NST; }': () |
1859 | [138; 139) 'x': {unknown} | 1867 | [138; 139) 'x': {unknown} |
1860 | [142; 153) 'LOCAL_CONST': {unknown} | 1868 | [142; 153) 'LOCAL_CONST': {unknown} |
@@ -1881,6 +1889,10 @@ fn test() { | |||
1881 | } | 1889 | } |
1882 | "#), | 1890 | "#), |
1883 | @r###" | 1891 | @r###" |
1892 | [29; 32) '101': u32 | ||
1893 | [70; 73) '101': u32 | ||
1894 | [118; 120) '99': u32 | ||
1895 | [161; 163) '99': u32 | ||
1884 | [85; 280) '{ ...MUT; }': () | 1896 | [85; 280) '{ ...MUT; }': () |
1885 | [173; 174) 'x': {unknown} | 1897 | [173; 174) 'x': {unknown} |
1886 | [177; 189) 'LOCAL_STATIC': {unknown} | 1898 | [177; 189) 'LOCAL_STATIC': {unknown} |
@@ -2212,6 +2224,24 @@ fn test<T: Iterable<Item=u32>>() { | |||
2212 | ); | 2224 | ); |
2213 | } | 2225 | } |
2214 | 2226 | ||
2227 | #[test] | ||
2228 | fn infer_const_body() { | ||
2229 | assert_snapshot_matches!( | ||
2230 | infer(r#" | ||
2231 | const A: u32 = 1 + 1; | ||
2232 | static B: u64 = { let x = 1; x }; | ||
2233 | "#), | ||
2234 | @r###" | ||
2235 | [16; 17) '1': u32 | ||
2236 | [16; 21) '1 + 1': u32 | ||
2237 | [20; 21) '1': u32 | ||
2238 | [39; 55) '{ let ...1; x }': u64 | ||
2239 | [45; 46) 'x': u64 | ||
2240 | [49; 50) '1': u64 | ||
2241 | [52; 53) 'x': u64"### | ||
2242 | ); | ||
2243 | } | ||
2244 | |||
2215 | fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { | 2245 | fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { |
2216 | let func = source_binder::function_from_position(db, pos).unwrap(); | 2246 | let func = source_binder::function_from_position(db, pos).unwrap(); |
2217 | let body_source_map = func.body_source_map(db); | 2247 | let body_source_map = func.body_source_map(db); |
@@ -2228,11 +2258,11 @@ fn infer(content: &str) -> String { | |||
2228 | let source_file = db.parse(file_id); | 2258 | let source_file = db.parse(file_id); |
2229 | let mut acc = String::new(); | 2259 | let mut acc = String::new(); |
2230 | acc.push_str("\n"); | 2260 | acc.push_str("\n"); |
2231 | for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { | 2261 | |
2232 | let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap(); | 2262 | let mut infer_def = |inference_result: Arc<InferenceResult>, |
2233 | let inference_result = func.infer(&db); | 2263 | body_source_map: Arc<BodySourceMap>| { |
2234 | let body_source_map = func.body_source_map(&db); | ||
2235 | let mut types = Vec::new(); | 2264 | let mut types = Vec::new(); |
2265 | |||
2236 | for (pat, ty) in inference_result.type_of_pat.iter() { | 2266 | for (pat, ty) in inference_result.type_of_pat.iter() { |
2237 | let syntax_ptr = match body_source_map.pat_syntax(pat) { | 2267 | let syntax_ptr = match body_source_map.pat_syntax(pat) { |
2238 | Some(sp) => sp, | 2268 | Some(sp) => sp, |
@@ -2240,6 +2270,7 @@ fn infer(content: &str) -> String { | |||
2240 | }; | 2270 | }; |
2241 | types.push((syntax_ptr, ty)); | 2271 | types.push((syntax_ptr, ty)); |
2242 | } | 2272 | } |
2273 | |||
2243 | for (expr, ty) in inference_result.type_of_expr.iter() { | 2274 | for (expr, ty) in inference_result.type_of_expr.iter() { |
2244 | let syntax_ptr = match body_source_map.expr_syntax(expr) { | 2275 | let syntax_ptr = match body_source_map.expr_syntax(expr) { |
2245 | Some(sp) => sp, | 2276 | Some(sp) => sp, |
@@ -2258,7 +2289,29 @@ fn infer(content: &str) -> String { | |||
2258 | }; | 2289 | }; |
2259 | write!(acc, "{} '{}': {}\n", range, ellipsize(text, 15), ty.display(&db)).unwrap(); | 2290 | write!(acc, "{} '{}': {}\n", range, ellipsize(text, 15), ty.display(&db)).unwrap(); |
2260 | } | 2291 | } |
2292 | }; | ||
2293 | |||
2294 | for const_def in source_file.syntax().descendants().filter_map(ast::ConstDef::cast) { | ||
2295 | let konst = source_binder::const_from_source(&db, file_id, const_def).unwrap(); | ||
2296 | let inference_result = konst.infer(&db); | ||
2297 | let body_source_map = konst.body_source_map(&db); | ||
2298 | infer_def(inference_result, body_source_map) | ||
2261 | } | 2299 | } |
2300 | |||
2301 | for static_def in source_file.syntax().descendants().filter_map(ast::StaticDef::cast) { | ||
2302 | let static_ = source_binder::static_from_source(&db, file_id, static_def).unwrap(); | ||
2303 | let inference_result = static_.infer(&db); | ||
2304 | let body_source_map = static_.body_source_map(&db); | ||
2305 | infer_def(inference_result, body_source_map) | ||
2306 | } | ||
2307 | |||
2308 | for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { | ||
2309 | let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap(); | ||
2310 | let inference_result = func.infer(&db); | ||
2311 | let body_source_map = func.body_source_map(&db); | ||
2312 | infer_def(inference_result, body_source_map) | ||
2313 | } | ||
2314 | |||
2262 | acc.truncate(acc.trim_end().len()); | 2315 | acc.truncate(acc.trim_end().len()); |
2263 | acc | 2316 | acc |
2264 | } | 2317 | } |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index c2e89de15..0376c91c8 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -513,7 +513,11 @@ impl ast::TypeParamsOwner for ConstDef {} | |||
513 | impl ast::AttrsOwner for ConstDef {} | 513 | impl ast::AttrsOwner for ConstDef {} |
514 | impl ast::DocCommentsOwner for ConstDef {} | 514 | impl ast::DocCommentsOwner for ConstDef {} |
515 | impl ast::TypeAscriptionOwner for ConstDef {} | 515 | impl ast::TypeAscriptionOwner for ConstDef {} |
516 | impl ConstDef {} | 516 | impl ConstDef { |
517 | pub fn body(&self) -> Option<&Expr> { | ||
518 | super::child_opt(self) | ||
519 | } | ||
520 | } | ||
517 | 521 | ||
518 | // ContinueExpr | 522 | // ContinueExpr |
519 | #[derive(Debug, PartialEq, Eq, Hash)] | 523 | #[derive(Debug, PartialEq, Eq, Hash)] |
@@ -3364,7 +3368,11 @@ impl ast::TypeParamsOwner for StaticDef {} | |||
3364 | impl ast::AttrsOwner for StaticDef {} | 3368 | impl ast::AttrsOwner for StaticDef {} |
3365 | impl ast::DocCommentsOwner for StaticDef {} | 3369 | impl ast::DocCommentsOwner for StaticDef {} |
3366 | impl ast::TypeAscriptionOwner for StaticDef {} | 3370 | impl ast::TypeAscriptionOwner for StaticDef {} |
3367 | impl StaticDef {} | 3371 | impl StaticDef { |
3372 | pub fn body(&self) -> Option<&Expr> { | ||
3373 | super::child_opt(self) | ||
3374 | } | ||
3375 | } | ||
3368 | 3376 | ||
3369 | // Stmt | 3377 | // Stmt |
3370 | #[derive(Debug, PartialEq, Eq, Hash)] | 3378 | #[derive(Debug, PartialEq, Eq, Hash)] |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index dc0de5808..0a35e25d5 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -315,6 +315,7 @@ Grammar( | |||
315 | "DocCommentsOwner", | 315 | "DocCommentsOwner", |
316 | "TypeAscriptionOwner", | 316 | "TypeAscriptionOwner", |
317 | ], | 317 | ], |
318 | options: [ ["body","Expr"]], | ||
318 | ), | 319 | ), |
319 | "StaticDef": ( | 320 | "StaticDef": ( |
320 | traits: [ | 321 | traits: [ |
@@ -325,6 +326,7 @@ Grammar( | |||
325 | "DocCommentsOwner", | 326 | "DocCommentsOwner", |
326 | "TypeAscriptionOwner", | 327 | "TypeAscriptionOwner", |
327 | ], | 328 | ], |
329 | options: [ ["body","Expr"]], | ||
328 | ), | 330 | ), |
329 | "TypeAliasDef": ( | 331 | "TypeAliasDef": ( |
330 | traits: [ | 332 | traits: [ |