Skip to content

Commit 7cdc800

Browse files
committed
Fix reversed current/expected type
It is reversed that type of arm pattern and type of search pattern in error message.
1 parent 0aa1aaa commit 7cdc800

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
103103
// check that the type of the value being matched is a subtype
104104
// of the type of the pattern:
105105
let pat_ty = fcx.node_ty(pat.id);
106-
demand::suptype(fcx, pat.span, pat_ty, expected);
106+
demand::subtype(fcx, pat.span, expected, pat_ty);
107107

108108
// Get the expected types of the arguments.
109109
arg_types = {
@@ -142,7 +142,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
142142
// Check that the type of the value being matched is a subtype of
143143
// the type of the pattern.
144144
let pat_ty = fcx.node_ty(pat.id);
145-
demand::suptype(fcx, pat.span, pat_ty, expected);
145+
demand::subtype(fcx, pat.span, expected, pat_ty);
146146

147147
// Get the expected types of the arguments.
148148
let class_fields = ty::struct_fields(
@@ -153,7 +153,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
153153
}
154154
_ => {
155155
tcx.sess.span_fatal(
156-
pat.span,
156+
pat.span, // XXX: should be discrim.span or other error
157157
fmt!("mismatched types: expected enum or structure but \
158158
found `%s`",
159159
fcx.infcx().ty_to_str(expected)));

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,25 @@ use syntax::codemap::span;
2121
// Requires that the two types unify, and prints an error message if they
2222
// don't.
2323
pub fn suptype(fcx: @mut FnCtxt, sp: span, expected: ty::t, actual: ty::t) {
24-
suptype_with_fn(fcx, sp, expected, actual,
24+
suptype_with_fn(fcx, sp, false, expected, actual,
2525
|sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) })
2626
}
2727

28+
pub fn subtype(fcx: @mut FnCtxt, sp: span, expected: ty::t, actual: ty::t) {
29+
suptype_with_fn(fcx, sp, true, actual, expected,
30+
|sp, a, e, s| { fcx.report_mismatched_types(sp, e, a, s) })
31+
}
32+
2833
pub fn suptype_with_fn(fcx: @mut FnCtxt,
29-
sp: span,
30-
expected: ty::t, actual: ty::t,
34+
sp: span, a_is_actual: bool,
35+
ty_a: ty::t, ty_b: ty::t,
3136
handle_err: fn(span, ty::t, ty::t, &ty::type_err)) {
3237
// n.b.: order of actual, expected is reversed
33-
match infer::mk_subty(fcx.infcx(), false, sp,
34-
actual, expected) {
38+
match infer::mk_subty(fcx.infcx(), a_is_actual, sp,
39+
ty_b, ty_a) {
3540
result::Ok(()) => { /* ok */ }
3641
result::Err(ref err) => {
37-
handle_err(sp, expected, actual, err);
42+
handle_err(sp, ty_a, ty_b, err);
3843
}
3944
}
4045
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ pub fn check_fn(ccx: @mut CrateCtxt,
355355
let tail_expr_ty = fcx.expr_ty(tail_expr);
356356
// Special case: we print a special error if there appears
357357
// to be do-block/for-loop confusion
358-
demand::suptype_with_fn(fcx, tail_expr.span, fcx.ret_ty, tail_expr_ty,
358+
demand::suptype_with_fn(fcx, tail_expr.span, false,
359+
fcx.ret_ty, tail_expr_ty,
359360
|sp, e, a, s| {
360361
fcx.report_mismatched_return_types(sp, e, a, s) });
361362
}

src/test/compile-fail/match-struct.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// error-pattern: mismatched types
21

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

65
fn main() {
76
match S { a: 1 } {
8-
C(_) => (),
7+
C(_) => (), //~ ERROR mismatched types: expected `S` but found `E`
98
_ => ()
109
}
1110
}

0 commit comments

Comments
 (0)