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/ty | |
parent | 18b0bd9bffeeeaf664f4a21894d5bfff51e82b32 (diff) |
Add static type inference
Diffstat (limited to 'crates/ra_hir/src/ty')
-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 |
3 files changed, 24 insertions, 10 deletions
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 | ||