Skip to content

Commit 1749fda

Browse files
committed
Factor write_ty out of more pattern-checking functions
1 parent c5ff28c commit 1749fda

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
150150
self.write_ty(pat.id, typ);
151151
}
152152
PatKind::TupleStruct(ref path, ref subpats, ddpos) => {
153-
self.check_pat_tuple_struct(pat, path, &subpats, ddpos, expected);
153+
let pat_ty = self.check_pat_tuple_struct(pat, path, &subpats, ddpos, expected);
154+
write_ty(pat.id, pat_ty);
154155
}
155156
PatKind::Path(ref opt_qself, ref path) => {
156157
let opt_qself_ty = opt_qself.as_ref().map(|qself| self.to_ty(&qself.ty));
157-
self.check_pat_path(pat, opt_qself_ty, path, expected);
158+
let pat_ty = self.check_pat_path(pat, opt_qself_ty, path, expected);
159+
write_ty(pat.id, pat_ty);
158160
}
159161
PatKind::Struct(ref path, ref fields, etc) => {
160-
self.check_pat_struct(pat, path, fields, etc, expected);
162+
let pat_ty = self.check_pat_struct(pat, path, fields, etc, expected);
163+
write_ty(pat.id, pat_ty);
161164
}
162165
PatKind::Tuple(ref elements, ddpos) => {
163166
let mut expected_len = elements.len();
@@ -481,40 +484,38 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
481484
path: &hir::Path,
482485
fields: &'gcx [Spanned<hir::FieldPat>],
483486
etc: bool,
484-
expected: Ty<'tcx>)
487+
expected: Ty<'tcx>) -> Ty<'tcx>
485488
{
486489
// Resolve the path and check the definition for errors.
487490
let (variant, pat_ty) = if let Some(variant_ty) = self.check_struct_path(path, pat.id,
488491
pat.span) {
489492
variant_ty
490493
} else {
491-
self.write_error(pat.id);
492494
for field in fields {
493495
self.check_pat(&field.node.pat, self.tcx.types.err);
494496
}
495-
return;
497+
return tcx.types.err;
496498
};
497-
self.write_ty(pat.id, pat_ty);
498499

499500
// Type check the path.
500501
self.demand_eqtype(pat.span, expected, pat_ty);
501502

502503
// Type check subpatterns.
503504
self.check_struct_pat_fields(pat_ty, pat.span, variant, fields, etc);
505+
pat_ty
504506
}
505507

506508
fn check_pat_path(&self,
507509
pat: &hir::Pat,
508510
opt_self_ty: Option<Ty<'tcx>>,
509511
path: &hir::Path,
510-
expected: Ty<'tcx>)
512+
expected: Ty<'tcx>) -> Ty<'tcx>
511513
{
512514
let tcx = self.tcx;
513515
let report_unexpected_def = || {
514516
span_err!(tcx.sess, pat.span, E0533,
515517
"`{}` does not name a unit variant, unit struct or a constant",
516518
pprust::path_to_string(path));
517-
self.write_error(pat.id);
518519
};
519520

520521
// Resolve the path and check the definition for errors.
@@ -523,18 +524,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
523524
match def {
524525
Def::Err => {
525526
self.set_tainted_by_errors();
526-
self.write_error(pat.id);
527-
return;
527+
return tcx.types.err;
528528
}
529529
Def::Method(..) => {
530530
report_unexpected_def();
531-
return;
531+
return tcx.types.err;
532532
}
533533
Def::Variant(..) | Def::Struct(..) => {
534534
let variant = tcx.expect_variant_def(def);
535535
if variant.kind != VariantKind::Unit {
536536
report_unexpected_def();
537-
return;
537+
return tcx.types.err;
538538
}
539539
}
540540
Def::Const(..) | Def::AssociatedConst(..) => {} // OK
@@ -543,20 +543,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
543543

544544
// Type check the path.
545545
let pat_ty = self.instantiate_value_path(segments, opt_ty, def, pat.span, pat.id);
546-
self.write_ty(pat.id, pat_ty);
547546
self.demand_suptype(pat.span, expected, pat_ty);
547+
pat_ty
548548
}
549549

550550
fn check_pat_tuple_struct(&self,
551551
pat: &hir::Pat,
552552
path: &hir::Path,
553553
subpats: &'gcx [P<hir::Pat>],
554554
ddpos: Option<usize>,
555-
expected: Ty<'tcx>)
555+
expected: Ty<'tcx>) -> Ty<'tcx>
556556
{
557557
let tcx = self.tcx;
558558
let on_error = || {
559-
self.write_error(pat.id);
560559
for pat in subpats {
561560
self.check_pat(&pat, tcx.types.err);
562561
}
@@ -580,11 +579,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
580579
Def::Err => {
581580
self.set_tainted_by_errors();
582581
on_error();
583-
return;
582+
return tcx.types.err;
584583
}
585584
Def::Const(..) | Def::AssociatedConst(..) | Def::Method(..) => {
586585
report_unexpected_def(false);
587-
return;
586+
return tcx.types.err;
588587
}
589588
Def::Variant(..) | Def::Struct(..) => {
590589
tcx.expect_variant_def(def)
@@ -597,21 +596,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
597596
report_unexpected_def(true);
598597
} else if variant.kind != VariantKind::Tuple {
599598
report_unexpected_def(false);
600-
return;
599+
return tcx.types.err;
601600
}
602601

603602
// Type check the path.
604603
let pat_ty = self.instantiate_value_path(segments, opt_ty, def, pat.span, pat.id);
605-
self.write_ty(pat.id, pat_ty);
606-
607604
let pat_ty = if pat_ty.is_fn() {
608605
// Replace constructor type with constructed type for tuple struct patterns.
609606
tcx.no_late_bound_regions(&pat_ty.fn_ret()).unwrap()
610607
} else {
611608
// Leave the type as is for unit structs (backward compatibility).
612609
pat_ty
613610
};
614-
self.write_ty(pat.id, pat_ty);
615611
self.demand_eqtype(pat.span, expected, pat_ty);
616612

617613
// Type check subpatterns.
@@ -644,7 +640,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
644640
variant.fields.len(), fields_ending, subpats.len()))
645641
.emit();
646642
on_error();
643+
return tcx.types.err;
647644
}
645+
pat_ty
648646
}
649647

650648
fn check_struct_pat_fields(&self,

0 commit comments

Comments
 (0)