Skip to content

Commit c5ff28c

Browse files
committed
Factor write_ty out of pattern-matching functions
1 parent 8c6086a commit c5ff28c

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5454
}
5555
}
5656

57-
self.write_ty(pat.id, pat_ty);
58-
5957
// somewhat surprising: in this case, the subtyping
6058
// relation goes the opposite way as the other
6159
// cases. Actually what we really want is not a subtyping
@@ -69,6 +67,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
6967
//
7068
// that's equivalent to there existing a LUB.
7169
self.demand_suptype(pat.span, expected, pat_ty);
70+
self.write_ty(pat.id, pat_ty);
7271
}
7372
PatKind::Range(ref begin, ref end) => {
7473
let lhs_ty = self.check_expr(begin);
@@ -101,11 +100,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
101100
// it to type the entire expression.
102101
let common_type = self.resolve_type_vars_if_possible(&lhs_ty);
103102

104-
self.write_ty(pat.id, common_type);
105-
106103
// subtyping doesn't matter here, as the value is some kind of scalar
107104
self.demand_eqtype(pat.span, expected, lhs_ty);
108105
self.demand_eqtype(pat.span, expected, rhs_ty);
106+
self.write_ty(pat.id, common_type);
109107
}
110108
PatKind::Binding(bm, _, ref sub) => {
111109
let typ = self.local_ty(pat.span, pat.id);
@@ -132,8 +130,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
132130
}
133131
}
134132

135-
self.write_ty(pat.id, typ);
136-
137133
// if there are multiple arms, make sure they all agree on
138134
// what the type of the binding `x` ought to be
139135
match tcx.expect_def(pat.id) {
@@ -150,6 +146,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
150146
if let Some(ref p) = *sub {
151147
self.check_pat(&p, expected);
152148
}
149+
150+
self.write_ty(pat.id, typ);
153151
}
154152
PatKind::TupleStruct(ref path, ref subpats, ddpos) => {
155153
self.check_pat_tuple_struct(pat, path, &subpats, ddpos, expected);
@@ -174,11 +172,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
174172

175173
let element_tys: Vec<_> = (0 .. max_len).map(|_| self.next_ty_var()).collect();
176174
let pat_ty = tcx.mk_tup(element_tys.clone());
177-
self.write_ty(pat.id, pat_ty);
178175
self.demand_eqtype(pat.span, expected, pat_ty);
179176
for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
180177
self.check_pat(elem, &element_tys[i]);
181178
}
179+
self.write_ty(pat.id, pat_ty);
182180
}
183181
PatKind::Box(ref inner) => {
184182
let inner_ty = self.next_ty_var();
@@ -189,11 +187,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
189187
// think any errors can be introduced by using
190188
// `demand::eqtype`.
191189
self.demand_eqtype(pat.span, expected, uniq_ty);
192-
self.write_ty(pat.id, uniq_ty);
193190
self.check_pat(&inner, inner_ty);
191+
self.write_ty(pat.id, uniq_ty);
194192
} else {
195-
self.write_error(pat.id);
196193
self.check_pat(&inner, tcx.types.err);
194+
self.write_error(pat.id);
197195
}
198196
}
199197
PatKind::Ref(ref inner, mutbl) => {
@@ -221,11 +219,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
221219
}
222220
};
223221

224-
self.write_ty(pat.id, rptr_ty);
225222
self.check_pat(&inner, inner_ty);
223+
self.write_ty(pat.id, rptr_ty);
226224
} else {
227-
self.write_error(pat.id);
228225
self.check_pat(&inner, tcx.types.err);
226+
self.write_error(pat.id);
229227
}
230228
}
231229
PatKind::Vec(ref before, ref slice, ref after) => {
@@ -277,8 +275,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
277275
}
278276
};
279277

280-
self.write_ty(pat.id, expected_ty);
281-
282278
for elt in before {
283279
self.check_pat(&elt, inner_ty);
284280
}
@@ -288,6 +284,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
288284
for elt in after {
289285
self.check_pat(&elt, inner_ty);
290286
}
287+
self.write_ty(pat.id, expected_ty);
291288
}
292289
}
293290

0 commit comments

Comments
 (0)