From 586acef5283e78f19e5e9daf4e8aac40e2a9ff16 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 22:44:24 +0300 Subject: Simplify ADT fields --- crates/ra_arena/src/lib.rs | 8 +++++++- crates/ra_hir/src/code_model.rs | 14 +++++--------- crates/ra_hir/src/from_source.rs | 3 +-- crates/ra_hir/src/ty/lower.rs | 16 +++++----------- 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 { } #[derive(Clone, PartialEq, Eq)] -pub struct Arena { +pub struct Arena { data: Vec, _ty: PhantomData, } @@ -67,6 +67,12 @@ pub trait ArenaId { fn into_raw(self) -> RawId; } +impl Arena { + pub const fn new() -> Arena { + Arena { data: Vec::new(), _ty: PhantomData } + } +} + impl Arena { pub fn len(&self) -> usize { 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 { impl StructField { pub fn name(&self, db: &impl HirDatabase) -> Name { - self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() + self.parent.variant_data(db).fields()[self.id].name.clone() } pub fn ty(&self, db: &impl HirDatabase) -> Ty { @@ -335,8 +335,7 @@ impl Struct { db.struct_data(self.id.into()) .variant_data .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .map(|(id, _)| StructField { parent: self.into(), id }) .collect() } @@ -345,8 +344,7 @@ impl Struct { db.struct_data(self.id.into()) .variant_data .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .find(|(_id, data)| data.name == *name) .map(|(id, _)| StructField { parent: self.into(), id }) } @@ -443,8 +441,7 @@ impl EnumVariant { pub fn fields(self, db: &impl HirDatabase) -> Vec { self.variant_data(db) .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .map(|(id, _)| StructField { parent: self.into(), id }) .collect() } @@ -452,8 +449,7 @@ impl EnumVariant { pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option { self.variant_data(db) .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .find(|(_id, data)| data.name == *name) .map(|(id, _)| StructField { parent: self.into(), id }) } 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 { variant_def .variant_data(db) .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .map(|(id, _)| StructField { parent: variant_def, id }) .find(|f| f.source(db) == src) } 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 { VariantDef::EnumVariant(it) => it.parent.id.resolver(db), }; let var_data = parent_def.variant_data(db); - let type_ref = &var_data.fields().unwrap()[field.id].type_ref; + let type_ref = &var_data.fields()[field.id].type_ref; Ty::from_hir(db, &resolver, type_ref) } @@ -696,10 +696,7 @@ impl From> for Uncertain { fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { let struct_data = db.struct_data(def.id.into()); - let fields = match struct_data.variant_data.fields() { - Some(fields) => fields, - None => panic!("fn_sig_for_struct_constructor called on unit struct"), - }; + let fields = struct_data.variant_data.fields(); let resolver = def.id.resolver(db); let params = fields .iter() @@ -712,7 +709,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { /// Build the type of a tuple struct constructor. fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { let struct_data = db.struct_data(def.id.into()); - if struct_data.variant_data.fields().is_none() { + if struct_data.variant_data.is_unit() { return type_for_adt(db, def); // Unit struct } let generics = db.generic_params(def.id.into()); @@ -722,10 +719,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { let var_data = def.variant_data(db); - let fields = match var_data.fields() { - Some(fields) => fields, - None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), - }; + let fields = var_data.fields(); let resolver = def.parent.id.resolver(db); let params = fields .iter() @@ -740,7 +734,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) /// Build the type of a tuple enum variant constructor. fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { let var_data = def.variant_data(db); - if var_data.fields().is_none() { + if var_data.is_unit() { return type_for_adt(db, def.parent_enum(db)); // Unit variant } 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 { } } - pub fn fields(&self) -> Option<&Arena> { + pub fn fields(&self) -> &Arena { + const EMPTY: &Arena = &Arena::new(); match &self { - VariantData::Record(fields) | VariantData::Tuple(fields) => Some(fields), - _ => None, + VariantData::Record(fields) | VariantData::Tuple(fields) => fields, + _ => EMPTY, + } + } + + pub fn is_unit(&self) -> bool { + match self { + VariantData::Unit => true, + _ => false, } } } -- cgit v1.2.3