From 1d14fd17372b42c3343daf6adc9a520fdf5e9810 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 27 Nov 2019 23:22:20 +0300 Subject: Use Name::missing consistently --- crates/ra_hir/src/code_model.rs | 17 +++++++---------- crates/ra_hir_def/src/adt.rs | 18 +++++++++--------- crates/ra_hir_def/src/data.rs | 11 ++++++----- crates/ra_hir_def/src/nameres/collector.rs | 2 +- crates/ra_hir_ty/src/lib.rs | 25 +++++++------------------ crates/ra_ide/src/completion/presentation.rs | 5 +---- crates/ra_ide/src/display/function_signature.rs | 7 ++----- 7 files changed, 33 insertions(+), 52 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9488521a9..38d66c2a7 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -330,7 +330,7 @@ impl Struct { Some(self.module(db).krate()) } - pub fn name(self, db: &impl DefDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Name { db.struct_data(self.id.into()).name.clone() } @@ -371,7 +371,7 @@ pub struct Union { } impl Union { - pub fn name(self, db: &impl DefDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Name { db.union_data(self.id).name.clone() } @@ -420,7 +420,7 @@ impl Enum { Some(self.module(db).krate()) } - pub fn name(self, db: &impl DefDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Name { db.enum_data(self.id).name.clone() } @@ -433,11 +433,8 @@ impl Enum { } pub fn variant(self, db: &impl DefDatabase, name: &Name) -> Option { - db.enum_data(self.id) - .variants - .iter() - .find(|(_id, data)| data.name.as_ref() == Some(name)) - .map(|(id, _)| EnumVariant { parent: self, id }) + let id = db.enum_data(self.id).variant(name)?; + Some(EnumVariant { parent: self, id }) } pub fn ty(self, db: &impl HirDatabase) -> Type { @@ -459,7 +456,7 @@ impl EnumVariant { self.parent } - pub fn name(self, db: &impl DefDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Name { db.enum_data(self.parent.id).variants[self.id].name.clone() } @@ -720,7 +717,7 @@ impl Trait { Module { id: self.id.module(db) } } - pub fn name(self, db: &impl DefDatabase) -> Option { + pub fn name(self, db: &impl DefDatabase) -> Name { db.trait_data(self.id).name.clone() } diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 0cf418d30..3666529b0 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -18,19 +18,19 @@ use crate::{ /// Note that we use `StructData` for unions as well! #[derive(Debug, Clone, PartialEq, Eq)] pub struct StructData { - pub name: Option, + pub name: Name, pub variant_data: Arc, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { - pub name: Option, + pub name: Name, pub variants: Arena, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumVariantData { - pub name: Option, + pub name: Name, pub variant_data: Arc, } @@ -51,14 +51,14 @@ pub struct StructFieldData { impl StructData { pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc { let src = id.source(db); - let name = src.value.name().map(|n| n.as_name()); + let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let variant_data = VariantData::new(src.value.kind()); let variant_data = Arc::new(variant_data); Arc::new(StructData { name, variant_data }) } pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc { let src = id.source(db); - let name = src.value.name().map(|n| n.as_name()); + let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let variant_data = VariantData::new( src.value .record_field_def_list() @@ -73,14 +73,14 @@ impl StructData { impl EnumData { pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc { let src = e.source(db); - let name = src.value.name().map(|n| n.as_name()); + let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let mut trace = Trace::new_for_arena(); lower_enum(&mut trace, &src.value); Arc::new(EnumData { name, variants: trace.into_arena() }) } - pub(crate) fn variant(&self, name: &Name) -> Option { - let (id, _) = self.variants.iter().find(|(_id, data)| data.name.as_ref() == Some(name))?; + pub fn variant(&self, name: &Name) -> Option { + let (id, _) = self.variants.iter().find(|(_id, data)| &data.name == name)?; Some(id) } } @@ -104,7 +104,7 @@ fn lower_enum( trace.alloc( || var.clone(), || EnumVariantData { - name: var.name().map(|it| it.as_name()), + name: var.name().map_or_else(Name::missing, |it| it.as_name()), variant_data: Arc::new(VariantData::new(var.kind())), }, ); diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 813099a05..fee10b237 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -86,7 +86,7 @@ impl TypeAliasData { #[derive(Debug, Clone, PartialEq, Eq)] pub struct TraitData { - pub name: Option, + pub name: Name, pub items: Vec<(Name, AssocItemId)>, pub auto: bool, } @@ -94,7 +94,7 @@ pub struct TraitData { impl TraitData { pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc { let src = tr.source(db); - let name = src.value.name().map(|n| n.as_name()); + let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let auto = src.value.is_auto(); let ast_id_map = db.ast_id_map(src.file_id); @@ -104,7 +104,7 @@ impl TraitData { .impl_items() .map(|item_node| match item_node { ast::ImplItem::FnDef(it) => { - let name = it.name().map(|it| it.as_name()).unwrap_or_else(Name::missing); + let name = it.name().map_or_else(Name::missing, |it| it.as_name()); let def = FunctionLoc { container, ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), @@ -114,7 +114,7 @@ impl TraitData { (name, def) } ast::ImplItem::ConstDef(it) => { - let name = it.name().map(|it| it.as_name()).unwrap_or_else(Name::missing); + let name = it.name().map_or_else(Name::missing, |it| it.as_name()); let def = ConstLoc { container, ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), @@ -124,7 +124,7 @@ impl TraitData { (name, def) } ast::ImplItem::TypeAliasDef(it) => { - let name = it.name().map(|it| it.as_name()).unwrap_or_else(Name::missing); + let name = it.name().map_or_else(Name::missing, |it| it.as_name()); let def = TypeAliasLoc { container, ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), @@ -214,6 +214,7 @@ impl ImplData { #[derive(Debug, Clone, PartialEq, Eq)] pub struct ConstData { + /// const _: () = (); pub name: Option, pub type_ref: TypeRef, } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 603b49738..fd8245113 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -362,7 +362,7 @@ where .variants .iter() .filter_map(|(local_id, variant_data)| { - let name = variant_data.name.clone()?; + let name = variant_data.name.clone(); let variant = EnumVariantId { parent: e, local_id }; let res = Resolution { def: PerNs::both(variant.into(), variant.into()), diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index c9ee34008..b45c8f82f 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -901,12 +901,10 @@ impl HirDisplay for ApplicationTy { let sig = f.db.callable_item_signature(def); let name = match def { CallableDef::FunctionId(ff) => f.db.function_data(ff).name.clone(), - CallableDef::StructId(s) => { - f.db.struct_data(s).name.clone().unwrap_or_else(Name::missing) - } + CallableDef::StructId(s) => f.db.struct_data(s).name.clone(), CallableDef::EnumVariantId(e) => { let enum_data = f.db.enum_data(e.parent); - enum_data.variants[e.local_id].name.clone().unwrap_or_else(Name::missing) + enum_data.variants[e.local_id].name.clone() } }; match def { @@ -929,8 +927,7 @@ impl HirDisplay for ApplicationTy { AdtId::StructId(it) => f.db.struct_data(it).name.clone(), AdtId::UnionId(it) => f.db.union_data(it).name.clone(), AdtId::EnumId(it) => f.db.enum_data(it).name.clone(), - } - .unwrap_or_else(Name::missing); + }; write!(f, "{}", name)?; if self.parameters.len() > 0 { write!(f, "<")?; @@ -943,7 +940,7 @@ impl HirDisplay for ApplicationTy { ContainerId::TraitId(it) => it, _ => panic!("not an associated type"), }; - let trait_name = f.db.trait_data(trait_).name.clone().unwrap_or_else(Name::missing); + let trait_name = f.db.trait_data(trait_).name.clone(); let name = f.db.type_alias_data(type_alias).name.clone(); write!(f, "{}::{}", trait_name, name)?; if self.parameters.len() > 0 { @@ -971,8 +968,7 @@ impl HirDisplay for ProjectionTy { return write!(f, "…"); } - let trait_name = - f.db.trait_data(self.trait_(f.db)).name.clone().unwrap_or_else(Name::missing); + let trait_name = f.db.trait_data(self.trait_(f.db)).name.clone(); write!(f, "<{} as {}", self.parameters[0].display(f.db), trait_name,)?; if self.parameters.len() > 1 { write!(f, "<")?; @@ -1021,14 +1017,7 @@ impl HirDisplay for Ty { // We assume that the self type is $0 (i.e. the // existential) here, which is the only thing that's // possible in actual Rust, and hence don't print it - write!( - f, - "{}", - f.db.trait_data(trait_ref.trait_) - .name - .clone() - .unwrap_or_else(Name::missing) - )?; + write!(f, "{}", f.db.trait_data(trait_ref.trait_).name.clone())?; if trait_ref.substs.len() > 1 { write!(f, "<")?; f.write_joined(&trait_ref.substs[1..], ", ")?; @@ -1088,7 +1077,7 @@ impl TraitRef { } else { write!(f, ": ")?; } - write!(f, "{}", f.db.trait_data(self.trait_).name.clone().unwrap_or_else(Name::missing))?; + write!(f, "{}", f.db.trait_data(self.trait_).name.clone())?; if self.substs.len() > 1 { write!(f, "<")?; f.write_joined(&self.substs[1..], ", ")?; diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 5f056730a..97475fc0b 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -267,10 +267,7 @@ impl Completions { pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { let is_deprecated = is_deprecated(variant, ctx.db); - let name = match variant.name(ctx.db) { - Some(it) => it, - None => return, - }; + let name = variant.name(ctx.db); let detail_types = variant.fields(ctx.db).into_iter().map(|field| field.ty(ctx.db)); let detail = join(detail_types.map(|t| t.display(ctx.db).to_string())) .separator(", ") diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index d96de4e4c..324ad9552 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -93,12 +93,9 @@ impl FunctionSignature { _ => (), }; - let parent_name = match variant.parent_enum(db).name(db) { - Some(name) => name.to_string(), - None => "missing".into(), - }; + let parent_name = variant.parent_enum(db).name(db).to_string(); - let name = format!("{}::{}", parent_name, variant.name(db).unwrap()); + let name = format!("{}::{}", parent_name, variant.name(db)); let params = variant .fields(db) -- cgit v1.2.3