aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/infer/pat.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/infer/pat.rs')
-rw-r--r--crates/ra_hir_ty/src/infer/pat.rs186
1 files changed, 186 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs
new file mode 100644
index 000000000..1ebb36239
--- /dev/null
+++ b/crates/ra_hir_ty/src/infer/pat.rs
@@ -0,0 +1,186 @@
1//! Type inference for patterns.
2
3use std::iter::repeat;
4use std::sync::Arc;
5
6use hir_def::{
7 expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
8 path::Path,
9 type_ref::Mutability,
10};
11use hir_expand::name::Name;
12use test_utils::tested_by;
13
14use super::{BindingMode, InferenceContext};
15use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor, TypeWalk};
16
17impl<'a, D: HirDatabase> InferenceContext<'a, D> {
18 fn infer_tuple_struct_pat(
19 &mut self,
20 path: Option<&Path>,
21 subpats: &[PatId],
22 expected: &Ty,
23 default_bm: BindingMode,
24 ) -> Ty {
25 let (ty, def) = self.resolve_variant(path);
26 let var_data = def.map(|it| variant_data(self.db, it));
27 self.unify(&ty, expected);
28
29 let substs = ty.substs().unwrap_or_else(Substs::empty);
30
31 let field_tys = def.map(|it| self.db.field_types(it.into())).unwrap_or_default();
32
33 for (i, &subpat) in subpats.iter().enumerate() {
34 let expected_ty = var_data
35 .as_ref()
36 .and_then(|d| d.field(&Name::new_tuple_field(i)))
37 .map_or(Ty::Unknown, |field| field_tys[field].clone())
38 .subst(&substs);
39 let expected_ty = self.normalize_associated_types_in(expected_ty);
40 self.infer_pat(subpat, &expected_ty, default_bm);
41 }
42
43 ty
44 }
45
46 fn infer_record_pat(
47 &mut self,
48 path: Option<&Path>,
49 subpats: &[RecordFieldPat],
50 expected: &Ty,
51 default_bm: BindingMode,
52 id: PatId,
53 ) -> Ty {
54 let (ty, def) = self.resolve_variant(path);
55 let var_data = def.map(|it| variant_data(self.db, it));
56 if let Some(variant) = def {
57 self.write_variant_resolution(id.into(), variant);
58 }
59
60 self.unify(&ty, expected);
61
62 let substs = ty.substs().unwrap_or_else(Substs::empty);
63
64 let field_tys = def.map(|it| self.db.field_types(it.into())).unwrap_or_default();
65 for subpat in subpats {
66 let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name));
67 let expected_ty =
68 matching_field.map_or(Ty::Unknown, |field| field_tys[field].clone()).subst(&substs);
69 let expected_ty = self.normalize_associated_types_in(expected_ty);
70 self.infer_pat(subpat.pat, &expected_ty, default_bm);
71 }
72
73 ty
74 }
75
76 pub(super) fn infer_pat(
77 &mut self,
78 pat: PatId,
79 mut expected: &Ty,
80 mut default_bm: BindingMode,
81 ) -> Ty {
82 let body = Arc::clone(&self.body); // avoid borrow checker problem
83
84 let is_non_ref_pat = match &body[pat] {
85 Pat::Tuple(..)
86 | Pat::TupleStruct { .. }
87 | Pat::Record { .. }
88 | Pat::Range { .. }
89 | Pat::Slice { .. } => true,
90 // FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented.
91 Pat::Path(..) | Pat::Lit(..) => true,
92 Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false,
93 };
94 if is_non_ref_pat {
95 while let Some((inner, mutability)) = expected.as_reference() {
96 expected = inner;
97 default_bm = match default_bm {
98 BindingMode::Move => BindingMode::Ref(mutability),
99 BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared),
100 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
101 }
102 }
103 } else if let Pat::Ref { .. } = &body[pat] {
104 tested_by!(match_ergonomics_ref);
105 // When you encounter a `&pat` pattern, reset to Move.
106 // This is so that `w` is by value: `let (_, &w) = &(1, &2);`
107 default_bm = BindingMode::Move;
108 }
109
110 // Lose mutability.
111 let default_bm = default_bm;
112 let expected = expected;
113
114 let ty = match &body[pat] {
115 Pat::Tuple(ref args) => {
116 let expectations = match expected.as_tuple() {
117 Some(parameters) => &*parameters.0,
118 _ => &[],
119 };
120 let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown));
121
122 let inner_tys = args
123 .iter()
124 .zip(expectations_iter)
125 .map(|(&pat, ty)| self.infer_pat(pat, ty, default_bm))
126 .collect();
127
128 Ty::apply(TypeCtor::Tuple { cardinality: args.len() as u16 }, Substs(inner_tys))
129 }
130 Pat::Ref { pat, mutability } => {
131 let expectation = match expected.as_reference() {
132 Some((inner_ty, exp_mut)) => {
133 if *mutability != exp_mut {
134 // FIXME: emit type error?
135 }
136 inner_ty
137 }
138 _ => &Ty::Unknown,
139 };
140 let subty = self.infer_pat(*pat, expectation, default_bm);
141 Ty::apply_one(TypeCtor::Ref(*mutability), subty)
142 }
143 Pat::TupleStruct { path: p, args: subpats } => {
144 self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm)
145 }
146 Pat::Record { path: p, args: fields } => {
147 self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat)
148 }
149 Pat::Path(path) => {
150 // FIXME use correct resolver for the surrounding expression
151 let resolver = self.resolver.clone();
152 self.infer_path(&resolver, &path, pat.into()).unwrap_or(Ty::Unknown)
153 }
154 Pat::Bind { mode, name: _, subpat } => {
155 let mode = if mode == &BindingAnnotation::Unannotated {
156 default_bm
157 } else {
158 BindingMode::convert(*mode)
159 };
160 let inner_ty = if let Some(subpat) = subpat {
161 self.infer_pat(*subpat, expected, default_bm)
162 } else {
163 expected.clone()
164 };
165 let inner_ty = self.insert_type_vars_shallow(inner_ty);
166
167 let bound_ty = match mode {
168 BindingMode::Ref(mutability) => {
169 Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone())
170 }
171 BindingMode::Move => inner_ty.clone(),
172 };
173 let bound_ty = self.resolve_ty_as_possible(&mut vec![], bound_ty);
174 self.write_pat_ty(pat, bound_ty);
175 return inner_ty;
176 }
177 _ => Ty::Unknown,
178 };
179 // use a new type variable if we got Ty::Unknown here
180 let ty = self.insert_type_vars_shallow(ty);
181 self.unify(&ty, expected);
182 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
183 self.write_pat_ty(pat, ty.clone());
184 ty
185 }
186}