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/ra_hir/src/ty/infer.rs | |
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/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 20 |
1 files changed, 14 insertions, 6 deletions
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()) { |