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