Skip to content

Commit d31d8a9

Browse files
committed
Auto merge of #32039 - arielb1:pat-enum, r=nagisa
Fixes #32004 r? @eddyb
2 parents c97524b + f40d12f commit d31d8a9

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

src/librustc_typeck/check/_match.rs

+29-47
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
203203
check_pat_enum(pcx, pat, path, subpats.as_ref().map(|v| &v[..]), expected, true);
204204
}
205205
PatKind::Path(ref path) => {
206-
check_pat_enum(pcx, pat, path, None, expected, false);
206+
check_pat_enum(pcx, pat, path, Some(&[]), expected, false);
207207
}
208208
PatKind::QPath(ref qself, ref path) => {
209209
let self_ty = fcx.to_ty(&qself.ty);
@@ -597,12 +597,12 @@ fn bad_struct_kind_err(sess: &Session, pat: &hir::Pat, path: &hir::Path, lint: b
597597
}
598598
}
599599

600-
pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
601-
pat: &hir::Pat,
602-
path: &hir::Path,
603-
subpats: Option<&'tcx [P<hir::Pat>]>,
604-
expected: Ty<'tcx>,
605-
is_tuple_struct_pat: bool)
600+
fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
601+
pat: &hir::Pat,
602+
path: &hir::Path,
603+
subpats: Option<&'tcx [P<hir::Pat>]>,
604+
expected: Ty<'tcx>,
605+
is_tuple_struct_pat: bool)
606606
{
607607
// Typecheck the path.
608608
let fcx = pcx.fcx;
@@ -685,59 +685,41 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
685685
demand::eqtype(fcx, pat.span, expected, pat_ty);
686686

687687
let real_path_ty = fcx.node_ty(pat.id);
688-
let (arg_tys, kind_name): (Vec<_>, &'static str) = match real_path_ty.sty {
688+
let (kind_name, variant, expected_substs) = match real_path_ty.sty {
689689
ty::TyEnum(enum_def, expected_substs) => {
690690
let variant = enum_def.variant_of_def(def);
691-
if variant.kind() == ty::VariantKind::Struct {
692-
report_bad_struct_kind(false);
693-
return;
694-
}
695-
if is_tuple_struct_pat && variant.kind() != ty::VariantKind::Tuple {
696-
// Matching unit variants with tuple variant patterns (`UnitVariant(..)`)
697-
// is allowed for backward compatibility.
698-
let is_special_case = variant.kind() == ty::VariantKind::Unit;
699-
report_bad_struct_kind(is_special_case);
700-
if !is_special_case {
701-
return
702-
}
703-
}
704-
(variant.fields
705-
.iter()
706-
.map(|f| fcx.instantiate_type_scheme(pat.span,
707-
expected_substs,
708-
&f.unsubst_ty()))
709-
.collect(),
710-
"variant")
691+
("variant", variant, expected_substs)
711692
}
712693
ty::TyStruct(struct_def, expected_substs) => {
713694
let variant = struct_def.struct_variant();
714-
if is_tuple_struct_pat && variant.kind() != ty::VariantKind::Tuple {
715-
// Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
716-
// is allowed for backward compatibility.
717-
let is_special_case = variant.kind() == ty::VariantKind::Unit;
718-
report_bad_struct_kind(is_special_case);
719-
return;
720-
}
721-
(variant.fields
722-
.iter()
723-
.map(|f| fcx.instantiate_type_scheme(pat.span,
724-
expected_substs,
725-
&f.unsubst_ty()))
726-
.collect(),
727-
"struct")
695+
("struct", variant, expected_substs)
728696
}
729697
_ => {
730698
report_bad_struct_kind(false);
731699
return;
732700
}
733701
};
734702

703+
match (is_tuple_struct_pat, variant.kind()) {
704+
(true, ty::VariantKind::Unit) => {
705+
// Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
706+
// is allowed for backward compatibility.
707+
report_bad_struct_kind(true);
708+
}
709+
(_, ty::VariantKind::Struct) => {
710+
report_bad_struct_kind(false);
711+
return
712+
}
713+
_ => {}
714+
}
715+
735716
if let Some(subpats) = subpats {
736-
if subpats.len() == arg_tys.len() {
737-
for (subpat, arg_ty) in subpats.iter().zip(arg_tys) {
738-
check_pat(pcx, &subpat, arg_ty);
717+
if subpats.len() == variant.fields.len() {
718+
for (subpat, field) in subpats.iter().zip(&variant.fields) {
719+
let field_ty = fcx.field_ty(subpat.span, field, expected_substs);
720+
check_pat(pcx, &subpat, field_ty);
739721
}
740-
} else if arg_tys.is_empty() {
722+
} else if variant.fields.is_empty() {
741723
span_err!(tcx.sess, pat.span, E0024,
742724
"this pattern has {} field{}, but the corresponding {} has no fields",
743725
subpats.len(), if subpats.len() == 1 {""} else {"s"}, kind_name);
@@ -750,7 +732,7 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
750732
"this pattern has {} field{}, but the corresponding {} has {} field{}",
751733
subpats.len(), if subpats.len() == 1 {""} else {"s"},
752734
kind_name,
753-
arg_tys.len(), if arg_tys.len() == 1 {""} else {"s"});
735+
variant.fields.len(), if variant.fields.len() == 1 {""} else {"s"});
754736

755737
for pat in subpats {
756738
check_pat(pcx, &pat, tcx.types.err);

src/test/compile-fail/issue-32004.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum Foo {
12+
Bar(i32),
13+
Baz
14+
}
15+
16+
struct S;
17+
18+
fn main() {
19+
match Foo::Baz {
20+
Foo::Bar => {}
21+
//~^ ERROR this pattern has 0 fields, but the corresponding variant
22+
_ => {}
23+
}
24+
25+
match S {
26+
S(()) => {}
27+
//~^ ERROR this pattern has 1 field, but the corresponding struct
28+
}
29+
}

0 commit comments

Comments
 (0)