aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r--crates/ra_hir/src/ty.rs24
1 files changed, 7 insertions, 17 deletions
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 {