Skip to content

Commit 1b5cbd0

Browse files
author
Jakub Bukaj
committed
Forbid matching struct variants with tuple variant patterns
1 parent 3e94144 commit 1b5cbd0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/librustc/middle/typeck/check/_match.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>, pat: &ast::Pat,
379379

380380
let real_path_ty = fcx.node_ty(pat.id);
381381
let (arg_tys, kind_name) = match real_path_ty.sty {
382-
ty::ty_enum(enum_def_id, ref expected_substs) => {
382+
ty::ty_enum(enum_def_id, ref expected_substs)
383+
if def == def::DefVariant(enum_def_id, def.def_id(), false) => {
383384
let variant = ty::enum_variant_with_id(tcx, enum_def_id, def.def_id());
384385
(variant.args.iter().map(|t| t.subst(tcx, expected_substs)).collect::<Vec<_>>(),
385386
"variant")
@@ -392,7 +393,7 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>, pat: &ast::Pat,
392393
_ => {
393394
let name = pprust::path_to_string(path);
394395
span_err!(tcx.sess, pat.span, E0164,
395-
"`{}` does not name a variant or a tuple struct", name);
396+
"`{}` does not name a non-struct variant or a tuple struct", name);
396397
fcx.write_error(pat.id);
397398

398399
if let Some(ref subpats) = *subpats {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 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+
fn main() {
12+
enum Foo {
13+
FooB { x: i32, y: i32 }
14+
}
15+
16+
let f = FooB { x: 3, y: 4 };
17+
match f {
18+
FooB(a, b) => println!("{} {}", a, b),
19+
//~^ ERROR `FooB` does not name a non-struct variant or a tuple struct
20+
}
21+
}

0 commit comments

Comments
 (0)