Skip to content

Commit 517bfc9

Browse files
committed
auto merge of #6200 : catamorphism/rust/issue-5358, r=catamorphism
r? @nikomatsakis Previously, rustc would ICE if you matched on an enum-typed thing with a structure pattern. Error out correctly. This will close #5358
2 parents bfd3cd8 + 32b3d3e commit 517bfc9

File tree

4 files changed

+85
-34
lines changed

4 files changed

+85
-34
lines changed

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

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -114,37 +114,53 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::pat, path: @ast::Path,
114114
ty::ty_enum(_, ref expected_substs) => {
115115
// Lookup the enum and variant def ids:
116116
let v_def = lookup_def(pcx.fcx, pat.span, pat.id);
117-
let (enm, var) = ast_util::variant_def_ids(v_def);
118-
119-
// Assign the pattern the type of the *enum*, not the variant.
120-
let enum_tpt = ty::lookup_item_type(tcx, enm);
121-
instantiate_path(pcx.fcx, path, enum_tpt, pat.span, pat.id,
122-
pcx.block_region);
123-
124-
// check that the type of the value being matched is a subtype
125-
// of the type of the pattern:
126-
let pat_ty = fcx.node_ty(pat.id);
127-
demand::subtype(fcx, pat.span, expected, pat_ty);
128-
129-
// Get the expected types of the arguments.
130-
arg_types = {
131-
let vinfo =
132-
ty::enum_variant_with_id(tcx, enm, var);
133-
let var_tpt = ty::lookup_item_type(tcx, var);
134-
vinfo.args.map(|t| {
135-
if var_tpt.generics.type_param_defs.len() ==
136-
expected_substs.tps.len()
137-
{
138-
ty::subst(tcx, expected_substs, *t)
139-
}
140-
else {
141-
*t // In this case, an error was already signaled
142-
// anyway
143-
}
144-
})
145-
};
146-
147-
kind_name = "variant";
117+
match ast_util::variant_def_ids(v_def) {
118+
Some((enm, var)) => {
119+
// Assign the pattern the type of the *enum*, not the variant.
120+
let enum_tpt = ty::lookup_item_type(tcx, enm);
121+
instantiate_path(pcx.fcx, path, enum_tpt, pat.span, pat.id,
122+
pcx.block_region);
123+
124+
// check that the type of the value being matched is a subtype
125+
// of the type of the pattern:
126+
let pat_ty = fcx.node_ty(pat.id);
127+
demand::subtype(fcx, pat.span, expected, pat_ty);
128+
129+
// Get the expected types of the arguments.
130+
arg_types = {
131+
let vinfo =
132+
ty::enum_variant_with_id(tcx, enm, var);
133+
let var_tpt = ty::lookup_item_type(tcx, var);
134+
vinfo.args.map(|t| {
135+
if var_tpt.generics.type_param_defs.len() ==
136+
expected_substs.tps.len()
137+
{
138+
ty::subst(tcx, expected_substs, *t)
139+
}
140+
else {
141+
*t // In this case, an error was already signaled
142+
// anyway
143+
}
144+
})
145+
};
146+
147+
kind_name = "variant";
148+
}
149+
None => {
150+
let resolved_expected =
151+
fcx.infcx().ty_to_str(fcx.infcx().resolve_type_vars_if_possible(expected));
152+
fcx.infcx().type_error_message_str(pat.span,
153+
|actual| {
154+
fmt!("mismatched types: expected `%s` but found %s",
155+
resolved_expected, actual)},
156+
~"a structure pattern",
157+
None);
158+
fcx.write_error(pat.id);
159+
kind_name = "[error]";
160+
arg_types = (copy subpats).get_or_default(~[]).map(|_|
161+
ty::mk_err());
162+
}
163+
}
148164
}
149165
ty::ty_struct(struct_def_id, ref expected_substs) => {
150166
// Lookup the struct ctor def id

src/libsyntax/ast_util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ pub fn stmt_id(s: &stmt) -> node_id {
4141
}
4242
}
4343
44-
pub fn variant_def_ids(d: def) -> (def_id, def_id) {
44+
pub fn variant_def_ids(d: def) -> Option<(def_id, def_id)> {
4545
match d {
4646
def_variant(enum_id, var_id) => {
47-
return (enum_id, var_id);
47+
Some((enum_id, var_id))
4848
}
49-
_ => fail!(~"non-variant in variant_def_ids")
49+
_ => None
5050
}
5151
}
5252

src/test/compile-fail/issue-5358-1.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 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+
struct S(Either<uint, uint>);
12+
13+
fn main() {
14+
match S(Left(5)) {
15+
Right(_) => {} //~ ERROR mismatched types: expected `S` but found `core::either::Either
16+
_ => {}
17+
}
18+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 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+
struct S(Either<uint, uint>);
12+
13+
fn main() {
14+
match *S(Left(5)) {
15+
S(_) => {} //~ ERROR mismatched types: expected `core::either::Either<uint,uint>` but found a structure pattern
16+
}
17+
}

0 commit comments

Comments
 (0)