diff options
author | Ville Penttinen <[email protected]> | 2019-02-25 08:21:01 +0000 |
---|---|---|
committer | Ville Penttinen <[email protected]> | 2019-02-25 08:55:23 +0000 |
commit | 29f93a79069cf929fbc6d4efa194a0ab18bb1f45 (patch) | |
tree | 84c7e7bbff03b649287bb55c8c9488e4c6597068 /crates/ra_hir/src | |
parent | 18b0bd9bffeeeaf664f4a21894d5bfff51e82b32 (diff) |
Add static type inference
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/konst.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/snapshots/tests__infer_static.snap | 10 |
6 files changed, 54 insertions, 12 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 9efd902fa..d668b883c 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -612,6 +612,16 @@ impl Static { | |||
612 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { | 612 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { |
613 | self.id.module(db) | 613 | self.id.module(db) |
614 | } | 614 | } |
615 | |||
616 | pub fn signature(&self, db: &impl HirDatabase) -> Arc<ConstSignature> { | ||
617 | db.static_signature(*self) | ||
618 | } | ||
619 | |||
620 | /// Builds a resolver for code inside this item. | ||
621 | pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { | ||
622 | // take the outer scope... | ||
623 | self.module(db).resolver(db) | ||
624 | } | ||
615 | } | 625 | } |
616 | 626 | ||
617 | impl Docs for Static { | 627 | impl Docs for Static { |
diff --git a/crates/ra_hir/src/code_model_impl/konst.rs b/crates/ra_hir/src/code_model_impl/konst.rs index ee03e3374..d7722ccc2 100644 --- a/crates/ra_hir/src/code_model_impl/konst.rs +++ b/crates/ra_hir/src/code_model_impl/konst.rs | |||
@@ -3,7 +3,7 @@ use std::sync::Arc; | |||
3 | use ra_syntax::ast::{NameOwner}; | 3 | use ra_syntax::ast::{NameOwner}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | Name, AsName, Const, ConstSignature, | 6 | Name, AsName, Const, ConstSignature, Static, |
7 | type_ref::{TypeRef}, | 7 | type_ref::{TypeRef}, |
8 | PersistentHirDatabase, | 8 | PersistentHirDatabase, |
9 | }; | 9 | }; |
@@ -23,4 +23,19 @@ impl ConstSignature { | |||
23 | 23 | ||
24 | Arc::new(sig) | 24 | Arc::new(sig) |
25 | } | 25 | } |
26 | |||
27 | pub(crate) fn static_signature_query( | ||
28 | db: &impl PersistentHirDatabase, | ||
29 | konst: Static, | ||
30 | ) -> Arc<ConstSignature> { | ||
31 | let (_, node) = konst.source(db); | ||
32 | |||
33 | let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
34 | |||
35 | let type_ref = TypeRef::from_ast_opt(node.type_ref()); | ||
36 | |||
37 | let sig = ConstSignature { name, type_ref }; | ||
38 | |||
39 | Arc::new(sig) | ||
40 | } | ||
26 | } | 41 | } |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 2be47c160..ec848f1b2 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -8,7 +8,7 @@ use crate::{ | |||
8 | SourceFileItems, SourceItemId, Crate, Module, HirInterner, | 8 | SourceFileItems, SourceItemId, Crate, Module, HirInterner, |
9 | Function, FnSignature, ExprScopes, TypeAlias, | 9 | Function, FnSignature, ExprScopes, TypeAlias, |
10 | Struct, Enum, StructField, | 10 | Struct, Enum, StructField, |
11 | Const, ConstSignature, | 11 | Const, ConstSignature, Static, |
12 | macros::MacroExpansion, | 12 | macros::MacroExpansion, |
13 | module_tree::ModuleTree, | 13 | module_tree::ModuleTree, |
14 | nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}}, | 14 | nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}}, |
@@ -86,6 +86,9 @@ pub trait PersistentHirDatabase: SourceDatabase + AsRef<HirInterner> { | |||
86 | 86 | ||
87 | #[salsa::invoke(crate::ConstSignature::const_signature_query)] | 87 | #[salsa::invoke(crate::ConstSignature::const_signature_query)] |
88 | fn const_signature(&self, konst: Const) -> Arc<ConstSignature>; | 88 | fn const_signature(&self, konst: Const) -> Arc<ConstSignature>; |
89 | |||
90 | #[salsa::invoke(crate::ConstSignature::static_signature_query)] | ||
91 | fn static_signature(&self, konst: Static) -> Arc<ConstSignature>; | ||
89 | } | 92 | } |
90 | 93 | ||
91 | #[salsa::query_group(HirDatabaseStorage)] | 94 | #[salsa::query_group(HirDatabaseStorage)] |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index f5f85308c..5e4d49ffb 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -485,7 +485,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
485 | TypableDef::TypeAlias(_) | 485 | TypableDef::TypeAlias(_) |
486 | | TypableDef::Function(_) | 486 | | TypableDef::Function(_) |
487 | | TypableDef::Enum(_) | 487 | | TypableDef::Enum(_) |
488 | | TypableDef::Const(_) => (Ty::Unknown, None), | 488 | | TypableDef::Const(_) |
489 | | TypableDef::Static(_) => (Ty::Unknown, None), | ||
489 | } | 490 | } |
490 | } | 491 | } |
491 | 492 | ||
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 4a9b725d1..f4e055feb 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -11,7 +11,7 @@ use std::sync::Arc; | |||
11 | use crate::{ | 11 | use crate::{ |
12 | Function, Struct, StructField, Enum, EnumVariant, Path, Name, | 12 | Function, Struct, StructField, Enum, EnumVariant, Path, Name, |
13 | ModuleDef, TypeAlias, | 13 | ModuleDef, TypeAlias, |
14 | Const, | 14 | Const, Static, |
15 | HirDatabase, | 15 | HirDatabase, |
16 | type_ref::TypeRef, | 16 | type_ref::TypeRef, |
17 | name::KnownName, | 17 | name::KnownName, |
@@ -126,7 +126,7 @@ impl Ty { | |||
126 | TypableDef::Enum(e) => e.generic_params(db), | 126 | TypableDef::Enum(e) => e.generic_params(db), |
127 | TypableDef::EnumVariant(var) => var.parent_enum(db).generic_params(db), | 127 | TypableDef::EnumVariant(var) => var.parent_enum(db).generic_params(db), |
128 | TypableDef::TypeAlias(t) => t.generic_params(db), | 128 | TypableDef::TypeAlias(t) => t.generic_params(db), |
129 | TypableDef::Const(_) => GenericParams::default().into(), | 129 | TypableDef::Const(_) | TypableDef::Static(_) => GenericParams::default().into(), |
130 | }; | 130 | }; |
131 | let parent_param_count = def_generics.count_parent_params(); | 131 | let parent_param_count = def_generics.count_parent_params(); |
132 | substs.extend((0..parent_param_count).map(|_| Ty::Unknown)); | 132 | substs.extend((0..parent_param_count).map(|_| Ty::Unknown)); |
@@ -166,6 +166,7 @@ impl Ty { | |||
166 | | TypableDef::Struct(_) | 166 | | TypableDef::Struct(_) |
167 | | TypableDef::Enum(_) | 167 | | TypableDef::Enum(_) |
168 | | TypableDef::Const(_) | 168 | | TypableDef::Const(_) |
169 | | TypableDef::Static(_) | ||
169 | | TypableDef::TypeAlias(_) => last, | 170 | | TypableDef::TypeAlias(_) => last, |
170 | TypableDef::EnumVariant(_) => { | 171 | TypableDef::EnumVariant(_) => { |
171 | // the generic args for an enum variant may be either specified | 172 | // the generic args for an enum variant may be either specified |
@@ -201,6 +202,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace | |||
201 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), | 202 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), |
202 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), | 203 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), |
203 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), | 204 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), |
205 | (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), | ||
204 | 206 | ||
205 | // 'error' cases: | 207 | // 'error' cases: |
206 | (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, | 208 | (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, |
@@ -208,6 +210,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace | |||
208 | (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, | 210 | (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, |
209 | (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, | 211 | (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, |
210 | (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, | 212 | (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, |
213 | (TypableDef::Static(_), Namespace::Types) => Ty::Unknown, | ||
211 | } | 214 | } |
212 | } | 215 | } |
213 | 216 | ||
@@ -246,6 +249,14 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { | |||
246 | Ty::from_hir(db, &resolver, signature.type_ref()) | 249 | Ty::from_hir(db, &resolver, signature.type_ref()) |
247 | } | 250 | } |
248 | 251 | ||
252 | /// Build the declared type of a static. | ||
253 | fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { | ||
254 | let signature = def.signature(db); | ||
255 | let resolver = def.resolver(db); | ||
256 | |||
257 | Ty::from_hir(db, &resolver, signature.type_ref()) | ||
258 | } | ||
259 | |||
249 | /// Build the type of a tuple struct constructor. | 260 | /// Build the type of a tuple struct constructor. |
250 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | 261 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { |
251 | let var_data = def.variant_data(db); | 262 | let var_data = def.variant_data(db); |
@@ -332,8 +343,9 @@ pub enum TypableDef { | |||
332 | EnumVariant(EnumVariant), | 343 | EnumVariant(EnumVariant), |
333 | TypeAlias(TypeAlias), | 344 | TypeAlias(TypeAlias), |
334 | Const(Const), | 345 | Const(Const), |
346 | Static(Static), | ||
335 | } | 347 | } |
336 | impl_froms!(TypableDef: Function, Struct, Enum, EnumVariant, TypeAlias, Const); | 348 | impl_froms!(TypableDef: Function, Struct, Enum, EnumVariant, TypeAlias, Const, Static); |
337 | 349 | ||
338 | impl From<ModuleDef> for Option<TypableDef> { | 350 | impl From<ModuleDef> for Option<TypableDef> { |
339 | fn from(def: ModuleDef) -> Option<TypableDef> { | 351 | fn from(def: ModuleDef) -> Option<TypableDef> { |
@@ -344,7 +356,8 @@ impl From<ModuleDef> for Option<TypableDef> { | |||
344 | ModuleDef::EnumVariant(v) => v.into(), | 356 | ModuleDef::EnumVariant(v) => v.into(), |
345 | ModuleDef::TypeAlias(t) => t.into(), | 357 | ModuleDef::TypeAlias(t) => t.into(), |
346 | ModuleDef::Const(v) => v.into(), | 358 | ModuleDef::Const(v) => v.into(), |
347 | ModuleDef::Static(_) | ModuleDef::Module(_) | ModuleDef::Trait(_) => return None, | 359 | ModuleDef::Static(v) => v.into(), |
360 | ModuleDef::Module(_) | ModuleDef::Trait(_) => return None, | ||
348 | }; | 361 | }; |
349 | Some(res) | 362 | Some(res) |
350 | } | 363 | } |
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_static.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_static.snap index ffe9ca66d..5d90f56ed 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__infer_static.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_static.snap | |||
@@ -1,5 +1,5 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-02-25T07:26:41.480764900Z" | 2 | created: "2019-02-25T08:20:17.807316Z" |
3 | creator: [email protected] | 3 | creator: [email protected] |
4 | source: crates/ra_hir/src/ty/tests.rs | 4 | source: crates/ra_hir/src/ty/tests.rs |
5 | expression: "&result" | 5 | expression: "&result" |
@@ -9,8 +9,8 @@ expression: "&result" | |||
9 | [177; 189) 'LOCAL_STATIC': [unknown] | 9 | [177; 189) 'LOCAL_STATIC': [unknown] |
10 | [199; 200) 'y': [unknown] | 10 | [199; 200) 'y': [unknown] |
11 | [203; 219) 'LOCAL_...IC_MUT': [unknown] | 11 | [203; 219) 'LOCAL_...IC_MUT': [unknown] |
12 | [229; 230) 'z': [unknown] | 12 | [229; 230) 'z': u32 |
13 | [233; 246) 'GLOBAL_STATIC': [unknown] | 13 | [233; 246) 'GLOBAL_STATIC': u32 |
14 | [256; 257) 'w': [unknown] | 14 | [256; 257) 'w': u32 |
15 | [260; 277) 'GLOBAL...IC_MUT': [unknown] | 15 | [260; 277) 'GLOBAL...IC_MUT': u32 |
16 | 16 | ||