diff options
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index f1b98cdd7..bcb705c24 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -7,13 +7,9 @@ use ra_syntax::{ | |||
7 | }; | 7 | }; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
10 | <<<<<<< HEAD | ||
11 | DefId, Name, AsName, Struct, Enum, HirDatabase, DefKind, | ||
12 | ======= | ||
13 | DefId, DefLoc, Name, AsName, Struct, Enum, EnumVariant, | 10 | DefId, DefLoc, Name, AsName, Struct, Enum, EnumVariant, |
14 | VariantData, StructField, HirDatabase, DefKind, | 11 | HirDatabase, DefKind, |
15 | SourceItemId, | 12 | SourceItemId, |
16 | >>>>>>> 95ac72a3... Implement type inference for enum variants | ||
17 | type_ref::TypeRef, | 13 | type_ref::TypeRef, |
18 | }; | 14 | }; |
19 | 15 | ||
@@ -79,13 +75,11 @@ fn get_def_id( | |||
79 | #[derive(Debug, Clone, PartialEq, Eq)] | 75 | #[derive(Debug, Clone, PartialEq, Eq)] |
80 | pub struct EnumData { | 76 | pub struct EnumData { |
81 | pub(crate) name: Option<Name>, | 77 | pub(crate) name: Option<Name>, |
82 | // TODO: keep track of names also since we already have them? | 78 | pub(crate) variants: Vec<(Name, EnumVariant)>, |
83 | // then we won't need additional db lookups | ||
84 | pub(crate) variants: Option<Vec<EnumVariant>>, | ||
85 | } | 79 | } |
86 | 80 | ||
87 | impl EnumData { | 81 | impl EnumData { |
88 | fn new(enum_def: &ast::EnumDef, variants: Option<Vec<EnumVariant>>) -> Self { | 82 | fn new(enum_def: &ast::EnumDef, variants: Vec<(Name, EnumVariant)>) -> Self { |
89 | let name = enum_def.name().map(|n| n.as_name()); | 83 | let name = enum_def.name().map(|n| n.as_name()); |
90 | EnumData { name, variants } | 84 | EnumData { name, variants } |
91 | } | 85 | } |
@@ -98,14 +92,21 @@ impl EnumData { | |||
98 | assert!(def_loc.kind == DefKind::Enum); | 92 | assert!(def_loc.kind == DefKind::Enum); |
99 | let syntax = db.file_item(def_loc.source_item_id); | 93 | let syntax = db.file_item(def_loc.source_item_id); |
100 | let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); | 94 | let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); |
101 | let variants = enum_def.variant_list().map(|vl| { | 95 | let variants = if let Some(vl) = enum_def.variant_list() { |
102 | vl.variants() | 96 | vl.variants() |
103 | .map(|ev| { | 97 | .filter_map(|variant_def| { |
104 | let def_id = get_def_id(db, &def_loc, ev.syntax(), DefKind::EnumVariant); | 98 | let name = variant_def.name().map(|n| n.as_name()); |
105 | EnumVariant::new(def_id) | 99 | |
100 | name.map(|n| { | ||
101 | let def_id = | ||
102 | get_def_id(db, &def_loc, variant_def.syntax(), DefKind::EnumVariant); | ||
103 | (n, EnumVariant::new(def_id)) | ||
104 | }) | ||
106 | }) | 105 | }) |
107 | .collect() | 106 | .collect() |
108 | }); | 107 | } else { |
108 | Vec::new() | ||
109 | }; | ||
109 | Ok(Arc::new(EnumData::new(enum_def, variants))) | 110 | Ok(Arc::new(EnumData::new(enum_def, variants))) |
110 | } | 111 | } |
111 | } | 112 | } |