aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2018-12-25 20:40:33 +0000
committerFlorian Diebold <[email protected]>2018-12-25 20:40:33 +0000
commitbc745a139674f289386f3081458793f756cab5b9 (patch)
tree518c38ce87807c76644b512ce0213dd01e43614a /crates/ra_hir/src/ty.rs
parentcdca39706121b2d1734a94938a2372da881e10c6 (diff)
Resolve field types lazily
I.e. not already when getting the HIR for the struct.
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r--crates/ra_hir/src/ty.rs36
1 files changed, 28 insertions, 8 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 11b4caa23..67b523c2c 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -309,6 +309,33 @@ pub fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> {
309 } 309 }
310} 310}
311 311
312pub(super) fn type_for_field(
313 db: &impl HirDatabase,
314 def_id: DefId,
315 field: SmolStr,
316) -> Cancelable<Ty> {
317 let def = def_id.resolve(db)?;
318 let variant_data = match def {
319 Def::Struct(s) => {
320 let variant_data = s.variant_data(db)?;
321 variant_data
322 }
323 // TODO: unions
324 // TODO: enum variants
325 _ => panic!(
326 "trying to get type for field in non-struct/variant {:?}",
327 def_id
328 ),
329 };
330 let module = def_id.module(db)?;
331 let type_ref = if let Some(tr) = variant_data.get_field_type_ref(&field) {
332 tr
333 } else {
334 return Ok(Ty::Unknown);
335 };
336 Ty::from_hir(db, &module, &type_ref)
337}
338
312#[derive(Clone, PartialEq, Eq, Debug)] 339#[derive(Clone, PartialEq, Eq, Debug)]
313pub struct InferenceResult { 340pub struct InferenceResult {
314 type_of: FxHashMap<LocalSyntaxPtr, Ty>, 341 type_of: FxHashMap<LocalSyntaxPtr, Ty>,
@@ -540,14 +567,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
540 i.and_then(|i| fields.get(i).cloned()) 567 i.and_then(|i| fields.get(i).cloned())
541 .unwrap_or(Ty::Unknown) 568 .unwrap_or(Ty::Unknown)
542 } 569 }
543 Ty::Adt { def_id, .. } => { 570 Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, text)?,
544 let field_ty = match def_id.resolve(self.db)? {
545 Def::Struct(s) => s.variant_data(self.db)?.get_field_ty(&text),
546 // TODO unions
547 _ => None,
548 };
549 field_ty.unwrap_or(Ty::Unknown)
550 }
551 _ => Ty::Unknown, 571 _ => Ty::Unknown,
552 } 572 }
553 } else { 573 } else {