diff options
author | Aleksey Kladov <[email protected]> | 2019-11-24 19:44:24 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-24 19:44:24 +0000 |
commit | 586acef5283e78f19e5e9daf4e8aac40e2a9ff16 (patch) | |
tree | 7daaf3dc8595dc8f431e84db6469c7fa02f6e680 | |
parent | 09389ed1d43c244352cecbd78c8685480d60157d (diff) |
Simplify ADT fields
-rw-r--r-- | crates/ra_arena/src/lib.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir_def/src/adt.rs | 14 |
5 files changed, 29 insertions, 26 deletions
diff --git a/crates/ra_arena/src/lib.rs b/crates/ra_arena/src/lib.rs index 3ec8d3b60..984e1eec3 100644 --- a/crates/ra_arena/src/lib.rs +++ b/crates/ra_arena/src/lib.rs | |||
@@ -37,7 +37,7 @@ impl fmt::Display for RawId { | |||
37 | } | 37 | } |
38 | 38 | ||
39 | #[derive(Clone, PartialEq, Eq)] | 39 | #[derive(Clone, PartialEq, Eq)] |
40 | pub struct Arena<ID: ArenaId, T> { | 40 | pub struct Arena<ID, T> { |
41 | data: Vec<T>, | 41 | data: Vec<T>, |
42 | _ty: PhantomData<ID>, | 42 | _ty: PhantomData<ID>, |
43 | } | 43 | } |
@@ -67,6 +67,12 @@ pub trait ArenaId { | |||
67 | fn into_raw(self) -> RawId; | 67 | fn into_raw(self) -> RawId; |
68 | } | 68 | } |
69 | 69 | ||
70 | impl<ID, T> Arena<ID, T> { | ||
71 | pub const fn new() -> Arena<ID, T> { | ||
72 | Arena { data: Vec::new(), _ty: PhantomData } | ||
73 | } | ||
74 | } | ||
75 | |||
70 | impl<ID: ArenaId, T> Arena<ID, T> { | 76 | impl<ID: ArenaId, T> Arena<ID, T> { |
71 | pub fn len(&self) -> usize { | 77 | pub fn len(&self) -> usize { |
72 | self.data.len() | 78 | self.data.len() |
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 96a5cc857..ab0544a4c 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -301,7 +301,7 @@ pub enum FieldSource { | |||
301 | 301 | ||
302 | impl StructField { | 302 | impl StructField { |
303 | pub fn name(&self, db: &impl HirDatabase) -> Name { | 303 | pub fn name(&self, db: &impl HirDatabase) -> Name { |
304 | self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() | 304 | self.parent.variant_data(db).fields()[self.id].name.clone() |
305 | } | 305 | } |
306 | 306 | ||
307 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 307 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { |
@@ -335,8 +335,7 @@ impl Struct { | |||
335 | db.struct_data(self.id.into()) | 335 | db.struct_data(self.id.into()) |
336 | .variant_data | 336 | .variant_data |
337 | .fields() | 337 | .fields() |
338 | .into_iter() | 338 | .iter() |
339 | .flat_map(|it| it.iter()) | ||
340 | .map(|(id, _)| StructField { parent: self.into(), id }) | 339 | .map(|(id, _)| StructField { parent: self.into(), id }) |
341 | .collect() | 340 | .collect() |
342 | } | 341 | } |
@@ -345,8 +344,7 @@ impl Struct { | |||
345 | db.struct_data(self.id.into()) | 344 | db.struct_data(self.id.into()) |
346 | .variant_data | 345 | .variant_data |
347 | .fields() | 346 | .fields() |
348 | .into_iter() | 347 | .iter() |
349 | .flat_map(|it| it.iter()) | ||
350 | .find(|(_id, data)| data.name == *name) | 348 | .find(|(_id, data)| data.name == *name) |
351 | .map(|(id, _)| StructField { parent: self.into(), id }) | 349 | .map(|(id, _)| StructField { parent: self.into(), id }) |
352 | } | 350 | } |
@@ -443,8 +441,7 @@ impl EnumVariant { | |||
443 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { | 441 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { |
444 | self.variant_data(db) | 442 | self.variant_data(db) |
445 | .fields() | 443 | .fields() |
446 | .into_iter() | 444 | .iter() |
447 | .flat_map(|it| it.iter()) | ||
448 | .map(|(id, _)| StructField { parent: self.into(), id }) | 445 | .map(|(id, _)| StructField { parent: self.into(), id }) |
449 | .collect() | 446 | .collect() |
450 | } | 447 | } |
@@ -452,8 +449,7 @@ impl EnumVariant { | |||
452 | pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { | 449 | pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { |
453 | self.variant_data(db) | 450 | self.variant_data(db) |
454 | .fields() | 451 | .fields() |
455 | .into_iter() | 452 | .iter() |
456 | .flat_map(|it| it.iter()) | ||
457 | .find(|(_id, data)| data.name == *name) | 453 | .find(|(_id, data)| data.name == *name) |
458 | .map(|(id, _)| StructField { parent: self.into(), id }) | 454 | .map(|(id, _)| StructField { parent: self.into(), id }) |
459 | } | 455 | } |
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 72d059a27..1e7c22774 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -200,8 +200,7 @@ impl FromSource for StructField { | |||
200 | variant_def | 200 | variant_def |
201 | .variant_data(db) | 201 | .variant_data(db) |
202 | .fields() | 202 | .fields() |
203 | .into_iter() | 203 | .iter() |
204 | .flat_map(|it| it.iter()) | ||
205 | .map(|(id, _)| StructField { parent: variant_def, id }) | 204 | .map(|(id, _)| StructField { parent: variant_def, id }) |
206 | .find(|f| f.source(db) == src) | 205 | .find(|f| f.source(db) == src) |
207 | } | 206 | } |
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 2272510e8..2b40cb07d 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -557,7 +557,7 @@ pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { | |||
557 | VariantDef::EnumVariant(it) => it.parent.id.resolver(db), | 557 | VariantDef::EnumVariant(it) => it.parent.id.resolver(db), |
558 | }; | 558 | }; |
559 | let var_data = parent_def.variant_data(db); | 559 | let var_data = parent_def.variant_data(db); |
560 | let type_ref = &var_data.fields().unwrap()[field.id].type_ref; | 560 | let type_ref = &var_data.fields()[field.id].type_ref; |
561 | Ty::from_hir(db, &resolver, type_ref) | 561 | Ty::from_hir(db, &resolver, type_ref) |
562 | } | 562 | } |
563 | 563 | ||
@@ -696,10 +696,7 @@ impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> { | |||
696 | 696 | ||
697 | fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { | 697 | fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { |
698 | let struct_data = db.struct_data(def.id.into()); | 698 | let struct_data = db.struct_data(def.id.into()); |
699 | let fields = match struct_data.variant_data.fields() { | 699 | let fields = struct_data.variant_data.fields(); |
700 | Some(fields) => fields, | ||
701 | None => panic!("fn_sig_for_struct_constructor called on unit struct"), | ||
702 | }; | ||
703 | let resolver = def.id.resolver(db); | 700 | let resolver = def.id.resolver(db); |
704 | let params = fields | 701 | let params = fields |
705 | .iter() | 702 | .iter() |
@@ -712,7 +709,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { | |||
712 | /// Build the type of a tuple struct constructor. | 709 | /// Build the type of a tuple struct constructor. |
713 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | 710 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { |
714 | let struct_data = db.struct_data(def.id.into()); | 711 | let struct_data = db.struct_data(def.id.into()); |
715 | if struct_data.variant_data.fields().is_none() { | 712 | if struct_data.variant_data.is_unit() { |
716 | return type_for_adt(db, def); // Unit struct | 713 | return type_for_adt(db, def); // Unit struct |
717 | } | 714 | } |
718 | let generics = db.generic_params(def.id.into()); | 715 | let generics = db.generic_params(def.id.into()); |
@@ -722,10 +719,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | |||
722 | 719 | ||
723 | fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { | 720 | fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { |
724 | let var_data = def.variant_data(db); | 721 | let var_data = def.variant_data(db); |
725 | let fields = match var_data.fields() { | 722 | let fields = var_data.fields(); |
726 | Some(fields) => fields, | ||
727 | None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), | ||
728 | }; | ||
729 | let resolver = def.parent.id.resolver(db); | 723 | let resolver = def.parent.id.resolver(db); |
730 | let params = fields | 724 | let params = fields |
731 | .iter() | 725 | .iter() |
@@ -740,7 +734,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) | |||
740 | /// Build the type of a tuple enum variant constructor. | 734 | /// Build the type of a tuple enum variant constructor. |
741 | fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { | 735 | fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { |
742 | let var_data = def.variant_data(db); | 736 | let var_data = def.variant_data(db); |
743 | if var_data.fields().is_none() { | 737 | if var_data.is_unit() { |
744 | return type_for_adt(db, def.parent_enum(db)); // Unit variant | 738 | return type_for_adt(db, def.parent_enum(db)); // Unit variant |
745 | } | 739 | } |
746 | let generics = db.generic_params(def.parent_enum(db).id.into()); | 740 | let generics = db.generic_params(def.parent_enum(db).id.into()); |
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index a43f553aa..c9f30923e 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs | |||
@@ -109,10 +109,18 @@ impl VariantData { | |||
109 | } | 109 | } |
110 | } | 110 | } |
111 | 111 | ||
112 | pub fn fields(&self) -> Option<&Arena<LocalStructFieldId, StructFieldData>> { | 112 | pub fn fields(&self) -> &Arena<LocalStructFieldId, StructFieldData> { |
113 | const EMPTY: &Arena<LocalStructFieldId, StructFieldData> = &Arena::new(); | ||
113 | match &self { | 114 | match &self { |
114 | VariantData::Record(fields) | VariantData::Tuple(fields) => Some(fields), | 115 | VariantData::Record(fields) | VariantData::Tuple(fields) => fields, |
115 | _ => None, | 116 | _ => EMPTY, |
117 | } | ||
118 | } | ||
119 | |||
120 | pub fn is_unit(&self) -> bool { | ||
121 | match self { | ||
122 | VariantData::Unit => true, | ||
123 | _ => false, | ||
116 | } | 124 | } |
117 | } | 125 | } |
118 | } | 126 | } |