diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-08 15:01:19 +0000 |
---|---|---|
committer | Marcus Klaas de Vries <[email protected]> | 2019-01-10 13:32:56 +0000 |
commit | 978de5cf8bfd2ff82696fc8d5369b41e147431c3 (patch) | |
tree | 27a25a5f2fb20a4afffed97cbb5b678b251d254b /crates/ra_hir/src/ty.rs | |
parent | aca14c591fea40b2f803bbf5f02c1571732348fb (diff) |
Implement type inference for enum variants
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index eb7764f65..18c41a015 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -30,7 +30,7 @@ use join_to_string::join; | |||
30 | use ra_db::Cancelable; | 30 | use ra_db::Cancelable; |
31 | 31 | ||
32 | use crate::{ | 32 | use crate::{ |
33 | Def, DefId, Module, Function, Struct, Enum, Path, Name, ImplBlock, | 33 | Def, DefId, Module, Function, Struct, Enum, EnumVariant, Path, Name, ImplBlock, |
34 | FnSignature, FnScopes, | 34 | FnSignature, FnScopes, |
35 | db::HirDatabase, | 35 | db::HirDatabase, |
36 | type_ref::{TypeRef, Mutability}, | 36 | type_ref::{TypeRef, Mutability}, |
@@ -453,6 +453,12 @@ pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> { | |||
453 | }) | 453 | }) |
454 | } | 454 | } |
455 | 455 | ||
456 | pub fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> { | ||
457 | let enum_parent = ev.parent_enum(db)?; | ||
458 | |||
459 | type_for_enum(db, enum_parent) | ||
460 | } | ||
461 | |||
456 | pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> { | 462 | pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> { |
457 | let def = def_id.resolve(db)?; | 463 | let def = def_id.resolve(db)?; |
458 | match def { | 464 | match def { |
@@ -463,6 +469,7 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<T | |||
463 | Def::Function(f) => type_for_fn(db, f), | 469 | Def::Function(f) => type_for_fn(db, f), |
464 | Def::Struct(s) => type_for_struct(db, s), | 470 | Def::Struct(s) => type_for_struct(db, s), |
465 | Def::Enum(e) => type_for_enum(db, e), | 471 | Def::Enum(e) => type_for_enum(db, e), |
472 | Def::EnumVariant(ev) => type_for_enum_variant(db, ev), | ||
466 | Def::Item => { | 473 | Def::Item => { |
467 | log::debug!("trying to get type for item of unknown type {:?}", def_id); | 474 | log::debug!("trying to get type for item of unknown type {:?}", def_id); |
468 | Ok(Ty::Unknown) | 475 | Ok(Ty::Unknown) |
@@ -477,12 +484,9 @@ pub(super) fn type_for_field( | |||
477 | ) -> Cancelable<Option<Ty>> { | 484 | ) -> Cancelable<Option<Ty>> { |
478 | let def = def_id.resolve(db)?; | 485 | let def = def_id.resolve(db)?; |
479 | let variant_data = match def { | 486 | let variant_data = match def { |
480 | Def::Struct(s) => { | 487 | Def::Struct(s) => s.variant_data(db)?, |
481 | let variant_data = s.variant_data(db)?; | 488 | Def::EnumVariant(ev) => ev.variant_data(db)?, |
482 | variant_data | ||
483 | } | ||
484 | // TODO: unions | 489 | // TODO: unions |
485 | // TODO: enum variants | ||
486 | _ => panic!( | 490 | _ => panic!( |
487 | "trying to get type for field in non-struct/variant {:?}", | 491 | "trying to get type for field in non-struct/variant {:?}", |
488 | def_id | 492 | def_id |
@@ -788,6 +792,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
788 | let ty = type_for_struct(self.db, s)?; | 792 | let ty = type_for_struct(self.db, s)?; |
789 | (ty, Some(def_id)) | 793 | (ty, Some(def_id)) |
790 | } | 794 | } |
795 | Def::EnumVariant(ev) => { | ||
796 | let ty = type_for_enum_variant(self.db, ev)?; | ||
797 | (ty, Some(def_id)) | ||
798 | } | ||
791 | _ => (Ty::Unknown, None), | 799 | _ => (Ty::Unknown, None), |
792 | }) | 800 | }) |
793 | } | 801 | } |