Handle structs/enums with missing names a bit better
This commit is contained in:
parent
3e4d41d1e4
commit
b96d361239
2 changed files with 22 additions and 20 deletions
|
@ -30,14 +30,14 @@ impl Struct {
|
||||||
Ok(db.struct_data(self.def_id)?)
|
Ok(db.struct_data(self.def_id)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> {
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
||||||
Ok(db.struct_data(self.def_id)?.name.clone())
|
Ok(db.struct_data(self.def_id)?.name.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct StructData {
|
pub struct StructData {
|
||||||
name: SmolStr,
|
name: Option<SmolStr>,
|
||||||
variant_data: Arc<VariantData>,
|
variant_data: Arc<VariantData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,17 +47,14 @@ impl StructData {
|
||||||
module: &Module,
|
module: &Module,
|
||||||
struct_def: ast::StructDef,
|
struct_def: ast::StructDef,
|
||||||
) -> Cancelable<StructData> {
|
) -> Cancelable<StructData> {
|
||||||
let name = struct_def
|
let name = struct_def.name().map(|n| n.text());
|
||||||
.name()
|
|
||||||
.map(|n| n.text())
|
|
||||||
.unwrap_or(SmolStr::new("[error]"));
|
|
||||||
let variant_data = VariantData::new(db, module, struct_def.flavor())?;
|
let variant_data = VariantData::new(db, module, struct_def.flavor())?;
|
||||||
let variant_data = Arc::new(variant_data);
|
let variant_data = Arc::new(variant_data);
|
||||||
Ok(StructData { name, variant_data })
|
Ok(StructData { name, variant_data })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &SmolStr {
|
pub fn name(&self) -> Option<&SmolStr> {
|
||||||
&self.name
|
self.name.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn variant_data(&self) -> &Arc<VariantData> {
|
pub fn variant_data(&self) -> &Arc<VariantData> {
|
||||||
|
@ -78,14 +75,14 @@ impl Enum {
|
||||||
self.def_id
|
self.def_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> {
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
||||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
Ok(db.enum_data(self.def_id)?.name.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct EnumData {
|
pub struct EnumData {
|
||||||
name: SmolStr,
|
name: Option<SmolStr>,
|
||||||
variants: Vec<(SmolStr, Arc<VariantData>)>,
|
variants: Vec<(SmolStr, Arc<VariantData>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,10 +92,7 @@ impl EnumData {
|
||||||
module: &Module,
|
module: &Module,
|
||||||
enum_def: ast::EnumDef,
|
enum_def: ast::EnumDef,
|
||||||
) -> Cancelable<Self> {
|
) -> Cancelable<Self> {
|
||||||
let name = enum_def
|
let name = enum_def.name().map(|n| n.text());
|
||||||
.name()
|
|
||||||
.map(|n| n.text())
|
|
||||||
.unwrap_or(SmolStr::new("[error]"));
|
|
||||||
let variants = if let Some(evl) = enum_def.variant_list() {
|
let variants = if let Some(evl) = enum_def.variant_list() {
|
||||||
evl.variants()
|
evl.variants()
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
|
|
|
@ -16,7 +16,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Def, DefId, FnScopes, Module, Function, Struct, Path,
|
Def, DefId, FnScopes, Module, Function, Struct, Enum, Path,
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
adt::VariantData,
|
adt::VariantData,
|
||||||
};
|
};
|
||||||
|
@ -251,7 +251,18 @@ pub fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
|
||||||
pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
|
pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
|
||||||
Ok(Ty::Adt {
|
Ok(Ty::Adt {
|
||||||
def_id: s.def_id(),
|
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<Ty> {
|
||||||
|
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<Ty> {
|
||||||
}
|
}
|
||||||
Def::Function(f) => type_for_fn(db, f),
|
Def::Function(f) => type_for_fn(db, f),
|
||||||
Def::Struct(s) => type_for_struct(db, s),
|
Def::Struct(s) => type_for_struct(db, s),
|
||||||
Def::Enum(e) => Ok(Ty::Adt {
|
Def::Enum(e) => type_for_enum(db, e),
|
||||||
def_id,
|
|
||||||
name: e.name(db)?,
|
|
||||||
}),
|
|
||||||
Def::Item => {
|
Def::Item => {
|
||||||
log::debug!("trying to get type for item of unknown type {:?}", def_id);
|
log::debug!("trying to get type for item of unknown type {:?}", def_id);
|
||||||
Ok(Ty::Unknown)
|
Ok(Ty::Unknown)
|
||||||
|
|
Loading…
Add table
Reference in a new issue