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.rs55
1 files changed, 23 insertions, 32 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index c57e222dd..714eaaae5 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -38,7 +38,7 @@ use crate::{
38 expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat}, 38 expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat},
39 generics::GenericParams, 39 generics::GenericParams,
40 path::GenericArg, 40 path::GenericArg,
41 adt::VariantData, 41 adt::VariantDef,
42}; 42};
43 43
44/// The ID of a type variable. 44/// The ID of a type variable.
@@ -696,28 +696,6 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def: TypableDef) -> Ty {
696 } 696 }
697} 697}
698 698
699#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
700pub enum VariantDef {
701 Struct(Struct),
702 EnumVariant(EnumVariant),
703}
704impl_froms!(VariantDef: Struct, EnumVariant);
705
706impl VariantDef {
707 pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
708 match self {
709 VariantDef::Struct(it) => it.field(db, name),
710 VariantDef::EnumVariant(it) => it.field(db, name),
711 }
712 }
713 pub(crate) fn variant_data(self, db: &impl HirDatabase) -> Arc<VariantData> {
714 match self {
715 VariantDef::Struct(it) => it.variant_data(db),
716 VariantDef::EnumVariant(it) => it.variant_data(db),
717 }
718 }
719}
720
721pub(super) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { 699pub(super) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
722 let parent_def = field.parent_def(db); 700 let parent_def = field.parent_def(db);
723 let (generics, module) = match parent_def { 701 let (generics, module) = match parent_def {
@@ -732,8 +710,10 @@ pub(super) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
732/// The result of type inference: A mapping from expressions and patterns to types. 710/// The result of type inference: A mapping from expressions and patterns to types.
733#[derive(Clone, PartialEq, Eq, Debug)] 711#[derive(Clone, PartialEq, Eq, Debug)]
734pub struct InferenceResult { 712pub struct InferenceResult {
735 /// For each method call expr, record the function it resolved to. 713 /// For each method call expr, records the function it resolves to.
736 method_resolutions: FxHashMap<ExprId, Function>, 714 method_resolutions: FxHashMap<ExprId, Function>,
715 /// For each field access expr, records the field it resolves to.
716 field_resolutions: FxHashMap<ExprId, StructField>,
737 type_of_expr: ArenaMap<ExprId, Ty>, 717 type_of_expr: ArenaMap<ExprId, Ty>,
738 type_of_pat: ArenaMap<PatId, Ty>, 718 type_of_pat: ArenaMap<PatId, Ty>,
739} 719}
@@ -742,6 +722,9 @@ impl InferenceResult {
742 pub fn method_resolution(&self, expr: ExprId) -> Option<Function> { 722 pub fn method_resolution(&self, expr: ExprId) -> Option<Function> {
743 self.method_resolutions.get(&expr).map(|it| *it) 723 self.method_resolutions.get(&expr).map(|it| *it)
744 } 724 }
725 pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> {
726 self.field_resolutions.get(&expr).map(|it| *it)
727 }
745} 728}
746 729
747impl Index<ExprId> for InferenceResult { 730impl Index<ExprId> for InferenceResult {
@@ -770,6 +753,7 @@ struct InferenceContext<'a, D: HirDatabase> {
770 impl_block: Option<ImplBlock>, 753 impl_block: Option<ImplBlock>,
771 var_unification_table: InPlaceUnificationTable<TypeVarId>, 754 var_unification_table: InPlaceUnificationTable<TypeVarId>,
772 method_resolutions: FxHashMap<ExprId, Function>, 755 method_resolutions: FxHashMap<ExprId, Function>,
756 field_resolutions: FxHashMap<ExprId, StructField>,
773 type_of_expr: ArenaMap<ExprId, Ty>, 757 type_of_expr: ArenaMap<ExprId, Ty>,
774 type_of_pat: ArenaMap<PatId, Ty>, 758 type_of_pat: ArenaMap<PatId, Ty>,
775 /// The return type of the function being inferred. 759 /// The return type of the function being inferred.
@@ -861,6 +845,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
861 ) -> Self { 845 ) -> Self {
862 InferenceContext { 846 InferenceContext {
863 method_resolutions: FxHashMap::default(), 847 method_resolutions: FxHashMap::default(),
848 field_resolutions: FxHashMap::default(),
864 type_of_expr: ArenaMap::default(), 849 type_of_expr: ArenaMap::default(),
865 type_of_pat: ArenaMap::default(), 850 type_of_pat: ArenaMap::default(),
866 var_unification_table: InPlaceUnificationTable::new(), 851 var_unification_table: InPlaceUnificationTable::new(),
@@ -886,6 +871,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
886 } 871 }
887 InferenceResult { 872 InferenceResult {
888 method_resolutions: mem::replace(&mut self.method_resolutions, Default::default()), 873 method_resolutions: mem::replace(&mut self.method_resolutions, Default::default()),
874 field_resolutions: mem::replace(&mut self.field_resolutions, Default::default()),
889 type_of_expr: expr_types, 875 type_of_expr: expr_types,
890 type_of_pat: pat_types, 876 type_of_pat: pat_types,
891 } 877 }
@@ -899,6 +885,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
899 self.method_resolutions.insert(expr, func); 885 self.method_resolutions.insert(expr, func);
900 } 886 }
901 887
888 fn write_field_resolution(&mut self, expr: ExprId, field: StructField) {
889 self.field_resolutions.insert(expr, field);
890 }
891
902 fn write_pat_ty(&mut self, pat: PatId, ty: Ty) { 892 fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {
903 self.type_of_pat.insert(pat, ty); 893 self.type_of_pat.insert(pat, ty);
904 } 894 }
@@ -1251,9 +1241,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1251 ty 1241 ty
1252 } 1242 }
1253 1243
1254 fn infer_expr(&mut self, expr: ExprId, expected: &Expectation) -> Ty { 1244 fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
1255 let body = Arc::clone(&self.body); // avoid borrow checker problem 1245 let body = Arc::clone(&self.body); // avoid borrow checker problem
1256 let ty = match &body[expr] { 1246 let ty = match &body[tgt_expr] {
1257 Expr::Missing => Ty::Unknown, 1247 Expr::Missing => Ty::Unknown,
1258 Expr::If { 1248 Expr::If {
1259 condition, 1249 condition,
@@ -1344,7 +1334,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1344 let resolved = receiver_ty.clone().lookup_method(self.db, method_name); 1334 let resolved = receiver_ty.clone().lookup_method(self.db, method_name);
1345 let method_ty = match resolved { 1335 let method_ty = match resolved {
1346 Some(func) => { 1336 Some(func) => {
1347 self.write_method_resolution(expr, func); 1337 self.write_method_resolution(tgt_expr, func);
1348 self.db.type_for_def(func.into()) 1338 self.db.type_for_def(func.into())
1349 } 1339 }
1350 None => Ty::Unknown, 1340 None => Ty::Unknown,
@@ -1389,7 +1379,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1389 1379
1390 expected.ty 1380 expected.ty
1391 } 1381 }
1392 Expr::Path(p) => self.infer_path_expr(expr, p).unwrap_or(Ty::Unknown), 1382 Expr::Path(p) => self.infer_path_expr(tgt_expr, p).unwrap_or(Ty::Unknown),
1393 Expr::Continue => Ty::Never, 1383 Expr::Continue => Ty::Never,
1394 Expr::Break { expr } => { 1384 Expr::Break { expr } => {
1395 if let Some(expr) = expr { 1385 if let Some(expr) = expr {
@@ -1436,9 +1426,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1436 def_id: AdtDef::Struct(s), 1426 def_id: AdtDef::Struct(s),
1437 ref substs, 1427 ref substs,
1438 .. 1428 ..
1439 } => s 1429 } => s.field(self.db, name).map(|field| {
1440 .field(self.db, name) 1430 self.write_field_resolution(tgt_expr, field);
1441 .map(|field| field.ty(self.db).subst(substs)), 1431 field.ty(self.db).subst(substs)
1432 }),
1442 _ => None, 1433 _ => None,
1443 }) 1434 })
1444 .unwrap_or(Ty::Unknown); 1435 .unwrap_or(Ty::Unknown);
@@ -1545,7 +1536,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1545 let ty = self.insert_type_vars_shallow(ty); 1536 let ty = self.insert_type_vars_shallow(ty);
1546 self.unify(&ty, &expected.ty); 1537 self.unify(&ty, &expected.ty);
1547 let ty = self.resolve_ty_as_possible(ty); 1538 let ty = self.resolve_ty_as_possible(ty);
1548 self.write_expr_ty(expr, ty.clone()); 1539 self.write_expr_ty(tgt_expr, ty.clone());
1549 ty 1540 ty
1550 } 1541 }
1551 1542