Skip to content

Check type of fields when struct is matched against enum-like pattern #5055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2823,7 +2823,7 @@ pub pure fn ty_fn_ret(fty: t) -> t {
}
}

fn is_fn_ty(fty: t) -> bool {
pub fn is_fn_ty(fty: t) -> bool {
match get(fty).sty {
ty_bare_fn(_) => true,
ty_closure(_) => true,
Expand Down
49 changes: 44 additions & 5 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
match structure_of(pcx.fcx, pat.span, expected) {
ty::ty_enum(_, ref expected_substs) => {
// Lookup the enum and variant def ids:
let v_def = lookup_def(pcx.fcx, path.span, pat.id);
let v_def = lookup_def(pcx.fcx, pat.span, pat.id);
let v_def_ids = ast_util::variant_def_ids(v_def);

// Assign the pattern the type of the *enum*, not the variant.
Expand Down Expand Up @@ -130,10 +130,49 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
instantiate_path(pcx.fcx, path, struct_tpt, pat.span, pat.id,
pcx.block_region);

// Check that the type of the value being matched is a subtype of
// the type of the pattern.
let pat_ty = fcx.node_ty(pat.id);
demand::suptype(fcx, pat.span, pat_ty, expected);
let s_tcx = pcx.fcx.ccx.tcx;

// Check to ensure that the tuple is the one specified.
match s_tcx.def_map.find(&pat.id) {
Some(ast::def_struct(supplied_def_id))
if supplied_def_id == struct_def_id => {
// OK
}
Some(ast::def_struct(*)) if subpats != None => {
let class_fields = ty::lookup_struct_fields(tcx,
struct_def_id);
for class_fields.eachi |i, class_field| {
let field_type = ty::lookup_field_type(tcx, struct_def_id,
class_field.id,
expected_substs);
match /*bad*/copy subpats {
Some(pats) => {
check_pat(pcx, pats[i], field_type);
}
_ => {
let name = pprust::path_to_str(path, tcx.sess.intr());
tcx.sess.span_err(pat.span,
fmt!("mismatched types: expected \
`%s` but found `%s`",
pcx.fcx.infcx().ty_to_str(expected),
name)); // unreachable?
}
}
}
}
Some(ast::def_struct(*)) | Some(ast::def_variant(*)) => {
let name = pprust::path_to_str(path, tcx.sess.intr());
tcx.sess.span_err(pat.span,
fmt!("mismatched types: expected `%s` \
but found `%s`",
pcx.fcx.infcx().ty_to_str(expected),
name));
}
_ => {
s_tcx.sess.span_bug(pat.span, ~"resolve didn't write in \
class");
}
}

// Get the expected types of the arguments.
let class_fields = ty::struct_fields(
Expand Down
11 changes: 11 additions & 0 deletions src/test/compile-fail/match-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// error-pattern: mismatched types

struct S { a: int }
enum E { C(int) }

fn main() {
match S { a: 1 } {
C(_) => (),
_ => ()
}
}