aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-28 18:34:58 +0000
committerAleksey Kladov <[email protected]>2018-12-28 18:34:58 +0000
commit792899587647f5aa0293c2588173677682187c0a (patch)
tree616bcd980ac69eb5070c1e7a6d90ebee71cb7348
parent11122e29b7ec5bc2e08822deaa6fdf9a1cc8ffca (diff)
nameify structs&enums
-rw-r--r--crates/ra_hir/src/adt.rs43
-rw-r--r--crates/ra_hir/src/db.rs9
-rw-r--r--crates/ra_hir/src/name.rs16
-rw-r--r--crates/ra_hir/src/query_definitions.rs8
-rw-r--r--crates/ra_hir/src/ty.rs24
5 files changed, 43 insertions, 57 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs
index 6cdaa1888..e839a5a90 100644
--- a/crates/ra_hir/src/adt.rs
+++ b/crates/ra_hir/src/adt.rs
@@ -1,10 +1,10 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::{SmolStr, ast::{self, NameOwner, StructFlavor}}; 3use ra_syntax::ast::{self, NameOwner, StructFlavor};
4 4
5use crate::{ 5use crate::{
6 DefId, Cancelable, 6 DefId, Cancelable, Name, AsName,
7 db::{HirDatabase}, 7 db::HirDatabase,
8 type_ref::TypeRef, 8 type_ref::TypeRef,
9}; 9};
10 10
@@ -29,26 +29,26 @@ impl Struct {
29 Ok(db.struct_data(self.def_id)?) 29 Ok(db.struct_data(self.def_id)?)
30 } 30 }
31 31
32 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> { 32 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
33 Ok(db.struct_data(self.def_id)?.name.clone()) 33 Ok(db.struct_data(self.def_id)?.name.clone())
34 } 34 }
35} 35}
36 36
37#[derive(Debug, Clone, PartialEq, Eq)] 37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct StructData { 38pub struct StructData {
39 name: Option<SmolStr>, 39 name: Option<Name>,
40 variant_data: Arc<VariantData>, 40 variant_data: Arc<VariantData>,
41} 41}
42 42
43impl StructData { 43impl StructData {
44 pub(crate) fn new(struct_def: ast::StructDef) -> StructData { 44 pub(crate) fn new(struct_def: ast::StructDef) -> StructData {
45 let name = struct_def.name().map(|n| n.text()); 45 let name = struct_def.name().map(|n| n.as_name());
46 let variant_data = VariantData::new(struct_def.flavor()); 46 let variant_data = VariantData::new(struct_def.flavor());
47 let variant_data = Arc::new(variant_data); 47 let variant_data = Arc::new(variant_data);
48 StructData { name, variant_data } 48 StructData { name, variant_data }
49 } 49 }
50 50
51 pub fn name(&self) -> Option<&SmolStr> { 51 pub fn name(&self) -> Option<&Name> {
52 self.name.as_ref() 52 self.name.as_ref()
53 } 53 }
54 54
@@ -70,31 +70,29 @@ impl Enum {
70 self.def_id 70 self.def_id
71 } 71 }
72 72
73 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> { 73 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
74 Ok(db.enum_data(self.def_id)?.name.clone()) 74 Ok(db.enum_data(self.def_id)?.name.clone())
75 } 75 }
76 76
77 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SmolStr, Arc<VariantData>)>> { 77 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> {
78 Ok(db.enum_data(self.def_id)?.variants.clone()) 78 Ok(db.enum_data(self.def_id)?.variants.clone())
79 } 79 }
80} 80}
81 81
82#[derive(Debug, Clone, PartialEq, Eq)] 82#[derive(Debug, Clone, PartialEq, Eq)]
83pub struct EnumData { 83pub struct EnumData {
84 name: Option<SmolStr>, 84 name: Option<Name>,
85 variants: Vec<(SmolStr, Arc<VariantData>)>, 85 variants: Vec<(Name, Arc<VariantData>)>,
86} 86}
87 87
88impl EnumData { 88impl EnumData {
89 pub(crate) fn new(enum_def: ast::EnumDef) -> Self { 89 pub(crate) fn new(enum_def: ast::EnumDef) -> Self {
90 let name = enum_def.name().map(|n| n.text()); 90 let name = enum_def.name().map(|n| n.as_name());
91 let variants = if let Some(evl) = enum_def.variant_list() { 91 let variants = if let Some(evl) = enum_def.variant_list() {
92 evl.variants() 92 evl.variants()
93 .map(|v| { 93 .map(|v| {
94 ( 94 (
95 v.name() 95 v.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
96 .map(|n| n.text())
97 .unwrap_or_else(|| SmolStr::new("[error]")),
98 Arc::new(VariantData::new(v.flavor())), 96 Arc::new(VariantData::new(v.flavor())),
99 ) 97 )
100 }) 98 })
@@ -109,12 +107,12 @@ impl EnumData {
109/// A single field of an enum variant or struct 107/// A single field of an enum variant or struct
110#[derive(Debug, Clone, PartialEq, Eq)] 108#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct StructField { 109pub struct StructField {
112 name: SmolStr, 110 name: Name,
113 type_ref: TypeRef, 111 type_ref: TypeRef,
114} 112}
115 113
116impl StructField { 114impl StructField {
117 pub fn name(&self) -> SmolStr { 115 pub fn name(&self) -> Name {
118 self.name.clone() 116 self.name.clone()
119 } 117 }
120 pub fn type_ref(&self) -> &TypeRef { 118 pub fn type_ref(&self) -> &TypeRef {
@@ -138,7 +136,7 @@ impl VariantData {
138 .fields() 136 .fields()
139 .enumerate() 137 .enumerate()
140 .map(|(i, fd)| StructField { 138 .map(|(i, fd)| StructField {
141 name: SmolStr::new(i.to_string()), 139 name: Name::tuple_field_name(i),
142 type_ref: TypeRef::from_ast_opt(fd.type_ref()), 140 type_ref: TypeRef::from_ast_opt(fd.type_ref()),
143 }) 141 })
144 .collect(); 142 .collect();
@@ -148,10 +146,7 @@ impl VariantData {
148 let fields = fl 146 let fields = fl
149 .fields() 147 .fields()
150 .map(|fd| StructField { 148 .map(|fd| StructField {
151 name: fd 149 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
152 .name()
153 .map(|n| n.text())
154 .unwrap_or_else(|| SmolStr::new("[error]")),
155 type_ref: TypeRef::from_ast_opt(fd.type_ref()), 150 type_ref: TypeRef::from_ast_opt(fd.type_ref()),
156 }) 151 })
157 .collect(); 152 .collect();
@@ -161,10 +156,10 @@ impl VariantData {
161 } 156 }
162 } 157 }
163 158
164 pub(crate) fn get_field_type_ref(&self, field_name: &str) -> Option<&TypeRef> { 159 pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
165 self.fields() 160 self.fields()
166 .iter() 161 .iter()
167 .find(|f| f.name == field_name) 162 .find(|f| f.name == *field_name)
168 .map(|f| &f.type_ref) 163 .map(|f| &f.type_ref)
169 } 164 }
170 165
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index ba43a4502..b41a7429a 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -1,13 +1,10 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::{ 3use ra_syntax::SyntaxNode;
4 SmolStr,
5 SyntaxNode,
6};
7use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable}; 4use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable};
8 5
9use crate::{ 6use crate::{
10 DefLoc, DefId, 7 DefLoc, DefId, Name,
11 SourceFileItems, SourceItemId, 8 SourceFileItems, SourceItemId,
12 query_definitions, 9 query_definitions,
13 FnScopes, 10 FnScopes,
@@ -47,7 +44,7 @@ pub trait HirDatabase: SyntaxDatabase
47 use fn query_definitions::type_for_def; 44 use fn query_definitions::type_for_def;
48 } 45 }
49 46
50 fn type_for_field(def_id: DefId, field: SmolStr) -> Cancelable<Ty> { 47 fn type_for_field(def_id: DefId, field: Name) -> Cancelable<Ty> {
51 type TypeForFieldQuery; 48 type TypeForFieldQuery;
52 use fn query_definitions::type_for_field; 49 use fn query_definitions::type_for_field;
53 } 50 }
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
index e4fc141a6..51e8b3da8 100644
--- a/crates/ra_hir/src/name.rs
+++ b/crates/ra_hir/src/name.rs
@@ -23,6 +23,18 @@ impl fmt::Debug for Name {
23} 23}
24 24
25impl Name { 25impl Name {
26 fn new(text: SmolStr) -> Name {
27 Name { text }
28 }
29
30 pub(crate) fn missing() -> Name {
31 Name::new("[missing name]".into())
32 }
33
34 pub(crate) fn tuple_field_name(idx: usize) -> Name {
35 Name::new(idx.to_string().into())
36 }
37
26 pub(crate) fn as_known_name(&self) -> Option<KnownName> { 38 pub(crate) fn as_known_name(&self) -> Option<KnownName> {
27 let name = match self.text.as_str() { 39 let name = match self.text.as_str() {
28 "isize" => KnownName::Isize, 40 "isize" => KnownName::Isize,
@@ -43,10 +55,6 @@ impl Name {
43 }; 55 };
44 Some(name) 56 Some(name)
45 } 57 }
46
47 fn new(text: SmolStr) -> Name {
48 Name { text }
49 }
50} 58}
51 59
52pub(crate) trait AsName { 60pub(crate) trait AsName {
diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs
index 56e3f7e9d..016d86ee6 100644
--- a/crates/ra_hir/src/query_definitions.rs
+++ b/crates/ra_hir/src/query_definitions.rs
@@ -5,7 +5,7 @@ use std::{
5 5
6use rustc_hash::FxHashMap; 6use rustc_hash::FxHashMap;
7use ra_syntax::{ 7use ra_syntax::{
8 AstNode, SyntaxNode, SmolStr, 8 AstNode, SyntaxNode,
9 ast::{self, NameOwner, ModuleItemOwner} 9 ast::{self, NameOwner, ModuleItemOwner}
10}; 10};
11use ra_db::{SourceRootId, FileId, Cancelable,}; 11use ra_db::{SourceRootId, FileId, Cancelable,};
@@ -39,11 +39,7 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<T
39 ty::type_for_def(db, def_id) 39 ty::type_for_def(db, def_id)
40} 40}
41 41
42pub(super) fn type_for_field( 42pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) -> Cancelable<Ty> {
43 db: &impl HirDatabase,
44 def_id: DefId,
45 field: SmolStr,
46) -> Cancelable<Ty> {
47 ty::type_for_field(db, def_id, field) 43 ty::type_for_field(db, def_id, field)
48} 44}
49 45
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index ad097d1f1..38720b7b5 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -10,13 +10,12 @@ use rustc_hash::{FxHashMap};
10 10
11use ra_db::{LocalSyntaxPtr, Cancelable}; 11use ra_db::{LocalSyntaxPtr, Cancelable};
12use ra_syntax::{ 12use ra_syntax::{
13 SmolStr,
14 ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp}, 13 ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp},
15 SyntaxNodeRef 14 SyntaxNodeRef
16}; 15};
17 16
18use crate::{ 17use crate::{
19 Def, DefId, FnScopes, Module, Function, Struct, Enum, Path, 18 Def, DefId, FnScopes, Module, Function, Struct, Enum, Path, Name, AsName,
20 db::HirDatabase, 19 db::HirDatabase,
21 adt::VariantData, 20 adt::VariantData,
22 type_ref::{TypeRef, Mutability}, 21 type_ref::{TypeRef, Mutability},
@@ -45,7 +44,7 @@ pub enum Ty {
45 /// The DefId of the struct/enum. 44 /// The DefId of the struct/enum.
46 def_id: DefId, 45 def_id: DefId,
47 /// The name, for displaying. 46 /// The name, for displaying.
48 name: SmolStr, 47 name: Name,
49 // later we'll need generic substitutions here 48 // later we'll need generic substitutions here
50 }, 49 },
51 50
@@ -276,18 +275,14 @@ pub fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
276pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> { 275pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
277 Ok(Ty::Adt { 276 Ok(Ty::Adt {
278 def_id: s.def_id(), 277 def_id: s.def_id(),
279 name: s 278 name: s.name(db)?.unwrap_or_else(Name::missing),
280 .name(db)?
281 .unwrap_or_else(|| SmolStr::new("[unnamed struct]")),
282 }) 279 })
283} 280}
284 281
285pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> { 282pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> {
286 Ok(Ty::Adt { 283 Ok(Ty::Adt {
287 def_id: s.def_id(), 284 def_id: s.def_id(),
288 name: s 285 name: s.name(db)?.unwrap_or_else(Name::missing),
289 .name(db)?
290 .unwrap_or_else(|| SmolStr::new("[unnamed enum]")),
291 }) 286 })
292} 287}
293 288
@@ -308,11 +303,7 @@ pub fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> {
308 } 303 }
309} 304}
310 305
311pub(super) fn type_for_field( 306pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) -> Cancelable<Ty> {
312 db: &impl HirDatabase,
313 def_id: DefId,
314 field: SmolStr,
315) -> Cancelable<Ty> {
316 let def = def_id.resolve(db)?; 307 let def = def_id.resolve(db)?;
317 let variant_data = match def { 308 let variant_data = match def {
318 Def::Struct(s) => { 309 Def::Struct(s) => {
@@ -559,14 +550,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
559 ast::Expr::FieldExpr(e) => { 550 ast::Expr::FieldExpr(e) => {
560 let receiver_ty = self.infer_expr_opt(e.expr())?; 551 let receiver_ty = self.infer_expr_opt(e.expr())?;
561 if let Some(nr) = e.name_ref() { 552 if let Some(nr) = e.name_ref() {
562 let text = nr.text();
563 match receiver_ty { 553 match receiver_ty {
564 Ty::Tuple(fields) => { 554 Ty::Tuple(fields) => {
565 let i = text.parse::<usize>().ok(); 555 let i = nr.text().parse::<usize>().ok();
566 i.and_then(|i| fields.get(i).cloned()) 556 i.and_then(|i| fields.get(i).cloned())
567 .unwrap_or(Ty::Unknown) 557 .unwrap_or(Ty::Unknown)
568 } 558 }
569 Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, text)?, 559 Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, nr.as_name())?,
570 _ => Ty::Unknown, 560 _ => Ty::Unknown,
571 } 561 }
572 } else { 562 } else {