diff options
Diffstat (limited to 'crates/ra_hir/src/ty/lower.rs')
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 8a8cc2a24..a1a2d0f6b 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -9,9 +9,7 @@ use std::sync::Arc; | |||
9 | use std::iter; | 9 | use std::iter; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | Function, Struct, StructField, Enum, EnumVariant, Path, | 12 | Function, Struct, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static, |
13 | ModuleDef, TypeAlias, | ||
14 | Const, Static, | ||
15 | HirDatabase, | 13 | HirDatabase, |
16 | type_ref::TypeRef, | 14 | type_ref::TypeRef, |
17 | name::KnownName, | 15 | name::KnownName, |
@@ -19,7 +17,10 @@ use crate::{ | |||
19 | resolve::{Resolver, Resolution}, | 17 | resolve::{Resolver, Resolution}, |
20 | path::{PathSegment, GenericArg}, | 18 | path::{PathSegment, GenericArg}, |
21 | generics::{HasGenericParams}, | 19 | generics::{HasGenericParams}, |
22 | adt::VariantDef, Trait, generics::{ WherePredicate, GenericDef} | 20 | adt::VariantDef, |
21 | Trait, | ||
22 | generics::{WherePredicate, GenericDef}, | ||
23 | ty::AdtDef, | ||
23 | }; | 24 | }; |
24 | use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef, GenericPredicate}; | 25 | use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef, GenericPredicate}; |
25 | 26 | ||
@@ -288,9 +289,9 @@ impl TraitRef { | |||
288 | pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty { | 289 | pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty { |
289 | match (def, ns) { | 290 | match (def, ns) { |
290 | (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f), | 291 | (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f), |
291 | (TypableDef::Struct(s), Namespace::Types) => type_for_struct(db, s), | 292 | (TypableDef::Struct(s), Namespace::Types) => type_for_adt(db, s), |
292 | (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s), | 293 | (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s), |
293 | (TypableDef::Enum(e), Namespace::Types) => type_for_enum(db, e), | 294 | (TypableDef::Enum(e), Namespace::Types) => type_for_adt(db, e), |
294 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), | 295 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), |
295 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), | 296 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), |
296 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), | 297 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), |
@@ -405,7 +406,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { | |||
405 | .iter() | 406 | .iter() |
406 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) | 407 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) |
407 | .collect::<Vec<_>>(); | 408 | .collect::<Vec<_>>(); |
408 | let ret = type_for_struct(db, def); | 409 | let ret = type_for_adt(db, def); |
409 | FnSig::from_params_and_return(params, ret) | 410 | FnSig::from_params_and_return(params, ret) |
410 | } | 411 | } |
411 | 412 | ||
@@ -413,7 +414,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { | |||
413 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | 414 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { |
414 | let var_data = def.variant_data(db); | 415 | let var_data = def.variant_data(db); |
415 | if var_data.fields().is_none() { | 416 | if var_data.fields().is_none() { |
416 | return type_for_struct(db, def); // Unit struct | 417 | return type_for_adt(db, def); // Unit struct |
417 | } | 418 | } |
418 | let generics = def.generic_params(db); | 419 | let generics = def.generic_params(db); |
419 | let substs = Substs::identity(&generics); | 420 | let substs = Substs::identity(&generics); |
@@ -433,7 +434,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) | |||
433 | .collect::<Vec<_>>(); | 434 | .collect::<Vec<_>>(); |
434 | let generics = def.parent_enum(db).generic_params(db); | 435 | let generics = def.parent_enum(db).generic_params(db); |
435 | let substs = Substs::identity(&generics); | 436 | let substs = Substs::identity(&generics); |
436 | let ret = type_for_enum(db, def.parent_enum(db)).subst(&substs); | 437 | let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs); |
437 | FnSig::from_params_and_return(params, ret) | 438 | FnSig::from_params_and_return(params, ret) |
438 | } | 439 | } |
439 | 440 | ||
@@ -441,21 +442,16 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) | |||
441 | fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { | 442 | fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { |
442 | let var_data = def.variant_data(db); | 443 | let var_data = def.variant_data(db); |
443 | if var_data.fields().is_none() { | 444 | if var_data.fields().is_none() { |
444 | return type_for_enum(db, def.parent_enum(db)); // Unit variant | 445 | return type_for_adt(db, def.parent_enum(db)); // Unit variant |
445 | } | 446 | } |
446 | let generics = def.parent_enum(db).generic_params(db); | 447 | let generics = def.parent_enum(db).generic_params(db); |
447 | let substs = Substs::identity(&generics); | 448 | let substs = Substs::identity(&generics); |
448 | Ty::apply(TypeCtor::FnDef(def.into()), substs) | 449 | Ty::apply(TypeCtor::FnDef(def.into()), substs) |
449 | } | 450 | } |
450 | 451 | ||
451 | fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty { | 452 | fn type_for_adt(db: &impl HirDatabase, adt: impl Into<AdtDef> + HasGenericParams) -> Ty { |
452 | let generics = s.generic_params(db); | 453 | let generics = adt.generic_params(db); |
453 | Ty::apply(TypeCtor::Adt(s.into()), Substs::identity(&generics)) | 454 | Ty::apply(TypeCtor::Adt(adt.into()), Substs::identity(&generics)) |
454 | } | ||
455 | |||
456 | fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty { | ||
457 | let generics = s.generic_params(db); | ||
458 | Ty::apply(TypeCtor::Adt(s.into()), Substs::identity(&generics)) | ||
459 | } | 455 | } |
460 | 456 | ||
461 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { | 457 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { |