diff options
Diffstat (limited to 'crates/ra_hir/src/ty/lower.rs')
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index b66b8e4a5..4a9b725d1 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -11,6 +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 | HirDatabase, | 15 | HirDatabase, |
15 | type_ref::TypeRef, | 16 | type_ref::TypeRef, |
16 | name::KnownName, | 17 | name::KnownName, |
@@ -125,6 +126,7 @@ impl Ty { | |||
125 | TypableDef::Enum(e) => e.generic_params(db), | 126 | TypableDef::Enum(e) => e.generic_params(db), |
126 | TypableDef::EnumVariant(var) => var.parent_enum(db).generic_params(db), | 127 | TypableDef::EnumVariant(var) => var.parent_enum(db).generic_params(db), |
127 | TypableDef::TypeAlias(t) => t.generic_params(db), | 128 | TypableDef::TypeAlias(t) => t.generic_params(db), |
129 | TypableDef::Const(_) => GenericParams::default().into(), | ||
128 | }; | 130 | }; |
129 | let parent_param_count = def_generics.count_parent_params(); | 131 | let parent_param_count = def_generics.count_parent_params(); |
130 | substs.extend((0..parent_param_count).map(|_| Ty::Unknown)); | 132 | substs.extend((0..parent_param_count).map(|_| Ty::Unknown)); |
@@ -163,6 +165,7 @@ impl Ty { | |||
163 | TypableDef::Function(_) | 165 | TypableDef::Function(_) |
164 | | TypableDef::Struct(_) | 166 | | TypableDef::Struct(_) |
165 | | TypableDef::Enum(_) | 167 | | TypableDef::Enum(_) |
168 | | TypableDef::Const(_) | ||
166 | | TypableDef::TypeAlias(_) => last, | 169 | | TypableDef::TypeAlias(_) => last, |
167 | TypableDef::EnumVariant(_) => { | 170 | TypableDef::EnumVariant(_) => { |
168 | // the generic args for an enum variant may be either specified | 171 | // the generic args for an enum variant may be either specified |
@@ -197,12 +200,14 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace | |||
197 | (TypableDef::Enum(e), Namespace::Types) => type_for_enum(db, e), | 200 | (TypableDef::Enum(e), Namespace::Types) => type_for_enum(db, e), |
198 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), | 201 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), |
199 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), | 202 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), |
203 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), | ||
200 | 204 | ||
201 | // 'error' cases: | 205 | // 'error' cases: |
202 | (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, | 206 | (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, |
203 | (TypableDef::Enum(_), Namespace::Values) => Ty::Unknown, | 207 | (TypableDef::Enum(_), Namespace::Values) => Ty::Unknown, |
204 | (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, | 208 | (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, |
205 | (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, | 209 | (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, |
210 | (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, | ||
206 | } | 211 | } |
207 | } | 212 | } |
208 | 213 | ||
@@ -233,6 +238,14 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { | |||
233 | Ty::FnDef { def: def.into(), sig, name, substs } | 238 | Ty::FnDef { def: def.into(), sig, name, substs } |
234 | } | 239 | } |
235 | 240 | ||
241 | /// Build the declared type of a const. | ||
242 | fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { | ||
243 | let signature = def.signature(db); | ||
244 | let resolver = def.resolver(db); | ||
245 | |||
246 | Ty::from_hir(db, &resolver, signature.type_ref()) | ||
247 | } | ||
248 | |||
236 | /// Build the type of a tuple struct constructor. | 249 | /// Build the type of a tuple struct constructor. |
237 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | 250 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { |
238 | let var_data = def.variant_data(db); | 251 | let var_data = def.variant_data(db); |
@@ -318,8 +331,9 @@ pub enum TypableDef { | |||
318 | Enum(Enum), | 331 | Enum(Enum), |
319 | EnumVariant(EnumVariant), | 332 | EnumVariant(EnumVariant), |
320 | TypeAlias(TypeAlias), | 333 | TypeAlias(TypeAlias), |
334 | Const(Const), | ||
321 | } | 335 | } |
322 | impl_froms!(TypableDef: Function, Struct, Enum, EnumVariant, TypeAlias); | 336 | impl_froms!(TypableDef: Function, Struct, Enum, EnumVariant, TypeAlias, Const); |
323 | 337 | ||
324 | impl From<ModuleDef> for Option<TypableDef> { | 338 | impl From<ModuleDef> for Option<TypableDef> { |
325 | fn from(def: ModuleDef) -> Option<TypableDef> { | 339 | fn from(def: ModuleDef) -> Option<TypableDef> { |
@@ -329,10 +343,8 @@ impl From<ModuleDef> for Option<TypableDef> { | |||
329 | ModuleDef::Enum(e) => e.into(), | 343 | ModuleDef::Enum(e) => e.into(), |
330 | ModuleDef::EnumVariant(v) => v.into(), | 344 | ModuleDef::EnumVariant(v) => v.into(), |
331 | ModuleDef::TypeAlias(t) => t.into(), | 345 | ModuleDef::TypeAlias(t) => t.into(), |
332 | ModuleDef::Const(_) | 346 | ModuleDef::Const(v) => v.into(), |
333 | | ModuleDef::Static(_) | 347 | ModuleDef::Static(_) | ModuleDef::Module(_) | ModuleDef::Trait(_) => return None, |
334 | | ModuleDef::Module(_) | ||
335 | | ModuleDef::Trait(_) => return None, | ||
336 | }; | 348 | }; |
337 | Some(res) | 349 | Some(res) |
338 | } | 350 | } |