diff options
author | Florian Diebold <[email protected]> | 2018-12-25 16:55:50 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2018-12-25 16:55:50 +0000 |
commit | b96d3612390e070936a176571c946ad0cafa69a9 (patch) | |
tree | 8e7bf01bb4db76502c787ccd1935d18dfcc603cc /crates/ra_hir | |
parent | 3e4d41d1e409315ce42cb3c3479236b5e73d0643 (diff) |
Handle structs/enums with missing names a bit better
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 20 |
2 files changed, 22 insertions, 20 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index e65f8deb8..40a45b831 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -30,14 +30,14 @@ impl Struct { | |||
30 | Ok(db.struct_data(self.def_id)?) | 30 | Ok(db.struct_data(self.def_id)?) |
31 | } | 31 | } |
32 | 32 | ||
33 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> { | 33 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> { |
34 | Ok(db.struct_data(self.def_id)?.name.clone()) | 34 | Ok(db.struct_data(self.def_id)?.name.clone()) |
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | #[derive(Debug, Clone, PartialEq, Eq)] | 38 | #[derive(Debug, Clone, PartialEq, Eq)] |
39 | pub struct StructData { | 39 | pub struct StructData { |
40 | name: SmolStr, | 40 | name: Option<SmolStr>, |
41 | variant_data: Arc<VariantData>, | 41 | variant_data: Arc<VariantData>, |
42 | } | 42 | } |
43 | 43 | ||
@@ -47,17 +47,14 @@ impl StructData { | |||
47 | module: &Module, | 47 | module: &Module, |
48 | struct_def: ast::StructDef, | 48 | struct_def: ast::StructDef, |
49 | ) -> Cancelable<StructData> { | 49 | ) -> Cancelable<StructData> { |
50 | let name = struct_def | 50 | let name = struct_def.name().map(|n| n.text()); |
51 | .name() | ||
52 | .map(|n| n.text()) | ||
53 | .unwrap_or(SmolStr::new("[error]")); | ||
54 | let variant_data = VariantData::new(db, module, struct_def.flavor())?; | 51 | let variant_data = VariantData::new(db, module, struct_def.flavor())?; |
55 | let variant_data = Arc::new(variant_data); | 52 | let variant_data = Arc::new(variant_data); |
56 | Ok(StructData { name, variant_data }) | 53 | Ok(StructData { name, variant_data }) |
57 | } | 54 | } |
58 | 55 | ||
59 | pub fn name(&self) -> &SmolStr { | 56 | pub fn name(&self) -> Option<&SmolStr> { |
60 | &self.name | 57 | self.name.as_ref() |
61 | } | 58 | } |
62 | 59 | ||
63 | pub fn variant_data(&self) -> &Arc<VariantData> { | 60 | pub fn variant_data(&self) -> &Arc<VariantData> { |
@@ -78,14 +75,14 @@ impl Enum { | |||
78 | self.def_id | 75 | self.def_id |
79 | } | 76 | } |
80 | 77 | ||
81 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> { | 78 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> { |
82 | Ok(db.enum_data(self.def_id)?.name.clone()) | 79 | Ok(db.enum_data(self.def_id)?.name.clone()) |
83 | } | 80 | } |
84 | } | 81 | } |
85 | 82 | ||
86 | #[derive(Debug, Clone, PartialEq, Eq)] | 83 | #[derive(Debug, Clone, PartialEq, Eq)] |
87 | pub struct EnumData { | 84 | pub struct EnumData { |
88 | name: SmolStr, | 85 | name: Option<SmolStr>, |
89 | variants: Vec<(SmolStr, Arc<VariantData>)>, | 86 | variants: Vec<(SmolStr, Arc<VariantData>)>, |
90 | } | 87 | } |
91 | 88 | ||
@@ -95,10 +92,7 @@ impl EnumData { | |||
95 | module: &Module, | 92 | module: &Module, |
96 | enum_def: ast::EnumDef, | 93 | enum_def: ast::EnumDef, |
97 | ) -> Cancelable<Self> { | 94 | ) -> Cancelable<Self> { |
98 | let name = enum_def | 95 | let name = enum_def.name().map(|n| n.text()); |
99 | .name() | ||
100 | .map(|n| n.text()) | ||
101 | .unwrap_or(SmolStr::new("[error]")); | ||
102 | let variants = if let Some(evl) = enum_def.variant_list() { | 96 | let variants = if let Some(evl) = enum_def.variant_list() { |
103 | evl.variants() | 97 | evl.variants() |
104 | .map(|v| { | 98 | .map(|v| { |
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 7b8dbe6b7..3674688ef 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -16,7 +16,7 @@ use ra_syntax::{ | |||
16 | }; | 16 | }; |
17 | 17 | ||
18 | use crate::{ | 18 | use crate::{ |
19 | Def, DefId, FnScopes, Module, Function, Struct, Path, | 19 | Def, DefId, FnScopes, Module, Function, Struct, Enum, Path, |
20 | db::HirDatabase, | 20 | db::HirDatabase, |
21 | adt::VariantData, | 21 | adt::VariantData, |
22 | }; | 22 | }; |
@@ -251,7 +251,18 @@ pub fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> { | |||
251 | pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> { | 251 | pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> { |
252 | Ok(Ty::Adt { | 252 | Ok(Ty::Adt { |
253 | def_id: s.def_id(), | 253 | def_id: s.def_id(), |
254 | name: s.name(db)?, | 254 | name: s |
255 | .name(db)? | ||
256 | .unwrap_or_else(|| SmolStr::new("[unnamed struct]")), | ||
257 | }) | ||
258 | } | ||
259 | |||
260 | pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> { | ||
261 | Ok(Ty::Adt { | ||
262 | def_id: s.def_id(), | ||
263 | name: s | ||
264 | .name(db)? | ||
265 | .unwrap_or_else(|| SmolStr::new("[unnamed enum]")), | ||
255 | }) | 266 | }) |
256 | } | 267 | } |
257 | 268 | ||
@@ -264,10 +275,7 @@ pub fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> { | |||
264 | } | 275 | } |
265 | Def::Function(f) => type_for_fn(db, f), | 276 | Def::Function(f) => type_for_fn(db, f), |
266 | Def::Struct(s) => type_for_struct(db, s), | 277 | Def::Struct(s) => type_for_struct(db, s), |
267 | Def::Enum(e) => Ok(Ty::Adt { | 278 | Def::Enum(e) => type_for_enum(db, e), |
268 | def_id, | ||
269 | name: e.name(db)?, | ||
270 | }), | ||
271 | Def::Item => { | 279 | Def::Item => { |
272 | log::debug!("trying to get type for item of unknown type {:?}", def_id); | 280 | log::debug!("trying to get type for item of unknown type {:?}", def_id); |
273 | Ok(Ty::Unknown) | 281 | Ok(Ty::Unknown) |