Skip to content

Fix reversed current/expected type #5070

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

Merged
merged 1 commit into from
Feb 27, 2013
Merged
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
8 changes: 4 additions & 4 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
// 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);
demand::subtype(fcx, pat.span, expected, pat_ty);

// Get the expected types of the arguments.
arg_types = {
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
// 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);
demand::subtype(fcx, pat.span, expected, pat_ty);

// Get the expected types of the arguments.
let class_fields = ty::struct_fields(
Expand All @@ -154,8 +154,8 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
_ => {
tcx.sess.span_fatal(
pat.span,
fmt!("mismatched types: expected enum or structure but \
found `%s`",
fmt!("mismatched types: expected `%s` but found enum or \
structure",
fcx.infcx().ty_to_str(expected)));
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/librustc/middle/typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ use syntax::codemap::span;
// Requires that the two types unify, and prints an error message if they
// don't.
pub fn suptype(fcx: @mut FnCtxt, sp: span, expected: ty::t, actual: ty::t) {
suptype_with_fn(fcx, sp, expected, actual,
suptype_with_fn(fcx, sp, false, expected, actual,
|sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) })
}

pub fn subtype(fcx: @mut FnCtxt, sp: span, expected: ty::t, actual: ty::t) {
suptype_with_fn(fcx, sp, true, actual, expected,
|sp, a, e, s| { fcx.report_mismatched_types(sp, e, a, s) })
}

pub fn suptype_with_fn(fcx: @mut FnCtxt,
sp: span,
expected: ty::t, actual: ty::t,
sp: span, b_is_expected: bool,
ty_a: ty::t, ty_b: ty::t,
handle_err: fn(span, ty::t, ty::t, &ty::type_err)) {
// n.b.: order of actual, expected is reversed
match infer::mk_subty(fcx.infcx(), false, sp,
actual, expected) {
match infer::mk_subty(fcx.infcx(), b_is_expected, sp,
ty_b, ty_a) {
result::Ok(()) => { /* ok */ }
result::Err(ref err) => {
handle_err(sp, expected, actual, err);
handle_err(sp, ty_a, ty_b, err);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ pub fn check_fn(ccx: @mut CrateCtxt,
let tail_expr_ty = fcx.expr_ty(tail_expr);
// Special case: we print a special error if there appears
// to be do-block/for-loop confusion
demand::suptype_with_fn(fcx, tail_expr.span, fcx.ret_ty, tail_expr_ty,
demand::suptype_with_fn(fcx, tail_expr.span, false,
fcx.ret_ty, tail_expr_ty,
|sp, e, a, s| {
fcx.report_mismatched_return_types(sp, e, a, s) });
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-3680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

fn main() {
match None {
Err(_) => () //~ ERROR expected `core::result
Err(_) => () //~ ERROR mismatched types: expected `core::option::Option<<V1>>` but found `core::result::Result<<V2>,<V3>>`
}
}
3 changes: 1 addition & 2 deletions src/test/compile-fail/match-struct.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// error-pattern: mismatched types

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

fn main() {
match S { a: 1 } {
C(_) => (),
C(_) => (), //~ ERROR mismatched types: expected `S` but found `E`
_ => ()
}
}