diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index e65f8deb866..40a45b83129 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -30,14 +30,14 @@ pub fn struct_data(&self, db: &impl HirDatabase) -> Cancelable> Ok(db.struct_data(self.def_id)?) } - pub fn name(&self, db: &impl HirDatabase) -> Cancelable { + pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.struct_data(self.def_id)?.name.clone()) } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct StructData { - name: SmolStr, + name: Option, variant_data: Arc, } @@ -47,17 +47,14 @@ pub(crate) fn new( module: &Module, struct_def: ast::StructDef, ) -> Cancelable { - let name = struct_def - .name() - .map(|n| n.text()) - .unwrap_or(SmolStr::new("[error]")); + let name = struct_def.name().map(|n| n.text()); let variant_data = VariantData::new(db, module, struct_def.flavor())?; let variant_data = Arc::new(variant_data); Ok(StructData { name, variant_data }) } - pub fn name(&self) -> &SmolStr { - &self.name + pub fn name(&self) -> Option<&SmolStr> { + self.name.as_ref() } pub fn variant_data(&self) -> &Arc { @@ -78,14 +75,14 @@ pub fn def_id(&self) -> DefId { self.def_id } - pub fn name(&self, db: &impl HirDatabase) -> Cancelable { + pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.enum_data(self.def_id)?.name.clone()) } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { - name: SmolStr, + name: Option, variants: Vec<(SmolStr, Arc)>, } @@ -95,10 +92,7 @@ pub(crate) fn new( module: &Module, enum_def: ast::EnumDef, ) -> Cancelable { - let name = enum_def - .name() - .map(|n| n.text()) - .unwrap_or(SmolStr::new("[error]")); + let name = enum_def.name().map(|n| n.text()); let variants = if let Some(evl) = enum_def.variant_list() { evl.variants() .map(|v| { diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 7b8dbe6b759..3674688ef4c 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -16,7 +16,7 @@ }; use crate::{ - Def, DefId, FnScopes, Module, Function, Struct, Path, + Def, DefId, FnScopes, Module, Function, Struct, Enum, Path, db::HirDatabase, adt::VariantData, }; @@ -251,7 +251,18 @@ pub fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable { pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable { Ok(Ty::Adt { def_id: s.def_id(), - name: s.name(db)?, + name: s + .name(db)? + .unwrap_or_else(|| SmolStr::new("[unnamed struct]")), + }) +} + +pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable { + Ok(Ty::Adt { + def_id: s.def_id(), + name: s + .name(db)? + .unwrap_or_else(|| SmolStr::new("[unnamed enum]")), }) } @@ -264,10 +275,7 @@ pub fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable { } Def::Function(f) => type_for_fn(db, f), Def::Struct(s) => type_for_struct(db, s), - Def::Enum(e) => Ok(Ty::Adt { - def_id, - name: e.name(db)?, - }), + Def::Enum(e) => type_for_enum(db, e), Def::Item => { log::debug!("trying to get type for item of unknown type {:?}", def_id); Ok(Ty::Unknown)