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